shellcheck icon indicating copy to clipboard operation
shellcheck copied to clipboard

SC2317 false positive warning

Open orbea opened this issue 2 years ago • 10 comments

For bugs

  • Rule Id (if any, e.g. SC1000): SC2317, SC2317
  • My shellcheck version (shellcheck --version or 'online'): online
  • [x] I tried on shellcheck.net and verified that this is still a problem on the latest commit
  • [ ] It's not reproducible on shellcheck.net, but I think that's because it's an OS, configuration or encoding issue

For new checks and feature suggestions

  • [x] shellcheck.net (i.e. the latest commit) currently gives no useful warnings about this
  • [x] I searched through https://github.com/koalaman/shellcheck/issues and didn't find anything related

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

#!/bin/sh

set -e

quit () { err=$?; bar; }

trap 'quit; trap - EXIT; exit $err' EXIT INT

foo

exit 0

Here's what shellcheck currently says:

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

Here's what I wanted or expected to see:

This should not be a warning, the exit 0 at the end of a script is very common and the quit function is indeed reachable due to the trap. In this case the exit 0 may never be reached if the foo command fails for whatever reason and even if the exit 0 is reached the quit function which will have any cleanup is still activated due to the trap.

orbea avatar Jul 23 '22 16:07 orbea