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 11:16:57 +0200

Next example:
i++ == 47
Here, the warning is clearly nonsense. We can continue to construct good
cases, bad cases, weird cases...

A logical (sub-)expression in the assignment *statement *except as #1 with
the triadic ?: is fishy.

-W




On Tue, 13 Apr 2021 at 11:11, Wolfgang Laun <wolfgang.laun@gmail.com> wrote:

> It's not so simple. See below. Is the warning a bug or not? No side effect
> - no effect.
>
> function f(x){
>    a[1] = "hooey"
> }
>
> BEGIN {
> i = 1
> f(i) == 47 ## Warning here
> print a[1]
> }
> -W
>
>
> On Tue, 13 Apr 2021 at 10:58, <arnold@skeeve.com> wrote:
>
>> Hi.
>>
>> Arkadiusz Drabczyk <arkadiusz@drabczyk.org> wrote:
>>
>> > > > The following code produces 'statement may have no effect' when run
>> > > > with --lint:
>> > > >
>> > > > BEGIN {
>> > > >     var = 1
>> > > >     var == 1 && var = 2
>> > > >     print "var ==", var
>> > > > }
>> > >
>> > > I think that it's a bug that it does so.
>> >
>> > ok, so just to make sure - is this an actual bug?
>>
>> Yes. Because:
>>
>> > > 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:
>> >
>> > > See above; if `var == 1' is used standalone as a statement,
>> > > it has no effect -- nothing is changed as result.
>> >
>> > ok, nothing is changed, that is value returned by == is not saved
>> > anywhere but it's an expression, right? == operator returns either 0
>> > or 1.
>>
>> Yes, but because the result is thrown away and nothing done with it,
>> the statement has no effect: no variables are changed, no 'if' or
>> loop statement is affected, etc.
>>
>> > 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 &&.
>>
>> And that's why I said that the current warning is a bug.
>>
>> > BTW (or maybe not?) the original snippet I posted cannot be reproduced
>> > with Busybox implementation of awk but works with mawk and FreeBSD
>> > awk. So maybe doing things like `var == 2 && var = 3' is not specified
>> > by POSIX and cannot be expected to work?
>>
>> You can expect it to work. It's perfectly fine. It sounds to me like
>> a bug in Busybox awk.
>>
>> > Subject: Re: Why 'statement may have no effect' lint warning here?
>> > To: Arkadiusz Drabczyk <arkadiusz@drabczyk.org>
>> > From: Manuel Collado <mcollado2011@gmail.com>
>> > Date: Tue, 13 Apr 2021 09:45:48 +0200
>> >
>> > [ .... ]
>> >
>> > 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
>>
>> I disagree that the warning here is valid. At least one of the
>> sub() calls will execute, so it has an effect.
>>
>> I am working to improve the lint warnings. I will  post a patch soon.
>>
>> Thanks,
>>
>> Arnold
>>
>>

-- 
Wolfgang Laun


reply via email to

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