Add SC2337: warn about grep early exit in pipelines under pipefail - #3498
Add SC2337: warn about grep early exit in pipelines under pipefail#3498mclasmeier wants to merge 6 commits into
Conversation
When `set -o pipefail` is active and `grep -q` (or `--quiet`) appears as a non-first command in a pipeline, the upstream command may receive SIGPIPE because grep -q exits immediately upon finding a match. This causes the pipeline to report a non-zero exit status under pipefail. The check covers grep, egrep, and fgrep variants. It does not warn when grep -q reads from a file directly or is the first command in the pipeline. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
e-kwsm
left a comment
There was a problem hiding this comment.
This gives false positive to
grep -e -q # search "-q"Also, there are other cases where SIGPIPE is sent:
grep -m 𝑛 -e 𝑝𝑎𝑡head- sed’s quit e.g.
sed -n -e '/𝑝𝑎𝑡/{p;q;}' - nawk’s exit e.g.
nawk '$0 ~ /𝑝𝑎𝑡/ { print; exit; }'
|
@e-kwsm Hi, thanks for the review. |
|
@e-kwsm I have pushed a commit addressing the false positives by using |
e-kwsm
left a comment
There was a problem hiding this comment.
The following non-POSIX options are not covered:
grep -m 𝑛 -e pat(GNU and BSD)grep -L -e pat(GNU sends SIGPIPE, while BSD does not)
So SC2337 covers grep only, and SC2338? should deal with head? |
|
@e-kwsm Updated to also detect |
|
This will fix #665. |
Adds a new linter rule about using
grep -qin combination withset -o pipefailWhen
set -o pipefailis active andgrep -q(or--quiet) appears as a non-first command in a pipeline, the upstream command may receive SIGPIPE because grep -q exits immediately upon finding a match. This causes the pipeline to report a non-zero exit status under pipefail.The check covers grep, egrep, and fgrep variants. It does not warn when grep -q reads from a file directly or is the first command in the pipeline.
Please note, my Haskell skills have become a bit rusty -- did this together with Claude.
Is there anything missing for this contribution?