Skip to content
Open
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
38 changes: 35 additions & 3 deletions src/cmds/system/grep_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -500,8 +500,9 @@ fn parse_match_line(line: &str) -> Option<(String, usize, &str)> {

fn has_format_flag<T: AsRef<str>>(extra_args: &[T]) -> bool {
extra_args.iter().any(|arg| {
matches!(
arg.as_ref(),
let s = arg.as_ref();
if matches!(
s,
"-c" | "--count"
| "--count-matches"
| "-l"
Expand All @@ -515,7 +516,20 @@ fn has_format_flag<T: AsRef<str>>(extra_args: &[T]) -> bool {
| "--json"
| "--passthru"
| "--files"
)
) {
return true;
}
// extract_pattern_path merges boolean short flags into one cluster
// token (e.g. `-rln` becomes `-ln` after stripping `r`), so a format
// flag like `-l` can arrive bundled with others rather than as its
// own arg. Clusters only ever contain boolean letters (see
// parse_cluster), so scanning characters here is safe.
if let Some(rest) = s.strip_prefix('-') {
if !rest.is_empty() && !rest.starts_with('-') {
return rest.chars().any(|c| matches!(c, 'c' | 'l' | 'L' | 'o' | 'Z'));
}
}
false
})
}

Expand Down Expand Up @@ -1119,6 +1133,24 @@ mod tests {
assert!(!has_format_flag(&["-i", "-w", "-A", "3"]));
}

#[test]
fn test_format_flag_detects_clustered_l() {
// `-rln` becomes `-ln` after extract_pattern_path strips the `r`.
// -l (files-with-matches) is bundled with -n here, not standalone.
assert!(has_format_flag(&["-ln"]));
}

#[test]
fn test_format_flag_detects_clustered_c() {
assert!(has_format_flag(&["-ic"]));
}

#[test]
fn test_format_flag_ignores_double_dash() {
// Long flags must not be mistaken for a short cluster.
assert!(!has_format_flag(&["--ignore-case"]));
}

// Verify line numbers are always enabled in rg invocation (grep_cmd.rs:24).
// The -n/--line-numbers clap flag in main.rs is a no-op accepted for compat.
#[test]
Expand Down