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

SC2178 not respecting local declaration: Variable was used as an array but is now assigned a string #1225

Open
1 task done
baumanj opened this issue May 15, 2018 · 3 comments

Comments

@baumanj
Copy link

baumanj commented May 15, 2018

Despite being declared as local within separate functions, shellcheck complains about a variable being used as both an array and a scalar.

Even if they weren't locally scoped, In this particular case, declaring with -n should obviate this error.

For bugs

  • Rule Id: SC2178
  • My shellcheck version: 0.4.7 on Ubuntu and 0.4.6 on Mac OS
  • 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

f1() {
    local -a var1
    var1[0]=bar
}

f2(){
    # shellcheck disable=2034
    local -n var1=variable_name
}

Here's what shellcheck currently says:

In /tmp/test.sh line 10:
    local -n var1=variable_name
             ^-- SC2178: Variable was used as an array but is now assigned a string.

Here's what I wanted or expected to see:

No error

Workaround

The error disappears if I reorder the references, but I don't think there should be any semantic difference.

#!/bin/bash

f2(){
    local -n var1=variable_name
}

f1() {
    local -a var1
    # shellcheck disable=2034
    var1[0]=bar
}
@ngzhian
Copy link
Contributor

ngzhian commented May 25, 2018

I think this is because the dataflow analysis shellcheck has is not complete. There is an open TODO in the code to make it better.
As for the workaround, reordering works because there isn't a check for assigning an array to string :)

@matthewpersico
Copy link

I have another example if scope bleeding across functions (cut 'n pasted from https://www.shellcheck.net/ instead of starting a new issue):

#!/bin/bash

_ekvstore_nonatomic_set () {
  declare -n ns="$1"
  local key="$2"
  local val="$3"

  ns[$key]=$val
}

_ekvstore_nonatomic_rm () {
  declare -n ns="$1"
  local key="$2"

  unset "ns[$key]"
}
$ shellcheck myscript
 
Line 12:
  declare -n ns="$1"
             ^-- SC2034: ns appears unused. Verify use (or export if used externally).
             ^-- SC2178: Variable was used as an array but is now assigned a string.

AJRepo added a commit to AJRepo/LibVirtKvm-scripts that referenced this issue Aug 21, 2019
SC2178 not respecting local declaration: "Variable was used as
an array but is now assigned a string #1225" See
koalaman/shellcheck#1225
@azizk
Copy link

azizk commented Mar 31, 2022

This is another simple example I came across:

#!/bin/bash

f1() {
  local cmd
  cmd=(echo array)
}

f2() {
  local cmd="echo string"
  echo "$cmd"
}

Errors with shellcheck version 0.8.0:

> shellcheck shellcheck_bug_SC2178.sh

In shellcheck_bug_SC2178.sh line 9:
  local cmd="echo string"
        ^-^ SC2178 (warning): Variable was used as an array but is now assigned a string.

In shellcheck_bug_SC2178.sh line 10:
  echo "$cmd"
        ^--^ SC2128 (warning): Expanding an array without an index only gives the first element.

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

4 participants