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

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

Open
3 of 4 tasks
kkmuffme opened this issue Nov 11, 2024 · 5 comments
Open
3 of 4 tasks

Comments

@kkmuffme
Copy link

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
Copy link

wileyhy commented Nov 15, 2024 via email

@kkmuffme
Copy link
Author

availability of nounset is already required?

What do you mean?

The problem is that currently there is no way to check a script statically, whether it will fail when "set -u" is added or not.
You'd have to try out all possible scenarios.
In a very simplistic case:

set -u

if [[ "$1" -eq 2 ]]
then
    if [[ "$foo" == "bar" ]]
    then
        echo "hello"
    fi
fi

This would only fail if called with the first arg set to a value of 2.

It would be helpful if shellcheck could report an error for this case just like it normally does with SC2154 already

@wileyhy
Copy link

wileyhy commented Nov 20, 2024 via email

@wileyhy
Copy link

wileyhy commented Nov 20, 2024 via email

@kkmuffme
Copy link
Author

And isn't that why people say 'limit your scripts to 100 lines?'

Even in 100 lines, there's plenty possible cases. Isn't that why shellcheck is used in the first place?

I'm sorry. There might be a solution. Someone else more knowledgeable than me should take it from here.

I think the solution is already implemented, it just needs a minor tweak for this case to work:
if set -u is set in the script or optionally a new shellcheck config flag is set, SC2154 should report an error for variables used in -z and -n (e.g. if [[ -n "$foo ]]) too, except if its used like -z ${foo+x} (or -n or whatever other checks won't trigger errors in "set -u") (where x might be any non-empty string)

Basically the way how variables get marked as defined needs to be slightly changed

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

2 participants