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

SC2115 fires when it is statically verifyable that the variable cannot be empty #2968

Open
2 tasks done
benblank opened this issue Apr 20, 2024 · 2 comments
Open
2 tasks done

Comments

@benblank
Copy link

For bugs

I had assumed this rule would work similarly to SC2086, in that it wouldn't trigger if the variable's declaration were available and could not lead to the described problem occurring. In the example below, STEAMROOT is declared within the script being checked and cannot be empty even if HOME is.

Here's a snippet that shows the problem:

#!/bin/sh

STEAMROOT=$HOME/.local/share/Steam

rm -rf "$STEAMROOT/"*

Here's what shellcheck currently says:

Line 5:
rm -rf "$STEAMROOT/"*
       ^-- SC2115 (warning): Use "${var:?}" to ensure this never expands to /* .

Here's what I expected to see:

No issues detected!
@ale5000-git
Copy link

Personally I think it is always advisable to use "${var:?}".

Put this case:

STEAMROT=$HOME/.local/share/Steam
rm -rf "$STEAMROOT/"*

In this example there is a missing letter in the variable declaration, and this cause the second commands to wipe all your files.

@benblank
Copy link
Author

There's definitely a case to be made for safe habits. SC2086 has a corresponding optional/"strict" check, SC2248. Potentially, an optional version of this check could be useful, as well? I do think it would be nice to take advantage of the extra information by default.

In the end, I certainly don't feel that the current behavior is bad, simply that it could perhaps be refined. This issue is really just the result of having been surprised by SC2115's behavior compared to SC2086. 🙂

As for the typo, that would still trigger SC2034 for the unused variable and SC2153 for the undefined variable, both of which I feel are more relevant to having made a typo than not knowing whether the misspelled variable is empty.

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