Mercurial > octave
changeset 28593:a9f1a922367b
New warning ID "Octave:infinite-loop" (bug #45143).
* pt-eval.cc (visit_simple_for_command): Call warning_with_id() rather than
just warning().
* warning_ids.m: Add documentation for new ID "Octave:infinite-loop".
* for.tst: Enable warning ID "Octave:infinite-loop" locally for BIST tests for
bug #45143.
author | Rik <rik@octave.org> |
---|---|
date | Fri, 24 Jul 2020 09:49:09 -0700 |
parents | e70c859c4bff |
children | 6e8d14f4fb2f |
files | libinterp/parse-tree/pt-eval.cc scripts/help/warning_ids.m test/for.tst |
diffstat | 3 files changed, 42 insertions(+), 10 deletions(-) [+] |
line wrap: on
line diff
--- a/libinterp/parse-tree/pt-eval.cc Fri Jul 24 01:01:35 2020 -0400 +++ b/libinterp/parse-tree/pt-eval.cc Fri Jul 24 09:49:09 2020 -0700 @@ -2530,8 +2530,9 @@ octave_idx_type steps = rng.numel (); if (octave::math::isinf (rng.limit ())) - warning ("FOR loop limit is infinite, will stop after %ld steps", - steps); + warning_with_id ("Octave:infinite-loop", + "FOR loop limit is infinite, will stop after %ld steps", + steps); for (octave_idx_type i = 0; i < steps; i++) {
--- a/scripts/help/warning_ids.m Fri Jul 24 01:01:35 2020 -0400 +++ b/scripts/help/warning_ids.m Fri Jul 24 09:49:09 2020 -0700 @@ -230,6 +230,12 @@ ## printed for implicit conversions of complex numbers to real numbers. ## By default, the @code{Octave:imag-to-real} warning is disabled. ## +## @item Octave:infinite-loop +## If the @code{Octave:infinite-loop} warning is enabled, a warning is +## printed when an infinite loop is detected such as @code{for i = 1:Inf} or +## @code{while (1)}. +## By default, the @code{Octave:infinite-loop} warning is enabled. +## ## @item Octave:language-extension ## Print warnings when using features that are unique to the Octave ## language and that may still be missing in @sc{matlab}.
--- a/test/for.tst Fri Jul 24 01:01:35 2020 -0400 +++ b/test/for.tst Fri Jul 24 09:49:09 2020 -0700 @@ -162,19 +162,44 @@ %! assert (cnt, 0); %! assert (k, cell (0,3)); -%!test <45143> +%!test <*45143> +%! warning ("on", "Octave:infinite-loop", "local"); %! fail ("for i = 0:inf; break; end", "warning", %! "FOR loop limit is infinite"); %! %! fail ("for i = 0:-1:-inf; break; end", "warning", %! "FOR loop limit is infinite"); -%!test <45143> -%! k = 0; for i = 1:inf; if (++k > 10); break; end; end +%!test <*45143> +%! warning ("on", "Octave:infinite-loop", "local"); +%! k = 0; +%! for i = 1:Inf +%! if (++k > 10) +%! break; +%! endif +%! endfor %! assert (i, 11); -%! k = 0; for i = -1:-1:-inf; if (++k > 10); break; end; end +%! +%! k = 0; +%! for i = -1:-1:-Inf +%! if (++k > 10) +%! break; +%! endif +%! endfor %! assert (i, -11); -%! k = 0; for i = 1:-inf; if (++k > 10); break; end; end -%! assert (i, zeros(1,0)); -%! k = 0; for i = 0:-1:inf; if (++k > 10); break; end; end -%! assert (i, zeros(1,0)); +%! +%! k = 0; +%! for i = 1:-Inf +%! if (++k > 10) +%! break; +%! endif +%! endfor +%! assert (i, zeros (1,0)); +%! +%! k = 0; +%! for i = 0:-1:Inf +%! if (++k > 10) +%! break; +%! endif +%! endfor +%! assert (i, zeros (1,0));