shellcheck icon indicating copy to clipboard operation
shellcheck copied to clipboard

Arithmetic expression creates SC2125 false positive

Open ronburk opened this issue 8 years ago • 2 comments

See related difficulties with the integer attribute causing false positives in #189

For bugs

  • Rule Id SC2125 (also SC2034)
  • My shellcheck version 0.4.6
  • [X ] I tried on shellcheck.net and verified that this is still a problem on the latest commit

Here's a snippet or screenshot that shows the problem:

#!/bin/bash
function foo(){
    local -i iArgc="$1"
    : ...
    local -i ArgCount=BASH_ARGC[iArgc]
    printf "%d\n" "$ArgCount"
}
foo 0

Here's what shellcheck currently says:

In test3.sh line 3:
    local -i iArgc="$1"
             ^-- SC2034: iArgc appears unused. Verify it or export it.


In test3.sh line 5:
    local -i ArgCount=BASH_ARGC[iArgc]
                      ^-- SC2125: Brace expansions and globs are literal in assignments. Quote it or use an array.

Here's what I wanted or expected to see:

As far as I can see, both are false positives. The SC2034 problem is perhaps the same as referenced in #189. Guessing without reading the code, perhaps the SC2125 arises from simply not treating variables with the -i attribute any differently, in which case the brackets are mistaken for glob pattern characters. Ironically, that makes the "or use an array" message extra confusing, since of course I am using an array. :-)

ronburk avatar May 23 '17 21:05 ronburk

Having this issue as well. As a side note, when is [] square brackets used for glob patterns, anyway? Is there even a reason for SC2125 to flag square brackets, independent of the arithmetic expression issue?

kwshi avatar Jul 22 '21 23:07 kwshi

@kwshi

As a side note, when is [] square brackets used for glob patterns, anyway?

The short answer is "always, except when there's an defined exception".

Right up front it's worth noting that if a glob expansion does not match anything, it will be left unchanged, so it might seem like glob expansions only happen occasionally.

The exceptions include:

  • On the right hand side of a bare assignment
  • On the right hand side of an assignment that's an argument to an unquoted special built-in command
    • declare
    • export
    • local
    • readonly
  • In the selector word between case and in
  • Inside ((...))
  • Inside [[...]] other than on the right side of = or == or =~

(and not quoted, of course).

Compare these two uses of the declare built-in command:

$ touch a=b0
$ declare a=b[0] ; echo "[$a]"    # no globbing because declare is special
[b[0]]
$ 'declare' a=b[0] ; echo "[$a]"  # globbing because 'declare' is not special
[b0]

kurahaupo avatar Mar 12 '25 03:03 kurahaupo