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
// SC2020: tr replaces sets of chars, not words (e.g. 'yes' to 'no')
//
// tr replaces individual characters, not strings. Each character in the first
// set is mapped to the corresponding character in the second set.
//
// Examples:
// Bad:
// tr 'yes' 'no' // Replaces y→n, e→o, s→o (not "yes"→"no")
// tr 'foo' 'bar' // Replaces f→b, o→a, o→r
// tr 'true' 'false' // Doesn't work as expected
//
// Good:
// sed 's/yes/no/g' // Use sed for word replacement
// awk '{gsub(/yes/,"no")}1' // Use awk for word replacement
// tr 'aeiou' 'AEIOU' // OK - char-to-char mapping
//
// Explanation:
// tr 'yes' 'no' means: y→n, e→o, s→o
// So "yes" becomes "noo", not "no"!
//
// Note: Use sed or awk for word/string replacement. tr is for character sets.
use crate::linter::{Diagnostic, LintResult, Severity, Span};
use regex::Regex;
static TR_WORD_PATTERN: std::sync::LazyLock<Regex> = std::sync::LazyLock::new(|| {
// Match: tr 'word' 'word' where both args are multi-char words
// Look for tr with quoted strings that look like words (alphanumeric)
Regex::new(r#"tr\s+['"]([a-z]{2,})['"]"#).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;
}
// Look for tr with word-like patterns
for cap in TR_WORD_PATTERN.captures_iter(line) {
let word = cap.get(1).unwrap().as_str();
// Skip if it looks like a character range or class
if word.contains('-') || word.contains(':') {
continue;
}
// Skip single character or very short patterns
if word.len() < 2 {
continue;
}
// Warn if the word looks like a common word being replaced
// Common patterns: yes/no, true/false, on/off, foo/bar, etc.
let common_words = [
"yes", "no", "true", "false", "on", "off", "foo", "bar", "old", "new",
];
if common_words.contains(&word) {
let start_col = line
.find(&format!("'{}'", word))
.or_else(|| line.find(&format!("\"{}\"", word)))
.map_or(1, |p| p + 1);
let end_col = start_col + word.len() + 2; // +2 for quotes
let diagnostic = Diagnostic::new(
"SC2020",
Severity::Info,
format!(
"tr replaces sets of chars, not words. To replace '{}', use sed or awk instead",
word
),
Span::new(line_num, start_col, line_num, end_col),
);
result.add(diagnostic);
}
}
}
result
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_sc2020_yes_no() {
let code = r#"tr 'yes' 'no'"#;
let result = check(code);
assert_eq!(result.diagnostics.len(), 1);
assert_eq!(result.diagnostics[0].code, "SC2020");
assert_eq!(result.diagnostics[0].severity, Severity::Info);
assert!(result.diagnostics[0].message.contains("sets of chars"));
}
#[test]
fn test_sc2020_true_false() {
let code = r#"tr 'true' 'false'"#;
let result = check(code);
assert_eq!(result.diagnostics.len(), 1);
}
#[test]
fn test_sc2020_foo_bar() {
let code = r#"tr 'foo' 'bar'"#;
let result = check(code);
assert_eq!(result.diagnostics.len(), 1);
}
#[test]
fn test_sc2020_on_off() {
let code = r#"tr 'on' 'off'"#;
let result = check(code);
assert_eq!(result.diagnostics.len(), 1);
}
#[test]
fn test_sc2020_char_set_ok() {
let code = r#"tr 'aeiou' 'AEIOU'"#;
let result = check(code);
// Not a common word pattern
assert_eq!(result.diagnostics.len(), 0);
}
#[test]
fn test_sc2020_range_ok() {
let code = r#"tr 'a-z' 'A-Z'"#;
let result = check(code);
// Contains dash, looks like range
assert_eq!(result.diagnostics.len(), 0);
}
#[test]
fn test_sc2020_posix_class_ok() {
let code = r#"tr '[:lower:]' '[:upper:]'"#;
let result = check(code);
// Contains colon, looks like POSIX class
assert_eq!(result.diagnostics.len(), 0);
}
#[test]
fn test_sc2020_single_char_ok() {
let code = r#"tr 'a' 'b'"#;
let result = check(code);
// Single char is fine
assert_eq!(result.diagnostics.len(), 0);
}
#[test]
fn test_sc2020_multiple_issues() {
let code = r#"
tr 'yes' 'no'
tr 'true' 'false'
"#;
let result = check(code);
assert_eq!(result.diagnostics.len(), 2);
}
#[test]
fn test_sc2020_uncommon_word_ok() {
let code = r#"tr 'xyz' 'abc'"#;
let result = check(code);
// Not in common words list
assert_eq!(result.diagnostics.len(), 0);
}
}