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

local unbound variable not detected #1611

Open
4 tasks done
jaysoffian opened this issue Jun 11, 2019 · 4 comments
Open
4 tasks done

local unbound variable not detected #1611

jaysoffian opened this issue Jun 11, 2019 · 4 comments

Comments

@jaysoffian
Copy link

jaysoffian commented Jun 11, 2019

For bugs

  • Rule Id (if any, e.g. SC1000):
  • My shellcheck version (shellcheck --version or "online"): online and 0.6.0
  • The rule's wiki page does not already cover this (e.g. https://shellcheck.net/wiki/SC2086)
  • I tried on shellcheck.net and verified that this is still a problem on the latest commit

For new checks and feature suggestions

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

#!/bin/bash

set -u

fn1() {
        local foo=foo
        echo "$foo"
}

fn2() {
        echo "$foo"  # foo is undefined here because it is local to fn1 and not called from fn1.
}

fn1
fn2

Here's what shellcheck currently says:

Nothing. :-(

Here's what I wanted or expected to see:

foo is not defined in fn2 because it was declared local in fn1 so ShellCheck should warn about this. I realize scoping is dynamic, not lexical but I think even a lexical analysis with a warning such as:

Line 11:
        echo "$foo"
              ^-- SCXXXX: foo is referenced but may not be assigned.

Maybe related to #1497 and #1225. Possible dupe of #518.

@matthewpersico
Copy link

Huh? You just assigned the string “foo” to the variable $foo in the previous line; what am I missing?

@jaysoffian
Copy link
Author

@matthewpersico I updated the description to be clearer. foo is local to fn1 so ShellCheck should warn about the reference to $foo in fn2.

@matthewpersico
Copy link

Duh. Seems I can’t count to 11. 🤦🏻‍♂️ Thanks.

@SebastianSimon
Copy link

This is the same issue as ShellCheck failing to warn about the unused variable foo (in function y) in this script, isn’t it?

#!/bin/bash

x(){
  local foo='foo'

  echo "${foo}"
}

y(){
  local foo='foo' # This is unused! Why doesn’t ShellCheck warn about it?

  echo "no foo"
}

x
y

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