Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SC2178/SC2128 false positives on local variables #680

Open
knirch opened this issue May 29, 2016 · 4 comments
Open

SC2178/SC2128 false positives on local variables #680

knirch opened this issue May 29, 2016 · 4 comments

Comments

@knirch
Copy link

knirch commented May 29, 2016

#!/bin/bash

foo () {
   local baz=()
   echo "${baz[@]}"
}

bar () {
   local baz=""
   # Variable was used as an array but is now assigned a string. [SC2178]
   echo "$baz"
   # Expanding an array without an index only gives the first element. [SC2128]
}
@knirch
Copy link
Author

knirch commented May 29, 2016

Saw the mention on the SC2178 page. Close if you want to, not too much refactoring needed to work around this.

@knirch
Copy link
Author

knirch commented Oct 15, 2018

Unsure if things have changed, but whilst clearing up old warnings I noticed that the above code can be fixed by simply changing local baz="" to local baz

#!/bin/bash

foo () {
   local baz=()
   echo "${baz[@]}"
}

bar () {
   local baz
   echo "$baz"
}

@b0o
Copy link

b0o commented Feb 10, 2020

@knirch Thanks for describing that workaround, I updated the wiki to include it: SC2128, SC2178

@ingydotnet
Copy link

Also functions defined using outer parens rather than braces like:

do-it() (
  foo=(xyz)
)

do-also() (
  foo=xyz
)

should not trigger these warnings.

I use parens for defining functions as much as possible as it limits global side-effects.
Vars defined within are essentially "local".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants