shellcheck icon indicating copy to clipboard operation
shellcheck copied to clipboard

SC2034 - False positive when using -v to check with variable

Open bgardner-noggin opened this issue 3 years ago • 0 comments

For bugs

  • SC2034
  • Shellcheck 0.72
  • [X] The rule's wiki page does not already cover this (partially covered, see below)
  • [-] I tried on https://www.shellcheck.net/ and verified that this is still a problem on the latest commit (cannot recreate on shellcheck.net, but changelog for 0.8.0 does not mention SC2034)

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


#!/bin/bash

declare -A FOO

FOO[bar]=1      # <---- Warning here that FOO appears unused

declare -a KEYS
KEYS=(abc bar)

#Uncommenting this and warning above disappears
#if [[ -v "FOO[bar]" ]]; then
#	echo "Referenced"
#fi

for KEY in "${KEYS[@]}"; do
	if [[ -v "FOO[$KEY]" ]]; then
		echo "$KEY Exists in \$FOO"
	else
		echo "$KEY Not exists in \$FOO"
	fi

	#Uncommenting this and warning above disappears
	#echo "${FOO[$KEY]}"
done


Here's what shellcheck currently says:

SC2034

Here's what I wanted or expected to see:

No SC2034 warning

Re wiki page for SC2034

SC2034 mentions that it will not follow indirect references, but note in my example above that

echo ${FOO[$KEY]}

Causes the warning to disappear, but

if [[ -v "FOO[$KEY]" ]];

does not, ie one type of indirect reference does work, but the other does not.

bgardner-noggin avatar Mar 02 '22 03:03 bgardner-noggin