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

false positives SC2154, SC2034 and SC2209 for associative array #2399

Open
4 tasks done
coiby opened this issue Dec 3, 2021 · 2 comments
Open
4 tasks done

false positives SC2154, SC2034 and SC2209 for associative array #2399

coiby opened this issue Dec 3, 2021 · 2 comments

Comments

@coiby
Copy link

coiby commented Dec 3, 2021

For bugs

  • Rule Id (if any, e.g. SC1000): SC2154, SC2034 and SC2209
  • My shellcheck version (shellcheck --version or "online"):
shellcheck --version          
ShellCheck - shell script analysis tool
version: 0.8.0
license: GNU General Public License, version 3
website: https://www.shellcheck.net

For new checks and feature suggestions

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

#!/bin/bash
test_arr()
{
    local -n farr=$1

    farr[c]=cc
    farr[d]=dd
    farr[keb]=dd
    echo "${farr[*]}"
}

declare -A arr

arr[a]=aa
arr[b]=bb
test_arr arr

Here's what shellcheck currently says:


In associate_array.sh line 6:
    farr[c]=cc
         ^-- SC2154 (warning): c is referenced but not assigned.


In associate_array.sh line 7:
    farr[d]=dd
    ^-----^ SC2209 (warning): Use var=$(command) to assign output (or quote to assign string).
         ^-- SC2154 (warning): d is referenced but not assigned.


In associate_array.sh line 8:
    farr[keb]=dd
    ^-------^ SC2209 (warning): Use var=$(command) to assign output (or quote to assign string).
         ^-^ SC2154 (warning): keb is referenced but not assigned.


In associate_array.sh line 16:
arr[ab]=aabb
^-----^ SC2034 (warning): arr appears unused. Verify use (or export if used externally).

For more information:
  https://www.shellcheck.net/wiki/SC2034 -- arr appears unused. Verify use (o...
  https://www.shellcheck.net/wiki/SC2154 -- c is referenced but not assigned.
  https://www.shellcheck.net/wiki/SC2209 -- Use var=$(command) to assign outp...

Here's what I wanted or expected to see:

empty result

@petersenna
Copy link

petersenna commented May 4, 2022

Here is what I have:

    VM_EXTRA_OPTS=('--boot-disk-size=40G')

    VM_SELFLINK=$(gcp_vm create --image "$VMTEMPLATE_GCP_IMAGE_URL" \
                --delete-after "$VM_DELETE_TIMEOUT_MINUTES" \
                --extra-opts VM_EXTRA_OPTS)

And I got an SC2034 warning about VM_EXTRA_OPTS.

@matthewpersico
Copy link

matthewpersico commented May 7, 2022

More examples:

declare -A hash

hash[foo]=1
hash[bar]=2
hash[biz]=3
echo '#' "$(set | grep -E '^hash=')"
# hash=([biz]="3" [bar]="2" [foo]="1" )

unset 'hash[foo]'
echo '#' "$(set | grep -E '^hash=')"
# hash=([biz]="3" [bar]="2" )

declare key='bar'
unset 'hash[$key]'
echo '#' "$(set | grep -E '^hash=')"
# hash=([biz]="3" )

So this code all worked. But shellecheck.net complained:

Line 7:
hash[biz]=3
^-- [SC2034](https://github.com/koalaman/shellcheck/wiki/SC2034) (warning): hash appears unused. Verify use (or export if used externally).
__I think that's wrong; we use it in the unset.__
 
Line 15:
declare key='bar'
        ^-- [SC2034](https://github.com/koalaman/shellcheck/wiki/SC2034) (warning): key appears unused. Verify use (or export if used externally).
__That's definitely wrong; we use it in the unset.__

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

3 participants