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
// SC2133: Incomplete arithmetic expressions
//
// Arithmetic expansions $(( )) should contain complete expressions.
// Operators must have both operands.
//
// NOTE: In bash arithmetic context, variables can be used with or without $:
// Both $((foo)) and $(($foo)) are valid and equivalent.
//
// Examples:
// Bad:
// result=$((5 +)) // Incomplete expression - missing second operand
// value=$((x * )) // Incomplete expression - missing second operand
//
// Good:
// echo $((foo)) // Variable without $ - VALID in arithmetic
// echo $(($foo)) // Variable with $ - also VALID
// result=$((5 + 3)) // Complete expression
// value=$((x + y)) // Proper operator (both forms valid)
//
// Impact: Incomplete expressions cause syntax errors
use crate::linter::{Diagnostic, LintResult, Severity, Span};
use regex::Regex;
static ARITH_EXPR: std::sync::LazyLock<Regex> = std::sync::LazyLock::new(|| {
// Match: $(( ... )) arithmetic expressions
Regex::new(r"\$\(\(([^)]+)\)\)").unwrap()
});
static VAR_NAME: std::sync::LazyLock<Regex> = std::sync::LazyLock::new(|| {
// Match: word boundaries (variable names)
Regex::new(r"\b([a-zA-Z_][a-zA-Z0-9_]*)\b").unwrap()
});
static INCOMPLETE_ARITH: std::sync::LazyLock<Regex> = std::sync::LazyLock::new(|| {
// Match: $(( expr operator )) with no second operand
Regex::new(r"\$\(\([^)]*[+\-*/]\s*\)\)").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;
}
// NOTE: Variables in arithmetic context can be used with or without $
// Both $((foo)) and $(($foo)) are valid bash syntax
// We ONLY check for incomplete expressions (operators without operands)
// Check for incomplete arithmetic expressions
for mat in INCOMPLETE_ARITH.find_iter(line) {
let expr = mat.as_str();
// Check if operator is at the end (incomplete)
if expr
.trim_end_matches(')')
.trim()
.ends_with(['+', '-', '*', '/'])
{
let start_col = mat.start() + 1;
let end_col = mat.end() + 1;
let diagnostic = Diagnostic::new(
"SC2133",
Severity::Error,
"Incomplete arithmetic expression - missing operand after operator".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_sc2133_variable_without_dollar_ok() {
// Variables without $ are VALID in arithmetic context
let code = "echo $((foo))";
let result = check(code);
assert_eq!(result.diagnostics.len(), 0);
}
#[test]
fn test_sc2133_variable_with_dollar_ok() {
// Variables with $ are also VALID in arithmetic context
let code = "echo $(($foo))";
let result = check(code);
assert_eq!(result.diagnostics.len(), 0);
}
#[test]
fn test_sc2133_incomplete_addition() {
let code = "result=$((5 +))";
let result = check(code);
assert_eq!(result.diagnostics.len(), 1);
assert!(result.diagnostics[0].message.contains("Incomplete"));
}
#[test]
fn test_sc2133_complete_expression_ok() {
let code = "result=$((5 + 3))";
let result = check(code);
assert_eq!(result.diagnostics.len(), 0);
}
#[test]
fn test_sc2133_number_literal_ok() {
let code = "echo $((42))";
let result = check(code);
// Numbers without $ are OK
assert_eq!(result.diagnostics.len(), 0);
}
#[test]
fn test_sc2133_comment_ok() {
let code = "# echo $((foo))";
let result = check(code);
assert_eq!(result.diagnostics.len(), 0);
}
#[test]
fn test_sc2133_multiple_variables_ok() {
// Variables in arithmetic don't need $
let code = "value=$((x + y))";
let result = check(code);
assert_eq!(result.diagnostics.len(), 0);
}
#[test]
fn test_sc2133_incomplete_subtraction() {
let code = "val=$((10 -))";
let result = check(code);
assert_eq!(result.diagnostics.len(), 1);
}
#[test]
fn test_sc2133_mixed_ok() {
// Both forms are OK in arithmetic
let code = "result=$(($a + b))";
let result = check(code);
assert_eq!(result.diagnostics.len(), 0);
}
#[test]
fn test_sc2133_multiline() {
let code = r#"
x=$((foo))
y=$((5 +))
"#;
let result = check(code);
// Only the incomplete expression should be flagged
assert_eq!(result.diagnostics.len(), 1);
assert!(result.diagnostics[0].message.contains("Incomplete"));
}
}