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

SC2015 inconsistent behavior #2976

Closed
rozcietrzewiacz opened this issue Apr 26, 2024 · 2 comments
Closed

SC2015 inconsistent behavior #2976

rozcietrzewiacz opened this issue Apr 26, 2024 · 2 comments

Comments

@rozcietrzewiacz
Copy link

For bugs

  • Rule Id (if any, e.g. SC1000): SC2015
  • shellcheck version: online

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

This triggers an error:

#!/bin/bash
[ "$1" ] && [ "$2" ] || { exit 1; }

While this doesn't:

#!/bin/bash
[ "$1" ] && [ "$2" ] || exit 1

Here's what I wanted or expected to see:

Consistency.

@koalaman
Copy link
Owner

koalaman commented May 4, 2024

This inconsistency was intentionally introduced in d603ee1 to reduce the noise of A && B || C when C is a single, allowlisted command (echo, printf, exit, return), since these generally aren't a problem. It does not aim to generally determine whether C is a harmless or side effect free command or structure, as this is an infinite rabbit hole.

Somewhat unrelated, the suggestion shouldn't trigger when B is a test, and this is added in 76ff702.

@koalaman koalaman closed this as completed May 4, 2024
@rozcietrzewiacz
Copy link
Author

rozcietrzewiacz commented May 13, 2024

@koalaman Oh 76ff702 seems definitely related - it actually solves my main issue 🤩 Thanks!

So a little background, in case someone else stumbles upon the same: I got here in the first place because of simple initial variable checks in a script, such as:

#!/bin/sh

#Check env vars
[ "$NECESSARY_ENV_VAR1" ] && [ "$NECESSARY_ENV_VAR2" ] || {
  echo "Missing correct set of environment vars (NECESSARY_ENV_VAR1 and NECESSARY_ENV_VAR2)"
  exit 1
}

...

This used to trigger a SC2015, so I dug into it and noticed that it doesn't trigger with a simpler [ "$NECESSARY_ENV_VAR1" ] && [ "$NECESSARY_ENV_VAR2" ] || exit 1... And this was inconsistent, but I agree it should rather be fixed by focusing on the nature of B in the A && B || C problem.

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