koalaman / shellcheck

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

Ignore SC2317 on: return $STATUS 2>&- || exit $STATUS #2620

Open ale5000-git opened 1 year ago

ale5000-git commented 1 year ago

For bugs

For new checks and feature suggestions

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


#!/usr/bin/env bash
# shellcheck enable=all

STATUS=0
# Some code
return "${STATUS}" 2>&- || exit "${STATUS}"

Here's what shellcheck currently says:

SC2317 (info): Command appears to be unreachable. Check usage (or ignore if invoked indirectly).

Here's what I wanted or expected to see:

No message, since I want to support both being executed and being sourced. return will apply when sourced while exit will apply when executed.

akinomyoga commented 1 year ago

We also hit this false positive in https://github.com/ohmybash/oh-my-bash/pull/402#discussion_r1111634730. The pattern if <some test command>; then return xxx 2>/dev/null || exit xxx; fi is an idiom for the scripts that can be used for both executables and source scripts. Because of this false positive information, users would try to remove || exit xxx in PRs, which would break the script. This is harmful. Could anyone support the detection of this case and suppress the information?