bug-gawk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Why 'statement may have no effect' lint warning here?


From: Wolfgang Laun
Subject: Re: Why 'statement may have no effect' lint warning here?
Date: Tue, 13 Apr 2021 10:20:26 +0200

Just ask your Software Quality Dptmt what they think of using a logical
expression as an alternative to an if statement without else.
-W


On Tue, 13 Apr 2021 at 09:46, Manuel Collado <mcollado2011@gmail.com> wrote:

> El 12/04/2021 a las 23:15, Arkadiusz Drabczyk escribió:
> >>> The following code produces 'statement may have no effect' when run
> >>> with --lint:
> >>>
> >>> BEGIN {
> >>>      var = 1
> >>>      var == 1 && var = 2
> >>>      print "var ==", var
> >...
> >> The intent for that
> >> warning is soemthing like:
> >
> >> BEGIN {
> >>      var = 1
> >>      var == 2        # no effect
> >> }
> >
> > I see why lint would warn me in that case but I still don't understand
> > that part:
> >...
> > nothing is changed, that is value returned by == is not saved
> > anywhere but it's an expression, right? == operator returns either 0
> > or 1. That
> >
> >      var = 1
> >      var == 2 && var = 3
> >      print "var ==", var
> >
> > would print 1 because var == 2 is not true and `var = 3' would not be
> > executed. So `var == 2' has an effect because it effects the other
> > side of &&.
>
> The lint warning is 'statement may have no effect'. The key part is the
> 'may' word. Just a hint that in some cases what the programmer wrote was
> not what (s)he wanted.
>
> A more elaborate example:
>
> $ cat no-effect.awk
> BEGIN {
>      var = "abcd"
>      sub("a", "x", var) && sub("c", "y", var)
>      print "var ==", var
>
>      var = "abcd"
>      sub("e", "x", var) && sub("c", "y", var)
>      print "var ==", var
> }
>
> $ gawk --lint -f no-effect.awk
> gawk: no-effect.awk:3: warning: statement may have no effect
> gawk: no-effect.awk:7: warning: statement may have no effect
> var == xbyd
> var == abcd
>
> At first sight the && expression looks like if both substitutions
> should be made in all cases, but in fact they aren't. So a lint warning
> makes sense.
>
> HTH.
> --
> Manuel Collado - http://mcollado.z15.es
>
>

-- 
Wolfgang Laun


reply via email to

[Prev in Thread] Current Thread [Next in Thread]