Skip to content

inconsistent flagging of SC2178/SC2128 based on local usage #1309

Open
@vapier

Description

@vapier

For bugs

  • Rule Id: SC2178/SC2128
  • My shellcheck version: online

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

if we start with this code:

#!/bin/bash
get_vars() {
  local vars=( 1 2 3 )
  echo "${vars[@]}"
}
main() {
  local vars="$(get_vars)"
  echo "${vars}"
}

we get:

$ shellcheck myscript
 
Line 7:
  local vars="$(get_vars)"
        ^-- SC2155: Declare and assign separately to avoid masking return values.
        ^-- SC2178: Variable was used as an array but is now assigned a string.
 
Line 8:
  echo "${vars}"
        ^-- SC2128: Expanding an array without an index only gives the first element.

but if we fix SC2155, all the other warnings disappear:

#!/bin/bash
get_vars() {
  local vars=( 1 2 3 )
  echo "${vars[@]}"
}
main() {
  local vars
  vars="$(get_vars)"
  echo "${vars}"
}
$ shellcheck myscript
No issues detected!

if we remove the local decl, they come back:

#!/bin/bash
get_vars() {
  local vars=( 1 2 3 )
  echo "${vars[@]}"
}
main() {
  vars="$(get_vars)"
  echo "${vars}"
}
$ shellcheck myscript
 
Line 7:
  vars="$(get_vars)"
  ^-- SC2178: Variable was used as an array but is now assigned a string.
 
Line 8:
  echo "${vars}"
        ^-- SC2128: Expanding an array without an index only gives the first element.

this is weird because the scope of var from main to get_vars hasn't changed ... the local var in main still cascades down into get_var.

the SC2178/SC2128 should always be flagged even if the parent func declared it local for its own usage.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions