shellcheck
shellcheck copied to clipboard
SC2034 doesn't consider namerefs
Shellcheck doesn't account for namerefs properly.
For bugs
- Version: 4.5 and online
- I tried on shellcheck.net and verified that this is still a problem on the latest commit
Here's a snippet or screenshot that shows the problem:
#!/bin/bash
read_bool() {
local -n out="$1"
case "$2" in
true) out=1 ;;
false) out=0 ;;
*) die "\`$2' is neither true nor false"
esac
}
Here's what shellcheck currently says:
false) out=0 ;;
^-- SC2034: out appears unused. Verify it or export it.
Here's what I wanted or expected to see:
Nothing.
Hey @koalaman - might I poke you about this?
Namerefs are an important feature of bash.
No updates, but you're right. Namerefs should be considered auto-used just like exported variables.
Duplicates of this issue: #1544 #1543
More apparent duplicates: #2919
7.5 year old issue now. Constant annoyance in our workflow. SC2034 is very helpful when it works correctly in identifying stray vars that need to be cleaned up, but cannot recognize any kind of nameref usage; not with a declare/local -n, not when adding entries to that ref, and not when using the ref in another function:
myfunc() {
local -n out="${1}" str="${2}"
str="${out[*]}"
}
array=(1 2 3)
declare -n ref="array"
ref+=(4)
myfunc ref string
echo "${string}"