shellcheck icon indicating copy to clipboard operation
shellcheck copied to clipboard

Detect invalid identifiers in local and declare to avoid unintended side-effects

Open grische opened this issue 4 years ago • 1 comments

For new checks and feature suggestions

  • [x] https://www.shellcheck.net/ (i.e. the latest commit) currently gives no useful warnings about this
  • [x] I searched through https://github.com/koalaman/shellcheck/issues and didn't find anything related

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

#!/bin/bash

# shellcheck enable=all

function dummy() {
        local a, b
        a=1; b=2;
}
dummy
echo "${a}${b}"

Here's what shellcheck currently says:

Nothing

Here's what I wanted or expected to see:

Line 6:
        local a, b
               ^-- SCxxxx (error): invalid identifier

Running the script with a bash, it will print the following error:

$ bash /tmp/script.sh
/tmp/script.sh: line 6: local: `a,': not a valid identifier
12

grische avatar Nov 15 '21 14:11 grische

I noticed a similar problem with several of these checks: https://github.com/koalaman/shellcheck/blob/a71a13c2fca05b822cb16840792dc013ca76493f/src/ShellCheck/Analytics.hs#L4385-L4446

#!/bin/bash
# shellcheck enable=all

function dummy() {
        local 2a $b
        a=1; b=2;
}
dummy
echo "${a}${b}"

There should probably be an additional check here: https://github.com/koalaman/shellcheck/blob/a71a13c2fca05b822cb16840792dc013ca76493f/src/ShellCheck/Checks/Commands.hs#L977

grische avatar Nov 25 '23 15:11 grische