use crate::linter::{Diagnostic, Fix, LintResult, Severity, Span};
use regex::Regex;
static PATTERN: std::sync::LazyLock<Regex> =
std::sync::LazyLock::new(|| Regex::new(r#"\[\s+("\$[A-Za-z_][A-Za-z0-9_]*")\s+\]"#).unwrap());
pub fn check(source: &str) -> LintResult {
let mut result = LintResult::new();
let pattern = &*PATTERN;
for (line_num, line) in source.lines().enumerate() {
let line_num = line_num + 1;
if line.trim_start().starts_with('#') {
continue;
}
if line.contains("[[") {
continue;
}
for cap in pattern.captures_iter(line) {
let full_match = cap.get(0).unwrap();
let var = cap.get(1).unwrap().as_str();
let start_col = full_match.start() + 1; let end_col = full_match.end() + 1;
let fix_text = format!(r"[ -n {} ]", var);
let diagnostic = Diagnostic::new(
"SC2070",
Severity::Info,
"Use -n for explicit non-empty test (or -z for empty test)",
Span::new(line_num, start_col, line_num, end_col),
)
.with_fix(Fix::new(fix_text));
result.add(diagnostic);
}
}
result
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_sc2070_basic_detection() {
let script = r#"if [ "$var" ]; then echo "set"; fi"#;
let result = check(script);
assert_eq!(result.diagnostics.len(), 1);
assert_eq!(result.diagnostics[0].code, "SC2070");
}
#[test]
fn test_sc2070_autofix() {
let script = r#"if [ "$var" ]; then echo "set"; fi"#;
let result = check(script);
assert_eq!(result.diagnostics.len(), 1);
assert!(result.diagnostics[0].fix.is_some());
assert_eq!(
result.diagnostics[0].fix.as_ref().unwrap().replacement,
r#"[ -n "$var" ]"#
);
}
#[test]
fn test_sc2070_braced_variable() {
let script = r#"[ "${myvar}" ]"#;
let result = check(script);
assert!(result.diagnostics.len() <= 1);
}
#[test]
fn test_sc2070_false_positive_with_n_flag() {
let script = r#"if [ -n "$var" ]; then echo "set"; fi"#;
let result = check(script);
assert_eq!(result.diagnostics.len(), 0);
}
#[test]
fn test_sc2070_false_positive_with_z_flag() {
let script = r#"if [ -z "$var" ]; then echo "empty"; fi"#;
let result = check(script);
assert_eq!(result.diagnostics.len(), 0);
}
#[test]
fn test_sc2070_false_positive_comparison() {
let script = r#"if [ "$var" = "value" ]; then echo "yes"; fi"#;
let result = check(script);
assert_eq!(result.diagnostics.len(), 0);
}
#[test]
fn test_sc2070_false_positive_double_bracket() {
let script = r#"if [[ "$var" ]]; then echo "set"; fi"#;
let result = check(script);
assert_eq!(result.diagnostics.len(), 0);
}
#[test]
fn test_sc2070_multiple_occurrences() {
let script = r#"
if [ "$var1" ]; then
echo "var1 set"
fi
if [ "$var2" ]; then
echo "var2 set"
fi
"#;
let result = check(script);
assert_eq!(result.diagnostics.len(), 2);
}
#[test]
fn test_sc2070_with_spaces() {
let script = r#"[ "$var" ]"#;
let result = check(script);
assert_eq!(result.diagnostics.len(), 1);
}
#[test]
fn test_sc2070_false_positive_numeric_test() {
let script = r#"if [ "$num" -gt 5 ]; then echo "big"; fi"#;
let result = check(script);
assert_eq!(result.diagnostics.len(), 0);
}
}