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

Don't trigger SC2154 (unassigned var) in -n/-z expressions #1583

Open
2 tasks done
trespasserw opened this issue May 17, 2019 · 5 comments
Open
2 tasks done

Don't trigger SC2154 (unassigned var) in -n/-z expressions #1583

trespasserw opened this issue May 17, 2019 · 5 comments

Comments

@trespasserw
Copy link

For bugs

  • Rule Id: SC2154
  • My shellcheck version: 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

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

#!/bin/sh

if [ -n "$__wtf__" ]; then
  echo "there is some $__wtf__"
fi

Here's what shellcheck currently says:

Line 3:
if [ -n "$__wtf__" ]; then
         ^-- SC2154: __wtf__ is referenced but not assigned.

Here's what I wanted or expected to see:

No warnings.

Rationale:

The -n and -z checks are idiomatic ways of probing a global variable before it's use.

@leagris
Copy link

leagris commented Jul 9, 2019

If you use this, ShellCheck does not complain because it has a default empty value

if [ -n "${__wtf__:-}" ]; then
  echo "there is some $__wtf__"
fi

@kenahoo
Copy link

kenahoo commented Aug 26, 2019

@leagris - would that be considered a workaround for a bug/misfeature in shellcheck, or is your suggestion a "best practice" for how to write these kinds of things in scripts?

@matthewpersico
Copy link

matthewpersico commented Aug 26, 2019 via email

@leagris
Copy link

leagris commented Aug 26, 2019

I agree with @matthewpersico that the default empty value makes it explicit. When you review the code 6 months later, you know that $__wtf__ may come from an external source, and if it is undefined you want to consider it as an empty string.

Now I also think that shellcheck is a bit too zealously complaining on "$__wtf__", because it is also clear this will result in an empty string when variable is not defined.

This is probably a check to move to some strict switch when/if this is implemented in the future like the "\\" backslash escaping in double quote strings.

@kenahoo
Copy link

kenahoo commented Aug 27, 2019

$__wtf__ may come from an external source, and if it is undefined you want to consider it as an empty string.

In most cases I don't actually want to consider it as an empty string, I want to take different action based on the presence or absence of a variable. I'm not sure how defaulting it to an empty string helps either clarity or logic - I feel like it would actually make the reader start wondering "wait, what does the :- logic do again?", and then go down the rabbit hole of https://stackoverflow.com/questions/3601515/how-to-check-if-a-variable-is-set-in-bash again...

Kangie pushed a commit to Kangie/shellcheck that referenced this issue Feb 27, 2023
Merge of upstream through 218deb6

Conflicts:
	src/ShellCheck/Analytics.hs

Manual changes:
        Renumbered tests to avoid conflict with new jq cases.

Changelog:
----------------------------------------------------------------
Aurelio Jargas (1):
      SC1102: Fix typo in error message: substition

Benjamin Gordon (6):
      CHROMIUM: Exclude D/ED from SC2115 in ebuilds
      CHROMIUM: Don't warn about usev splits in ebuilds
      CHROMIUM: Renumber local warnings
      CHROMIUM: Reorganize variable lists
      CHROMIUM: Add stack.yaml.lock to gitignore
      CHROMIUM: Merge 'upstream/master' into chromeos-0.7

Brennan Vincent (1):
      Update README.md

Commit Bot (1):
      Merge "ShellCheck: Change PortageAutoInternalVariables.hs to a map" into chromeos-0.7

Gandalf- (1):
      Issue 1759 mapfile and process substition

Glen Mailer (1):
      Mention the CircleCI shellcheck orb in the README.

Mason Wilde (3):
      CHROMIUM: Update portageInternalVariables in Data.hs
      ShellCheck: Split auto and manual portage var lists
      ShellCheck: Change PortageAutoInternalVariables.hs to a map

Mike Frysinger (1):
      CHROMIUM: add new cros-workon variable

Onno Zweers (1):
      Rephrase: *Shellcheck* can't follow non-constant source

Peter Gromov (1):
      Don't trigger SC2154 (unassigned var) in `-n`/`-z` expressions koalaman#1583

Simon Shine (1):
      Fix whitespace in README.md

Stavros Ntentos (1):
      Autolink https://www.shellcheck.net/

