koalaman / shellcheck

ShellCheck, a static analysis tool for shell scripts

Home Page:https://www.shellcheck.net

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

SC2015 inconsistent behavior

rozcietrzewiacz opened this issue · comments

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.

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 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.