koalaman / shellcheck

ShellCheck, a static analysis tool for shell scripts
https://www.shellcheck.net
GNU General Public License v3.0
36.45k stars 1.78k forks source link

local associative array seen as global #2171

Open wybodekker opened 3 years ago

wybodekker commented 3 years ago

For bugs

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

!/bin/bash

f() { local -A map map[x]=assoc map[y]=array echo "local map: ${map[*]}" }

g() { local -a map map+=(index) map+=(arr) }

f echo "x: ${map[x]} y: ${map[y]}" g echo "global map: ${map[*]}"


#### Here's what shellcheck currently says:

Line 12 SC2190: Elements in associative arrays need index, e.g. array=( [index]=value ) . Line 13 SC2190: Elements in associative arrays need index, e.g. array=( [index]=value ) .


#### Here's what I wanted or expected to see:
it should give error code SC2034 instead: map appears unused. Verify...
pmhahn commented 3 years ago
# shellcheck shell=bash
foo () {
        declare -a cmd=(1 2 3)
        echo "func ${cmd[*]}"
}
cmd="bar"
foo
echo "main ${cmd[*]}"

results in

Line 6: cmd="bar" ^-- SC2178: Variable was used as an array but is now assigned a string.

while cmd is local to the function foo only and does not spill out