shellcheck icon indicating copy to clipboard operation
shellcheck copied to clipboard

SC2034: False Positive: Negated by 'set -a'.

Open 5h1r4s3 opened this issue 2 years ago • 1 comments

For bugs

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

For new checks and feature suggestions

  • [x] https://www.shellcheck.net/ (i.e. the latest commit) currently gives no useful warnings about this
  • [x] I searched through https://github.com/koalaman/shellcheck/issues and didn't find anything related

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


# shellcheck shell=sh
# shellcheck enable=all
## shellcheck disable=SC2034        # False Positive: Negated by set -o allexport
# shellcheck disable=SC3000-SC4000  # Ignore Bashisms

set -a ;
if test -d "${HOME}/.cargo" ;
then
	mkdir --parents "${XDG_DATA_HOME}/cargo" ;
	mv --backup='numbered' --force "${HOME}/.cargo" "${XDG_DATA_HOME}/cargo" ;
fi ;
CARGO_HOME="${XDG_DATA_HOME}/cargo" ;

Here's what shellcheck currently says:


[Line 9:](javascript:setPosition(9, 26))
        mkdir --parents "${XDG_DATA_HOME}/cargo" ;
                         ^-- [SC2154](https://www.shellcheck.net/wiki/SC2154) (warning): XDG_DATA_HOME is referenced but not assigned.
 
[Line 12:](javascript:setPosition(12, 1))
CARGO_HOME="${XDG_DATA_HOME}/cargo" ;
^-- [SC2034](https://www.shellcheck.net/wiki/SC2034) (warning): CARGO_HOME appears unused. Verify use (or export if used externally).

Here's what I wanted or expected to see:

[Line 9:](javascript:setPosition(9, 26))
        mkdir --parents "${XDG_DATA_HOME}/cargo" ;
                         ^-- [SC2154](https://www.shellcheck.net/wiki/SC2154) (warning): XDG_DATA_HOME is referenced but not assigned.

This error can occur if the shebang is any of these: #!/usr/bin/env --split-string sh -a #!/usr/bin/env --split-string sh -o allexport

It can also occur if set -a or set -o allexport are used instead of changing the shebang.

5h1r4s3 avatar Oct 08 '23 16:10 5h1r4s3

You could use default to prevent such a warning.

aeiplatform avatar Oct 22 '23 23:10 aeiplatform