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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// SC2016: Expressions don't expand in single quotes, use double quotes for that
//
// Single quotes preserve literal strings - variables and command substitutions
// don't expand inside them. Use double quotes if you want expansion.
//
// Examples:
// Bad:
// echo 'Hello $USER' // Prints literal "$USER"
// msg='Value: $value' // $value doesn't expand
// cmd='$(date)' // Command doesn't run
//
// Good:
// echo "Hello $USER" // Variable expands
// msg="Value: $value" // Variable expands
// cmd="$(date)" // Command runs
// literal='$50 per item' // OK - intentional literal
//
// Note: This rule detects likely mistakes where users expect expansion
// but use single quotes. Intentional literals with $ are acceptable.
use crate::linter::{Diagnostic, LintResult, Severity, Span};
use regex::Regex;
#[allow(clippy::unwrap_used)] // Compile-time regex, panic on invalid pattern is acceptable
static SINGLE_QUOTE_WITH_VAR: std::sync::LazyLock<Regex> = std::sync::LazyLock::new(|| {
// Match: '...$var...' or '...${var}...' or '...$(cmd)...'
Regex::new(r"'[^']*(\$[a-zA-Z_][a-zA-Z0-9_]*|\$\{[^}]+\}|\$\([^)]+\))[^']*'").unwrap()
});
/// F025: Check if the variable in single quotes is wrapped in double quotes
/// Pattern: '"$var"' or '${var}' inside quotes means it's likely documentation/example
fn is_documentation_pattern(matched: &str) -> bool {
// Check if the variable is wrapped in double quotes (e.g., '"$var"')
// This is a common pattern for showing shell syntax as literal text
if matched.contains("\"$") || matched.contains("\"${") || matched.contains("\"$(") {
return true;
}
// Check for JSON-like patterns (e.g., '{"key": "$val"}')
if matched.contains('{') && matched.contains(':') && matched.contains("\"$") {
return true;
}
false
}
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;
}
// Look for single-quoted strings with $ expressions
for m in SINGLE_QUOTE_WITH_VAR.find_iter(line) {
let matched = m.as_str();
let start_col = m.start() + 1;
let end_col = m.end() + 1;
// Skip some common false positives
// Skip if it's clearly a price/money (like '$50')
if matched.contains("$0") || matched.contains("$1") && matched.len() < 10 {
continue;
}
// F025: Skip if variable is in double quotes (documentation pattern)
// e.g., 'Value: "$var"' or 'Use "$(cmd)"' are intentional literals
if is_documentation_pattern(matched) {
continue;
}
let diagnostic = Diagnostic::new(
"SC2016",
Severity::Info,
"Expressions don't expand in single quotes, use double quotes for that".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_sc2016_var_in_single_quotes() {
let code = r#"echo 'Hello $USER'"#;
let result = check(code);
assert_eq!(result.diagnostics.len(), 1);
assert_eq!(result.diagnostics[0].code, "SC2016");
assert_eq!(result.diagnostics[0].severity, Severity::Info);
assert!(result.diagnostics[0].message.contains("single quotes"));
}
#[test]
fn test_sc2016_var_with_braces() {
let code = r#"msg='Value: ${value}'"#;
let result = check(code);
assert_eq!(result.diagnostics.len(), 1);
}
#[test]
fn test_sc2016_command_substitution() {
let code = r#"cmd='$(date)'"#;
let result = check(code);
assert_eq!(result.diagnostics.len(), 1);
}
#[test]
fn test_sc2016_multiple_vars() {
let code = r#"text='$name is $age years old'"#;
let result = check(code);
assert_eq!(result.diagnostics.len(), 1);
}
#[test]
fn test_sc2016_double_quotes_ok() {
let code = r#"echo "Hello $USER""#;
let result = check(code);
assert_eq!(result.diagnostics.len(), 0);
}
#[test]
fn test_sc2016_literal_dollar_ok() {
let code = r#"price='$50 per item'"#;
let result = check(code);
// Price pattern, likely intentional
assert_eq!(result.diagnostics.len(), 0);
}
#[test]
fn test_sc2016_no_dollar_ok() {
let code = r#"msg='Hello World'"#;
let result = check(code);
assert_eq!(result.diagnostics.len(), 0);
}
#[test]
fn test_sc2016_escaped_dollar_ok() {
let code = r#"echo "\$USER""#;
let result = check(code);
// Not in single quotes
assert_eq!(result.diagnostics.len(), 0);
}
#[test]
fn test_sc2016_multiple_issues() {
let code = r#"
msg1='Hello $USER'
msg2='Today is $(date)'
"#;
let result = check(code);
assert_eq!(result.diagnostics.len(), 2);
}
#[test]
fn test_sc2016_in_assignment() {
let code = r#"VAR='Current path: $PWD'"#;
let result = check(code);
assert_eq!(result.diagnostics.len(), 1);
}
// ===== F025: Literal quotes in single-quoted strings =====
// When single-quoted string contains quoted variables like "$var",
// it's likely documentation or intentional literal output
#[test]
fn test_FP_025_literal_quoted_var_not_flagged() {
// User wants to output literal '"$var"' - documentation pattern
let code = r#"echo 'Value: "$var"'"#;
let result = check(code);
assert_eq!(
result.diagnostics.len(),
0,
"SC2016 must NOT flag quotes inside literal (documentation pattern)"
);
}
#[test]
fn test_FP_025_syntax_example_not_flagged() {
// Common pattern: showing shell syntax as literal text
let code = r#"echo 'The syntax is: "$variable"'"#;
let result = check(code);
assert_eq!(
result.diagnostics.len(),
0,
"SC2016 must NOT flag syntax examples in single quotes"
);
}
#[test]
fn test_FP_025_json_pattern_not_flagged() {
// JSON-like patterns with quoted variables
let code = r#"echo '{"name": "$name"}'"#;
let result = check(code);
assert_eq!(
result.diagnostics.len(),
0,
"SC2016 must NOT flag JSON-like patterns"
);
}
#[test]
fn test_FP_025_bare_var_still_flagged() {
// Bare variable without quotes should still be flagged (likely mistake)
let code = r#"echo 'Value: $var'"#;
let result = check(code);
assert_eq!(
result.diagnostics.len(),
1,
"SC2016 SHOULD flag bare variable in single quotes"
);
}
#[test]
fn test_FP_025_command_subst_in_quotes_not_flagged() {
// Documentation showing command substitution syntax
let code = r#"echo 'Use "$(command)" for substitution'"#;
let result = check(code);
assert_eq!(
result.diagnostics.len(),
0,
"SC2016 must NOT flag quoted command substitution in docs"
);
}
}