# HG changeset patch # User Rik # Date 1571258959 25200 # Node ID 85ad4689aa05672392c35c617b02eff3155ede8c # Parent c3e24c82157f1d2fe27a80164ec522b8516c6a06 Add warning when non-scalar argument presented to ':' range operator. * NEWS: Announce new warning IDs Octave:colon-nonscalar-argument, Octave:colon-complex-argument. * basics.txi: Document that warning Octave:colon-nonscalar-argument is turned off when --traditional mode is used. * interpreter.cc (interpreter::maximum_braindamage): Disable Octave:colon-nonscalar-argument when --traditional used. * ov.cc (do_colon_op): Check that numel() == 1 for base, limit, and increment, or call warning_with_id. Change warning() to warning_with_id call for Octave:colon-complex-argument. * warning_ids.m: Document new warning IDs Octave:colon-nonscalar-argument, Octave:colon-complex-argument. * edit.m (default_user): Fix code that emitted a colon warning. diff -r c3e24c82157f -r 85ad4689aa05 NEWS --- a/NEWS Wed Oct 16 13:35:08 2019 -0400 +++ b/NEWS Wed Oct 16 13:49:19 2019 -0700 @@ -24,6 +24,12 @@ 'A-F' instead. The previous uppercase formats, `E` and `G`, no longer control the case of the output. +- New warnings have been added about questionable uses of the colon ':' + range operator. Each has a new warning ID so that it can be disabled + if desired. + Octave:colon-complex-argument : when any arg is complex + Octave:colon-nonscalar-argument : when any arg is non-scalar + #### Graphics backend - Graphic primitives now accept a color property value of `"none"` diff -r c3e24c82157f -r 85ad4689aa05 doc/interpreter/basics.txi --- a/doc/interpreter/basics.txi Wed Oct 16 13:35:08 2019 -0400 +++ b/doc/interpreter/basics.txi Wed Oct 16 13:49:19 2019 -0700 @@ -267,6 +267,7 @@ @example @group Octave:abbreviated-property-match +Octave:colon-nonscalar-argument Octave:data-file-in-path Octave:function-name-clash Octave:possible-matlab-short-circuit-operator diff -r c3e24c82157f -r 85ad4689aa05 libinterp/corefcn/interpreter.cc --- a/libinterp/corefcn/interpreter.cc Wed Oct 16 13:35:08 2019 -0400 +++ b/libinterp/corefcn/interpreter.cc Wed Oct 16 13:49:19 2019 -0700 @@ -1606,6 +1606,7 @@ Fstruct_levels_to_print (octave_value (0)); disable_warning ("Octave:abbreviated-property-match"); + disable_warning ("Octave:colon-nonscalar-argument"); disable_warning ("Octave:data-file-in-path"); disable_warning ("Octave:function-name-clash"); disable_warning ("Octave:possible-matlab-short-circuit-operator"); diff -r c3e24c82157f -r 85ad4689aa05 libinterp/octave-value/ov.cc --- a/libinterp/octave-value/ov.cc Wed Oct 16 13:35:08 2019 -0400 +++ b/libinterp/octave-value/ov.cc Wed Oct 16 13:49:19 2019 -0700 @@ -2550,9 +2550,15 @@ bool result_is_str = (base.is_string () && limit.is_string ()); bool dq_str = (base.is_dq_string () || limit.is_dq_string ()); + if (base.numel () > 1 || limit.numel () > 1 + || (increment.is_defined () && increment.numel () > 1)) + warning_with_id ("Octave:colon-nonscalar-argument", + "colon arguments should be scalars"); + if (base.iscomplex () || limit.iscomplex () || (increment.is_defined () && increment.iscomplex ())) - warning ("imaginary part of complex colon arguments is ignored"); + warning_with_id ("Octave:colon-complex-argument", + "imaginary part of complex colon arguments is ignored"); Matrix m_base, m_limit, m_increment; diff -r c3e24c82157f -r 85ad4689aa05 scripts/help/warning_ids.m --- a/scripts/help/warning_ids.m Wed Oct 16 13:35:08 2019 -0400 +++ b/scripts/help/warning_ids.m Wed Oct 16 13:49:19 2019 -0700 @@ -140,6 +140,20 @@ ## By default, the @code{Octave:built-in-variable-assignment} warning is ## enabled. ## +## @item Octave:colon-complex-argument +## If the @code{Octave:colon-complex-argument} warning is enabled, a warning +## is issued when one of the three arguments to the colon operator (base, +## increment, limit) is a complex value. For example, @code{1:3*i} will +## cause a warning to be emitted. +## By default, the @code{Octave:colon-complex-argument} warning is enabled. +## +## @item Octave:colon-nonscalar-argument +## If the @code{Octave:colon-nonscalar-argument} warning is enabled, a warning +## is issued when one of the three arguments to the colon operator (base, +## increment, limit) is not a scalar. For example, @code{1:[3, 5]} will +## cause a warning to be emitted. +## By default, the @code{Octave:colon-nonscalar-argument} warning is enabled. +## ## @item Octave:data-file-in-path ## If the @code{Octave:data-file-in-path} warning is enabled, a warning is ## issued when Octave does not find the target of a file operation such as diff -r c3e24c82157f -r 85ad4689aa05 scripts/miscellaneous/edit.m --- a/scripts/miscellaneous/edit.m Wed Oct 16 13:35:08 2019 -0400 +++ b/scripts/miscellaneous/edit.m Wed Oct 16 13:49:19 2019 -0700 @@ -553,7 +553,7 @@ retval = ent.gecos; pos = strfind (retval, ","); if (! isempty (pos)) - retval = retval(1:pos-1); + retval = retval(1:pos(1)-1); endif else retval = ent.name;