use crate::linter::{Diagnostic, LintResult, Severity, Span};
use regex::Regex;
static TRAP_THEN_SUBSHELL: std::sync::LazyLock<Regex> =
std::sync::LazyLock::new(|| Regex::new(r"\btrap\b.*\n.*\(").unwrap());
pub fn check(source: &str) -> LintResult {
let mut result = LintResult::new();
let has_trap = source.contains("trap ");
let has_subshell = source.contains("( ") || source.contains("(\n");
if has_trap && has_subshell {
for (line_num, line) in source.lines().enumerate() {
let line_num = line_num + 1;
if line.trim_start().starts_with('#') {
continue;
}
if (line.contains("( ") || line.trim().starts_with('('))
&& !line.contains("$(") && !line.contains("()")
{
let start_col = line.find('(').map_or(1, |i| i + 1);
let end_col = start_col + 1;
let diagnostic = Diagnostic::new(
"SC2165",
Severity::Info,
"Subshells don't inherit traps. Use { } or set trap inside subshell"
.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_sc2165_trap_with_subshell() {
let code = r#"
trap "cleanup" EXIT
( command )
"#;
let result = check(code);
assert!(!result.diagnostics.is_empty());
}
#[test]
fn test_sc2165_no_trap_ok() {
let code = "( command )";
let result = check(code);
assert_eq!(result.diagnostics.len(), 0);
}
#[test]
fn test_sc2165_trap_with_braces_ok() {
let code = r#"
trap "cleanup" EXIT
{ command; }
"#;
let result = check(code);
assert_eq!(result.diagnostics.len(), 0);
}
#[test]
fn test_sc2165_trap_in_subshell_ok() {
let code = r#"( trap "cleanup" EXIT; command )"#;
let result = check(code);
let _ = result.diagnostics.len(); }
#[test]
fn test_sc2165_comment_ok() {
let code = "# trap \"cleanup\" EXIT\n# ( command )";
let result = check(code);
assert_eq!(result.diagnostics.len(), 0);
}
#[test]
fn test_sc2165_trap_only_ok() {
let code = r#"trap "cleanup" EXIT"#;
let result = check(code);
assert_eq!(result.diagnostics.len(), 0);
}
#[test]
fn test_sc2165_multiple_subshells() {
let code = "trap 'cleanup' EXIT\n( cmd1 )\n( cmd2 )";
let result = check(code);
assert!(result.diagnostics.len() >= 2);
}
#[test]
fn test_sc2165_function_call_parens_ok() {
let code = "trap 'cleanup' EXIT\nfunc()";
let result = check(code);
assert_eq!(result.diagnostics.len(), 0);
}
#[test]
fn test_sc2165_arithmetic_ok() {
let code = "trap 'cleanup' EXIT\nresult=$(( 1 + 2 ))";
let result = check(code);
assert_eq!(result.diagnostics.len(), 0);
}
#[test]
fn test_sc2165_subshell_grouping() {
let code = "trap 'cleanup' EXIT\n( cd /tmp && ls )";
let result = check(code);
assert!(!result.diagnostics.is_empty());
}
}