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

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

Open
vapier opened this issue Aug 1, 2018 · 3 comments
Open

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

vapier opened this issue Aug 1, 2018 · 3 comments

Comments

@vapier
Copy link
Contributor

vapier commented Aug 1, 2018

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.

@ngzhian
Copy link
Contributor

ngzhian commented Aug 4, 2018

This is probably because ShellCheck does not have proper control flow, similar to #1225

@kolyshkin
Copy link
Contributor

Similarly, there are cases when SC2128 gets misdetected because a variable comes from a caller's context. For example:

#!/bin/bash

x() {
	# Here we have args that came from y, i.e. args is an array.
	# This is clearly SC2128 ("Expanding an array without an index ..."),
	# but it is not detected.
	echo x: "$args"

	# Redeclare args as a string, shadowing the parent one.
	local args="three four"
	echo x: "$args"
}

y() {
	# Here we have args that came from global context, i.e. args is an array.
        # This is clearly SC2128 ("Expanding an array without an index ..."),
	# but it is not detected.
	echo y: "$args"

	# Redeclare args.
	local args=(one two)
	echo y: "${args[@]}"
	x
	echo y: "${args[@]}"
}

args=(global args)
y

@MatthijsBurgh
Copy link

Even with all local declarations on a separate line, I still get this issue when running checking multiple files at once. I only get the SC2128 flag. The SC2127 did show up when declaring and assigning on the same line, but that one disappeared. (GH actions run: https://github.com/tue-robotics/tue-env/actions/runs/3648670225/jobs/6162361889)

MatthijsBurgh added a commit to tue-robotics/tue-env that referenced this issue Dec 8, 2022
MatthijsBurgh added a commit to tue-robotics/tue-env that referenced this issue Dec 9, 2022
* Remove pip completion of pip2

* Apply Shellcheck 0.8.0

* (tue-env) use more local vars

* Bump version to 1.3.9

* (tue-data) use more local

* (tue-env-config) use more local

* (tue-env-config) declare local vars on sep line

* (tue-env-config) move main code to func to use local vars

* (tue-data) declare local vars on sep line

* (tue-env) declare local vars on sep line

* (tue-functions) declare local vars on sep line

* (tue-get) declare local vars on sep line

* (tue-functions) use 'local -a' for arrays

* Rename array to prevent shellcheck bug

koalaman/shellcheck#1309
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

4 participants