1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// SC2145: Argument mixin in arrays - $@ inside quotes with other text.
//
// When using $@ inside double quotes alongside other text, the array elements
// join with spaces which is often unintended. Use "$*" for explicit concatenation
// or "$@" alone for separate arguments.
//
// NOTE: Using $* inside quotes IS the correct way to concatenate arguments.
// This rule only warns about $@ inside quotes (which should be $* for concatenation).
//
// Examples:
// Bad:
// echo "Args: $@" // Elements concatenate with spaces (use $*)
// msg="Files: $@" // Array elements join incorrectly
//
// Good:
// echo "Args: $*" // Explicit concatenation - CORRECT
// log_info() { echo "$*"; } // $* for log functions - CORRECT
// printf '%s\n' "$@" // "$@" alone for separate args
// for arg in "$@"; do // Proper iteration
//
// Impact: Incorrect argument handling
use crate::linter::{Diagnostic, LintResult, Severity, Span};
use regex::Regex;
static UNQUOTED_AT_IN_QUOTES: std::sync::LazyLock<Regex> = std::sync::LazyLock::new(|| {
// Match: "...$@..." (unquoted $@ inside double quotes with other content)
// Look for $@ that's NOT immediately preceded by opening quote or space-quote
Regex::new(r#""[^"]*\$@[^"]*""#).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;
}
// Check for $@ in double quotes mixed with other text
// Using $* in this context is CORRECT (concatenation), so we don't warn about it
for mat in UNQUOTED_AT_IN_QUOTES.find_iter(line) {
let matched = mat.as_str();
// Skip if it's properly quoted: "$@" (the entire quoted string is just "$@")
if matched == r#""$@""# {
continue;
}
let start_col = mat.start() + 1;
let end_col = mat.end() + 1;
let diagnostic = Diagnostic::new(
"SC2145",
Severity::Warning,
"Argument mixin: Use \"$*\" for concatenation or \"$@\" as separate arguments"
.to_string(),
Span::new(line_num, start_col, line_num, end_col),
);
result.add(diagnostic);
}
// NOTE: We do NOT warn about $* inside quotes - that IS the correct usage
// for concatenation. The previous implementation was incorrect.
}
result
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_sc2145_unquoted_at_in_quotes() {
let code = r#"echo "Args: $@""#;
let result = check(code);
assert_eq!(result.diagnostics.len(), 1);
assert!(result.diagnostics[0].message.contains("Argument mixin"));
}
#[test]
fn test_sc2145_star_in_quotes_is_correct() {
// $* inside quotes is the CORRECT way to concatenate arguments
// This should NOT trigger a warning (Issue #129)
let code = r#"echo "All: $*""#;
let result = check(code);
assert_eq!(
result.diagnostics.len(),
0,
"$* in quotes is correct for concatenation"
);
}
#[test]
fn test_sc2145_log_function_with_star() {
// Common log function pattern - $* is correct here (Issue #129)
let code = r#"log_info() { echo -e "${GREEN}[INFO]${NC} $*"; }"#;
let result = check(code);
assert_eq!(
result.diagnostics.len(),
0,
"$* in log functions is correct"
);
}
#[test]
fn test_sc2145_quoted_at_ok() {
let code = r#"printf '%s\n' "$@""#;
let result = check(code);
assert_eq!(result.diagnostics.len(), 0);
}
#[test]
fn test_sc2145_quoted_star_ok() {
let code = r#"echo "$*""#;
let result = check(code);
assert_eq!(result.diagnostics.len(), 0);
}
#[test]
fn test_sc2145_unquoted_at_ok() {
let code = r#"for arg in $@; do"#;
let result = check(code);
// Unquoted outside of quotes is a different issue (SC2068)
assert_eq!(result.diagnostics.len(), 0);
}
#[test]
fn test_sc2145_in_assignment() {
let code = r#"msg="Files: $@""#;
let result = check(code);
assert_eq!(result.diagnostics.len(), 1);
}
#[test]
fn test_sc2145_comment_ok() {
let code = r#"# echo "Args: $@""#;
let result = check(code);
assert_eq!(result.diagnostics.len(), 0);
}
#[test]
fn test_sc2145_multiple_at_only() {
// Only $@ in quotes should warn, not $*
let code = r#"
echo "Args: $@"
msg="All: $*"
"#;
let result = check(code);
// Only the $@ line should warn (line 2), not the $* line
assert_eq!(result.diagnostics.len(), 1);
}
#[test]
fn test_sc2145_at_beginning() {
let code = r#"echo "$@ are the args""#;
let result = check(code);
assert_eq!(result.diagnostics.len(), 1);
}
#[test]
fn test_sc2145_at_end() {
let code = r#"echo "Arguments: $@""#;
let result = check(code);
assert_eq!(result.diagnostics.len(), 1);
}
#[test]
fn test_sc2145_star_concatenation_multiple() {
// All of these use $* correctly for concatenation
let code = r#"
log_info() { echo "[INFO] $*"; }
log_warn() { echo "[WARN] $*" >&2; }
log_error() { echo "[ERROR] $*" >&2; }
"#;
let result = check(code);
assert_eq!(
result.diagnostics.len(),
0,
"All $* concatenations should be allowed"
);
}
}