Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -445,8 +445,15 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
}

// An empty pattern matches every line; with `-v`, GNU grep selects no lines
// and exits as "no match" without reading any input files.
Comment on lines 447 to -448

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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!

if invert_match && patterns.iter().any(|pattern| pattern.is_empty()) {
// and exits as "no match" without reading any input files. This does not
// hold under `-x` or `-w`, where an empty pattern matches only an empty
// line or an empty match at a word boundary, so the inversion still selects
// the remaining lines and the input has to be read.
if invert_match
&& !word_regexp
&& !line_regexp
&& patterns.iter().any(|pattern| pattern.is_empty())
{
return Err(ExitCode::new(1));
}

Expand Down
24 changes: 24 additions & 0 deletions tests/test_grep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,30 @@ fn empty_pattern_matches_every_line() {
.stdout_only("a\nb\nc\n");
}

#[test]
fn inverted_empty_pattern_does_not_short_circuit_under_x_or_w() {
// Under `-x` the empty pattern matches only an empty line, and under `-w`
// only an empty match at a word boundary, so `-v` still selects the rest.
let (_s, mut c) = ucmd();
c.args(&["-e", "", "-x", "-v"])
.pipe_in("abc\ndef\n")
.succeeds()
.stdout_only("abc\ndef\n");

let (_s, mut c) = ucmd();
c.args(&["-e", "", "-w", "-v"])
.pipe_in("abc\ndef\n")
.succeeds()
.stdout_only("abc\ndef\n");

// `-x` still drops the empty line itself.
let (_s, mut c) = ucmd();
c.args(&["-e", "", "-x", "-v"])
.pipe_in("abc\n\ndef\n")
.succeeds()
.stdout_only("abc\ndef\n");
}

#[test]
fn inverted_empty_pattern_short_circuits() {
// grep short-circuits without reading stdin at all, so the harness's write
Expand Down
Loading