koalaman / shellcheck

ShellCheck, a static analysis tool for shell scripts
https://www.shellcheck.net
GNU General Public License v3.0
36.43k stars 1.78k forks source link

SC2154 should optionally strict check to avoid errors when using "set -u" #3081

Open kkmuffme opened 5 days ago

kkmuffme commented 5 days ago

For bugs

For new checks and feature suggestions

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

#!/bin/bash
set -u
if [[ -n "$foo" ]]
then
    echo "$foo"
fi

Here's what shellcheck currently says:

No error

Here's what I wanted or expected to see:

foo is referenced but not assigned with code SC2154

Since this is an error.

Correct/no error code e.g.

#!/bin/bash
set -u
if [[ -n "${foo+x}" ]] && [[ -n "$foo" ]]
then
    echo "$foo"
fi

While usually this is pointless, in some cases the "set -u" is outside of your control (= project requirements, set by a parent file that calls your script,...) and of course these issues always happen when you're on vacation :-) It would be helpful if there were a strict/pedantic mode for SC2154/check-unassigned-uppercase to check them as if "set -u" were set and report errors accordingly

wileyhy commented 1 day ago

set -u is such a test, and it's specified by POSIX. Why risk getting it wrong while reimplementing it when the availability of nounset is already required?

Wiley

~ $ head -v test.sh ==> test.sh <==

!/bin/bash -x

set -u if [[ -n ${foo} ]] then echo "$foo" fi ~ $ ./test.sh

On Sun, Nov 10, 2024, 20:23 kkmuffme @.***> wrote:

For bugs

For new checks and feature suggestions

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

!/bin/bashset -uif [[ -n "$foo" ]]then

echo "$foo"fi

Here's what shellcheck currently says:

No error Here's what I wanted or expected to see:

foo is referenced but not assigned with code SC2154

Since this is an error.

Correct/no error code e.g.

!/bin/bashset -uif [[ -n "${foo+x}" ]] && [[ -n "$foo" ]]then

echo "$foo"fi

While usually this is pointless, in some cases the "set -u" is outside of your control (= project requirements, set by a parent file that calls your script,...) and of course these issues always happen when you're on vacation :-) It would be helpful if there were a strict/pedantic mode for SC2154/check-unassigned-uppercase to check them as if "set -u" were set and report errors accordingly

— Reply to this email directly, view it on GitHub https://github.com/koalaman/shellcheck/issues/3081, or unsubscribe https://github.com/notifications/unsubscribe-auth/AUF2F25WKTVCDUO3PCLQQFD2AAWK3AVCNFSM6AAAAABRQ7NRESVHI2DSMVQWIX3LMV43ASLTON2WKOZSGY2DQMBZGUZDENI . You are receiving this because you are subscribed to this thread.Message ID: @.***>