Vidar Holen (36):
      Clarify that SC1090 refers to ShellCheck, not sh
      Warn about extra spaces between ((s in for((;;))
      Merge branch 'patch-1' of https://github.com/onnozweers/shellcheck into onnozweers-patch-1
      Merge branch 'onnozweers-patch-1'
      Merge pull request koalaman#1999 from aureliojargas/patch-1
      Merge pull request koalaman#2002 from stdedos/patch-1
      Treat $x/ or $(x)/ as ./ when finding sourced files (fixes koalaman#1998)
      Use TravisCI workspaces
      Remove trailing whitespace
      Re-enable Windows job
      Warn about non-POSIX case modification expansions (fixes koalaman#1977)
      Improve handling of command prefixes like exec/command (fixes koalaman#2008)
      Handle literal linefeeds in printf format strings (fixes koalaman#2007)
      Handle tilde expansion in pattern matching (fixes koalaman#1769)
      Improve SC1033/SC1034 message
      Warn when shell functions blatantly recurse (fixes koalaman#1994)
      Merge pull request koalaman#2031 from umanwizard/patch-1
      Merge pull request koalaman#2028 from Lin-Buo-Ren/github-issue-1643
      Suppress SC2216 for du --files0-from or --exclude-from (fixes koalaman#1286)
      Merge branch 'patch-1' of https://github.com/glenjamin/shellcheck into glenjamin-patch-1
      Merge branch 'glenjamin-patch-1'
      Merge branch 'issue_1759_mapfile_proc_substition' of https://github.com/Gandalf-/shellcheck into Gandalf--issue_1759_mapfile_proc_substition
      Modernize getting mapfile array name
      Merge branch 'Gandalf--issue_1759_mapfile_proc_substition'
      Merge branch 'supportMinusNZ' of https://github.com/donnerpeter/shellcheck into donnerpeter-supportMinusNZ
      Consider variables in -z/-n tests to be checked
      Merge branch 'donnerpeter-supportMinusNZ'
      Warn when using &/| between test statements
      Upgrade SC2169 (unsupported in dash) from warning to error (fixes koalaman#2013)
      Merge pull request koalaman#2042 from sshine/patch-1
      Parse assignments according to spec (fixes koalaman#2022)
      Suppress SC2035 for echo * and printf * (fixes koalaman#2036)
      Allow specifying ranges in disable directives
      Give each sh/dash compatibility warning its own SC3xxx error code
      Improve compatibility checks
      Update SC2091/SC2092 message and ignore in quotes.

林博仁(Buo-ren, Lin) (1):
      Fix snap distribution unable to process scripts in Unicode(Chinese) (fixes koalaman#1643)

 .compile_binaries                              |   14 +-
 .github/ISSUE_TEMPLATE.md                      |    4 +-
 .gitignore                                     |    1 +
 .prepare_deploy                                |    6 +-
 .travis.yml                                    |   37 +-
 CHANGELOG.md                                   |    9 +
 README.md                                      |    9 +-
 ShellCheck.cabal                               |    1 +
 nextnumber                                     |    2 +-
 shellcheck.1.md                                |    3 +-
 snap/snapcraft.yaml                            |    2 +
 src/ShellCheck/AST.hs                          |    2 +-
 src/ShellCheck/ASTLib.hs                       |  119 ++-
 src/ShellCheck/Analytics.hs                    |  189 +++-
 src/ShellCheck/AnalyzerLib.hs                  |   74 +-
 src/ShellCheck/Checker.hs                      |    8 +-
 src/ShellCheck/Checks/Commands.hs              |   48 +-
 src/ShellCheck/Checks/ShellSupport.hs          |  137 +--
 src/ShellCheck/Data.hs                         |  529 +----------
 src/ShellCheck/Parser.hs                       |  142 ++-
 src/ShellCheck/PortageAutoInternalVariables.hs | 1205 ++++++++++++++++++++++++
 21 files changed, 1770 insertions(+), 771 deletions(-)
 create mode 100644 src/ShellCheck/PortageAutoInternalVariables.hs

BUG=chromium:1131488
TEST=stack test; spot check cros lint

Change-Id: Iad5d2aa67b20bfdfddb0fa30a48865a087ff4695
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