bug-gawk
[Top][All Lists]
Advanced

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

Re: Lint warning when passing strnum to 'length'


From: Wolfgang Laun
Subject: Re: Lint warning when passing strnum to 'length'
Date: Tue, 15 Mar 2022 07:04:10 +0100

It may not be possible to learn about the rationale for the decision to
have lint warn when length() is applied to a strnum. It might be very
interesting to hear about the reason for not issuing a warning when
substr() or pattern matching is applied to a strnum, especially when you
consider the interpretations, i.e., "How many digits does this number
have?" vs. "What is the second digit from the left of this number?" and
"Does this number contain 42?" Simply put, length might be the most likely
intentional operation when applied to a number. (There is no need to remind
me that a number may have a sign, or a decimal point, or both.)

The only way to make everybody happy is (a) to add a warning in all
applications of string operations to a strnum and (b) to make all warnings
configurable (as many code checkers in several other languages are).

Regards,
Wolfgang


On Mon, 14 Mar 2022 at 21:11, Timothy Sample <samplet@ngyro.com> wrote:

> Hello!
>
> While using ‘gawk --lint’ just now, I received many warnings:
>
>     warning: length: received non-string argument
>
> However, what I am passing is an “strnum”.  Even though the input string
> looks like a number, I’m happy to treat it like a string, which makes
> the lint warning net very useful to me.
>
> The following demonstrates the behaviour:
>
>     echo 123 | gawk --lint '{ print(length($1)); }'
>
> (BTW, this is on GNU Awk 5.1.0 from Guix.)
>
> There’s every possibility that this is intentional, but my naïve
> expectation is that Gawk should be okay with passing an strnum to
> ‘length’.  I am aware that I can suppress the warning by forcing Gawk to
> treat the input as a string (‘length($1 "")’), but I’m not sure I should
> have to.  Thoughts?
>
> In general, I am very impressed with the Gawk linter and also the
> “--posix” flag, which help me write cleaner, more portable Awk
> programs.  Thank you very much!
>
>
> -- Tim
>
>

-- 
Wolfgang Laun


reply via email to

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