grep: don't short-circuit -v empty pattern under -x/-w - #107
Open
MsfPablo wants to merge 1 commit into
Open
Conversation
lhecker
reviewed
Aug 18, 2026
Comment on lines
447
to
-448
| // An empty pattern matches every line; with `-v`, GNU grep selects no lines | ||
| // and exits as "no match" without reading any input files. |
Collaborator
There was a problem hiding this comment.
Please revert this comment. IMO the two-line comment is sufficient to explain why the early-return is necessary. The new comment is redundant with the code which already states the logic via && !word_regexp && !line_regexp.
Thanks!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #65.
The
-v+ empty-pattern fast path returned "no lines selected, exit 1" without reading input, on the assumption that an empty pattern matches every line. Under-xthe empty pattern matches only an empty line, and under-wonly an empty match at a word boundary, so the inversion should still select the remaining lines. Gated the short-circuit on neither flag being set.Diffed against GNU grep across
-x/-wcombined with-v,-i,-c,-n,-oand-E. All-xcases now agree, including exit codes, and the plain-vshort-circuit is untouched (its existing test still passes).One divergence remains under
-w, and it is separate from this change.uu_greptreats an empty line as matching-wwith an empty pattern; GNU does not:That reproduces on
mainwith no-vinvolved, so it is a-wmatcher issue rather than a short-circuit one — the consequence is that-w -vstill drops empty lines that GNU keeps. I left it alone to keep this change to what the issue describes; happy to open a separate issue for it, or fold a fix in here if you would rather.Tests cover
-x/-won input without empty lines and-xon input with one.cargo test: 95 passed. clippy-D warningsandcargo fmt --checkclean.