use crate::linter::{Diagnostic, LintResult, Severity, Span};
use regex::Regex;
static ARRAY_TO_STRING: std::sync::LazyLock<Regex> = std::sync::LazyLock::new(|| {
Regex::new(r#"([A-Za-z_][A-Za-z0-9_]*)="(\$@|\$\{[A-Za-z_][A-Za-z0-9_]*\[@\]\})""#).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;
}
if line.contains("=(") {
continue;
}
for cap in ARRAY_TO_STRING.captures_iter(line) {
let var_name = cap.get(1).unwrap().as_str();
let array_ref = cap.get(2).unwrap().as_str();
let start_col = cap.get(0).unwrap().start() + 1;
let end_col = cap.get(0).unwrap().end() + 1;
let suggestion = if array_ref == "$@" {
"Use var=(\"$@\") to assign as array, or var=\"$*\" to concatenate"
} else {
"Use var=(\"${array[@]}\") to assign as array, or var=\"${array[*]}\" to concatenate"
};
let diagnostic = Diagnostic::new(
"SC2124",
Severity::Warning,
format!(
"Assigning an array to a string! {}. Assignment to {} loses all but first element",
suggestion, var_name
),
Span::new(line_num, start_col, line_num, end_col),
);
result.add(diagnostic);
}
}
result
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_sc2124_at_to_string() {
let code = r#"var="$@""#;
let result = check(code);
assert_eq!(result.diagnostics.len(), 1);
assert_eq!(result.diagnostics[0].code, "SC2124");
assert_eq!(result.diagnostics[0].severity, Severity::Warning);
assert!(result.diagnostics[0].message.contains("array"));
}
#[test]
fn test_sc2124_array_at_to_string() {
let code = r#"str="${array[@]}""#;
let result = check(code);
assert_eq!(result.diagnostics.len(), 1);
}
#[test]
fn test_sc2124_array_assignment_ok() {
let code = r#"var=("$@")"#;
let result = check(code);
assert_eq!(result.diagnostics.len(), 0);
}
#[test]
fn test_sc2124_star_concatenation_ok() {
let code = r#"var="$*""#;
let result = check(code);
assert_eq!(result.diagnostics.len(), 0);
}
#[test]
fn test_sc2124_array_star_ok() {
let code = r#"str="${array[*]}""#;
let result = check(code);
assert_eq!(result.diagnostics.len(), 0);
}
#[test]
fn test_sc2124_array_index_ok() {
let code = r#"val="${array[0]}""#;
let result = check(code);
assert_eq!(result.diagnostics.len(), 0);
}
#[test]
fn test_sc2124_regular_var_ok() {
let code = r#"var="$other""#;
let result = check(code);
assert_eq!(result.diagnostics.len(), 0);
}
#[test]
fn test_sc2124_multiple_assignments() {
let code = r#"
a="$@"
b="${arr[@]}"
"#;
let result = check(code);
assert_eq!(result.diagnostics.len(), 2);
}
#[test]
fn test_sc2124_in_command_ok() {
let code = r#"echo "$@""#;
let result = check(code);
assert_eq!(result.diagnostics.len(), 0);
}
#[test]
fn test_sc2124_array_to_array_ok() {
let code = r#"new_arr=("${old_arr[@]}")"#;
let result = check(code);
assert_eq!(result.diagnostics.len(), 0);
}
}