From 10c76c698fd465076cc02c947fcdc031236e992c Mon Sep 17 00:00:00 2001 From: Michael Diamond Date: Fri, 8 May 2020 14:39:10 -0700 Subject: [PATCH] Updated SC2178 (markdown) --- SC2178.md | 27 +++++++-------------------- 1 file changed, 7 insertions(+), 20 deletions(-) diff --git a/SC2178.md b/SC2178.md index c6749ad..43f87a1 100644 --- a/SC2178.md +++ b/SC2178.md @@ -36,13 +36,14 @@ In the correct code, `"${flags[@]}"` will contain `--dry-run` only. Another possible cause is accidentally missing the `$` on a previous assignment: `var=(my command); var=bar` instead of `var=$(my command); var=bar`. If the variable is not intended to be an array, ensure that it's never assigned as one. -### Exceptions: +### Bugs: -ShellCheck can get confused by variable scope if the same variable name was used as an array previously, but is a string in the current context. You can [[ignore]] it in this case. +There is a [known issue](https://github.com/koalaman/shellcheck/issues/1309) with this check's handling of `local` variables, causing ShellCheck to flag variables that were previously declared as arrays, even if they are in different scopes. -In the case of local variables, a workaround is to declare the local variable separately from assigning to it: +The easiest workaround is to simply use different variable names. Alternatively, you can [[ignore]] the check. + +It is also possible to satisfy ShellCheck by declaring the `local` variable separately from assigning to it, e.g.: -**Problematic Code:** ```sh foo () { local -a baz @@ -51,22 +52,8 @@ foo () { } bar () { - local baz="qux" - echo "$baz" -} -``` - -**Correct Code:** -```sh -foo () { - local -a baz - baz+=("foo" "bar") - echo "${baz[@]}" -} - -bar () { - local baz + local baz # ShellCheck gets confused if these lines are merged as local baz="qux" baz="qux" echo "$baz" } -``` \ No newline at end of file +```