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

SC2181 misleading for errexit #2944

Open
2 tasks done
JohannesLorenz opened this issue Mar 13, 2024 · 0 comments
Open
2 tasks done

SC2181 misleading for errexit #2944

JohannesLorenz opened this issue Mar 13, 2024 · 0 comments

Comments

@JohannesLorenz
Copy link

JohannesLorenz commented Mar 13, 2024

For bugs

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

#!/bin/bash

funcname()
{
        false
}

set -e
funcname
[ "$?" == 0 ] || echo "no"

Here's what shellcheck currently says:

[Line 10:](javascript:setPosition(10, 3))
[ "$?" == 0 ] || echo "no"
  ^-- [SC2181](https://www.shellcheck.net/wiki/SC2181) (style): Check exit code directly with e.g. 'if mycmd;', not indirectly with $?.

Here's what I wanted or expected to see:

No warning or no misleading warning, and an exception on the SC2181 page.

Rationale: The warnings suggests ("Check exit code directly with e.g. 'if mycmd;'") to write code like this:

#!/bin/bash

funcname()
{
        false
}

set -e
if funcname; then
        echo yes
else
        echo no
fi

This code passes, but is dangerous: errexit is now inactive in funcname and all errors in funcname or functions called by it will silently ignore errexit. So, after a function, doing something like rval=$? and then testing $rval seems the most clean to me (before funcname calls return, you need to set errexit off, and after catching rval, you need to turn it on again).

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

1 participant