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

SC2034 doesn't consider local function definitions to define new scopes #2965

Open hseg opened 7 months ago

hseg commented 7 months ago

For bugs

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

#!/bin/bash

ppArrInd() {
    local -n args="$1"
    printf '<%s>\n' "${args[@]}"
}

case "$1" in
    0) main() {
        local param=(foo bar)
        ppArrInd param
    };;
    1) main() {
        local param=(baz quz)
        ppArrInd param
    };;
    *) main() {
        local param=(fizz buzz)
        ppArrInd param
    };;
esac
main

Here's what shellcheck currently says:

SC2034 is triggered unless specifically it is disabled on the last definition of param

Here's what I wanted or expected to see:

Given that realizing that an array is passed by name is out-of-scope for SC2034, at least it should warn at each of the definition lines for param (either since it understands that these are distinct variables, or since "is later redefined" should not count as a usage -- I'd similarly expect x=1; x=2; x=3 to warn that each of these definitions of x are unused)