use crate::linter::{Diagnostic, LintResult, Severity, Span};
use regex::Regex;
static ARRAY_RECONSTRUCTION: std::sync::LazyLock<Regex> = std::sync::LazyLock::new(|| {
Regex::new(r#"(\w+)=\(\s*"\$\{(\w+)\[@\]\}""#).unwrap()
});
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;
}
for cap in ARRAY_RECONSTRUCTION.captures_iter(line) {
let var1 = cap.get(1).map_or("", |m| m.as_str());
let var2 = cap.get(2).map_or("", |m| m.as_str());
if var1 == var2 {
let mat = cap.get(0).unwrap();
let start_col = mat.start() + 1;
let end_col = mat.end() + 1;
let diagnostic = Diagnostic::new(
"SC2179",
Severity::Info,
r#"Use array+=("item") to append, not array=("${array[@]}" "item")"#
.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_sc2179_array_reconstruction() {
let code = r#"array=("${array[@]}" "new")"#;
let result = check(code);
assert_eq!(result.diagnostics.len(), 1);
}
#[test]
fn test_sc2179_array_append_ok() {
let code = r#"array+=("new")"#;
let result = check(code);
assert_eq!(result.diagnostics.len(), 0);
}
#[test]
fn test_sc2179_array_init_ok() {
let code = r#"array=("item1" "item2")"#;
let result = check(code);
assert_eq!(result.diagnostics.len(), 0);
}
#[test]
fn test_sc2179_comment_ok() {
let code = r#"# array=("${array[@]}" "new")"#;
let result = check(code);
assert_eq!(result.diagnostics.len(), 0);
}
#[test]
fn test_sc2179_different_array_ok() {
let code = r#"arr2=("${arr1[@]}" "new")"#;
let result = check(code);
assert_eq!(result.diagnostics.len(), 0);
}
#[test]
fn test_sc2179_multiple_items() {
let code = r#"array=("${array[@]}" "item1" "item2")"#;
let result = check(code);
assert_eq!(result.diagnostics.len(), 1);
}
#[test]
fn test_sc2179_append_multiple_ok() {
let code = r#"array+=("item1" "item2")"#;
let result = check(code);
assert_eq!(result.diagnostics.len(), 0);
}
#[test]
fn test_sc2179_multiple() {
let code = r#"
arr=("${arr[@]}" "a")
arr=("${arr[@]}" "b")
"#;
let result = check(code);
assert_eq!(result.diagnostics.len(), 2);
}
#[test]
fn test_sc2179_var_reconstruction() {
let code = r#"files=("${files[@]}" "$newfile")"#;
let result = check(code);
assert_eq!(result.diagnostics.len(), 1);
}
#[test]
fn test_sc2179_star_syntax() {
let code = r#"array=("${array[*]}" "new")"#;
let result = check(code);
assert_eq!(result.diagnostics.len(), 0);
}
}