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
Comments
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 |
@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? |
I’ve never heard of it as a best practice it I could see it as one. If you
get the 2154, then you have to decide: global var or real bug. Using the :-
syntax tells you six months from now that you made that decision. Putting
an exclusion directive above it may not mean that. Or, either way, get
into the habit of commenting such constructs with # gv?
On Mon, Aug 26, 2019 at 5:53 PM Ken Williams ***@***.***> wrote:
@leagris <https://github.com/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?
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#1583?email_source=notifications&email_token=AAX365XL76RXCLNR5N2DNGDQGRGEVA5CNFSM4HNTXTEKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD5FZSFI#issuecomment-525048085>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAX365TOWZYCH7JCH3L7B53QGRGEVANCNFSM4HNTXTEA>
.
--
Matthew O. Persico
|
I agree with @matthewpersico that the default empty value makes it explicit. When you review the code 6 months later, you know that Now I also think that shellcheck is a bit too zealously complaining on This is probably a check to move to some strict switch when/if this is implemented in the future like the |
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 |
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
For bugs
Here's a snippet or screenshot that shows the problem:
Here's what shellcheck currently says:
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.The text was updated successfully, but these errors were encountered: