use crate::linter::{Diagnostic, LintResult, Severity, Span};
use regex::Regex;
static GREP_UNQUOTED: std::sync::LazyLock<Regex> = std::sync::LazyLock::new(|| {
Regex::new(r"\bgrep(?:\s+-\S+)*\s+\S*[\*\?\[]").unwrap()
});
fn is_ssh_remote_command(line: &str) -> bool {
let trimmed = line.trim();
if let Some(ssh_pos) = trimmed.find("ssh ") {
let after_ssh = &trimmed[ssh_pos..];
if let Some(quote_pos) = after_ssh.find('"') {
if let Some(grep_pos) = after_ssh.find("grep") {
if grep_pos > quote_pos {
return true;
}
}
}
}
false
}
pub fn check(source: &str) -> LintResult {
let mut result = LintResult::new();
for (line_num, line) in source.lines().enumerate() {
let line_num = line_num + 1;
if line.trim_start().starts_with('#') {
continue;
}
if line.contains("grep '") || line.contains("grep \"") {
continue;
}
if is_ssh_remote_command(line) {
continue;
}
if let Some(mat) = GREP_UNQUOTED.find(line) {
let start_col = mat.start() + 1;
let end_col = mat.end() + 1;
let diagnostic = Diagnostic::new(
"SC2062",
Severity::Warning,
"Quote the grep pattern so the shell won't interpret it".to_string(),
Span::new(line_num, start_col, line_num, end_col),
);
result.add(diagnostic);
}
}
result
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_sc2062_unquoted_glob() {
let code = r#"grep *.txt file"#;
let result = check(code);
assert_eq!(result.diagnostics.len(), 1);
}
#[test]
fn test_sc2062_bracket_expression() {
let code = r#"grep [0-9]+ data"#;
let result = check(code);
assert_eq!(result.diagnostics.len(), 1);
}
#[test]
fn test_sc2062_question_mark() {
let code = r#"grep file?.txt data"#;
let result = check(code);
assert_eq!(result.diagnostics.len(), 1);
}
#[test]
fn test_sc2062_quoted_single_ok() {
let code = r#"grep '*.txt' file"#;
let result = check(code);
assert_eq!(result.diagnostics.len(), 0);
}
#[test]
fn test_sc2062_quoted_double_ok() {
let code = r#"grep "*.txt" file"#;
let result = check(code);
assert_eq!(result.diagnostics.len(), 0);
}
#[test]
fn test_sc2062_no_special_chars_ok() {
let code = r#"grep pattern file"#;
let result = check(code);
assert_eq!(result.diagnostics.len(), 0);
}
#[test]
fn test_sc2062_comment_ok() {
let code = r#"# grep *.txt file"#;
let result = check(code);
assert_eq!(result.diagnostics.len(), 0);
}
#[test]
fn test_sc2062_with_flags() {
let code = r#"grep -r *.log ."#;
let result = check(code);
assert_eq!(result.diagnostics.len(), 1); }
#[test]
fn test_sc2062_pipe() {
let code = r#"cat file | grep [ERROR]"#;
let result = check(code);
assert_eq!(result.diagnostics.len(), 1);
}
#[test]
fn test_sc2062_variable_ok() {
let code = r#"grep "$pattern" file"#;
let result = check(code);
assert_eq!(result.diagnostics.len(), 0);
}
#[test]
fn test_issue_125_ssh_remote_grep_not_flagged() {
let code = r#"ssh user@host "grep -E '^(Mem|Swap)' /proc/meminfo""#;
let result = check(code);
assert_eq!(
result.diagnostics.len(),
0,
"SC2062 must NOT flag grep inside SSH remote command string (Issue #125)"
);
}
#[test]
fn test_issue_125_ssh_with_pipe_not_flagged() {
let code = r#"ssh user@host "df -h | grep -E '^/dev/(nvme|mmcblk)'""#;
let result = check(code);
assert_eq!(
result.diagnostics.len(),
0,
"SC2062 must NOT flag grep with pipe inside SSH remote command (Issue #125)"
);
}
#[test]
fn test_issue_125_ssh_simple_pattern() {
let code = r#"ssh server "grep [0-9]+ file""#;
let result = check(code);
assert_eq!(
result.diagnostics.len(),
0,
"SC2062 must NOT flag grep pattern inside SSH command"
);
}
#[test]
fn test_issue_125_local_grep_still_flagged() {
let code = r#"grep -E '^[0-9]+' file"#;
let result = check(code);
assert_eq!(
result.diagnostics.len(),
1,
"Local grep with glob-like patterns SHOULD still be flagged"
);
}
}