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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
//! BASH008: Missing Error Messages Before Exit
//!
//! **Rule**: Detect `exit` without preceding error message
//!
//! **Why this matters**:
//! Silent failures are hard to debug:
//! - Users don't know what went wrong
//! - No context for troubleshooting
//! - Automation pipelines fail mysteriously
//! - Wastes developer time investigating
//!
//! **Examples**:
//!
//! ❌ **BAD** (silent failure):
//! ```bash
//! if [ ! -f "$CONFIG" ]; then
//! exit 1 # Why did it fail? User has no idea!
//! fi
//!
//! command || exit 1 # Silent failure
//! ```
//!
//! ✅ **GOOD** (informative error):
//! ```bash
//! if [ ! -f "$CONFIG" ]; then
//! echo "Error: Config file not found: $CONFIG" >&2
//! exit 1
//! fi
//!
//! command || { echo "Error: Command failed" >&2; exit 1; }
//! ```
//!
//! ## Detection Logic
//!
//! This rule detects:
//! - `exit 1` or `exit <non-zero>` without preceding error message
//! - `exit` without explicit code (defaults to last command's exit code)
//!
//! Does NOT flag:
//! - `exit 0` - success, no error message needed
//! - Lines with both error message and exit: `echo "Error" >&2; exit 1`
//! - Error message on previous line
//!
//! ## Auto-fix
//!
//! Suggests:
//! ```bash
//! echo "Error: [description]" >&2
//! exit 1
//! ```
use crate::linter::LintResult;
use crate::linter::{Diagnostic, Severity, Span};
/// Check for exit without error message
pub fn check(source: &str) -> LintResult {
let mut result = LintResult::new();
let lines: Vec<&str> = source.lines().collect();
for (line_num, line) in lines.iter().enumerate() {
let trimmed = line.trim();
// Skip comments
if trimmed.starts_with('#') {
continue;
}
// Strip comments from code
let code_only = if let Some(pos) = trimmed.find('#') {
&trimmed[..pos]
} else {
trimmed
};
let code_only = code_only.trim();
// Detect exit statements (non-zero exit codes)
if has_exit_statement(code_only) {
// Check if exit code is 0 (success - no error message needed)
if code_only.contains("exit 0") {
continue;
}
// Check if error message is on same line (e.g., echo "Error" >&2; exit 1)
if code_only.contains("echo") && code_only.contains(">&2") {
continue;
}
// Check if previous line has error message
if line_num > 0 && has_error_message_on_line(lines[line_num - 1]) {
continue;
}
let span = Span::new(line_num + 1, 1, line_num + 1, line.len());
let diag = Diagnostic::new(
"BASH008",
Severity::Info,
"Exit without error message - add 'echo \"Error: [description]\" >&2' before exit for better debugging",
span,
);
result.add(diag);
}
}
result
}
/// Check if line has exit statement (non-zero)
fn has_exit_statement(line: &str) -> bool {
// Match: exit, exit 1, exit <number>
// But not: exit 0
if line.contains("exit") {
// Exclude "exit 0"
if line.contains("exit 0") {
return false;
}
// Match standalone "exit" or "exit <number>"
return true;
}
false
}
/// Check if line has error message to stderr
fn has_error_message_on_line(line: &str) -> bool {
let trimmed = line.trim();
// Strip comments
let code_only = if let Some(pos) = trimmed.find('#') {
&trimmed[..pos]
} else {
trimmed
};
// Look for echo/printf to stderr
(code_only.contains("echo") || code_only.contains("printf")) && code_only.contains(">&2")
}
#[cfg(test)]
mod tests {
use super::*;
// RED Phase: Write failing tests first (EXTREME TDD)
/// RED TEST 1: Detect exit 1 without error message
#[test]
fn test_BASH008_detects_exit_without_message() {
let script = r#"#!/bin/bash
if [ ! -f "$CONFIG" ]; then
exit 1
fi
"#;
let result = check(script);
assert_eq!(result.diagnostics.len(), 1);
let diag = &result.diagnostics[0];
assert_eq!(diag.code, "BASH008");
assert_eq!(diag.severity, Severity::Info);
assert!(diag.message.contains("error message"));
assert!(diag.message.contains(">&2"));
}
/// RED TEST 2: Pass when error message precedes exit
#[test]
fn test_BASH008_passes_with_error_message() {
let script = r#"#!/bin/bash
if [ ! -f "$CONFIG" ]; then
echo "Error: Config not found" >&2
exit 1
fi
"#;
let result = check(script);
assert_eq!(
result.diagnostics.len(),
0,
"Should pass with error message"
);
}
/// RED TEST 3: Pass when exit 0 (success)
#[test]
fn test_BASH008_passes_exit_0() {
let script = r#"#!/bin/bash
echo "Success"
exit 0
"#;
let result = check(script);
assert_eq!(
result.diagnostics.len(),
0,
"exit 0 doesn't need error message"
);
}
/// RED TEST 4: Pass when error message on same line
#[test]
fn test_BASH008_passes_inline_error() {
let script = r#"#!/bin/bash
command || { echo "Error: Command failed" >&2; exit 1; }
"#;
let result = check(script);
assert_eq!(
result.diagnostics.len(),
0,
"Inline error message should pass"
);
}
/// RED TEST 5: Detect standalone exit
#[test]
fn test_BASH008_detects_standalone_exit() {
let script = r#"#!/bin/bash
if [ -z "$VAR" ]; then
exit
fi
"#;
let result = check(script);
assert_eq!(result.diagnostics.len(), 1);
let diag = &result.diagnostics[0];
assert_eq!(diag.code, "BASH008");
}
/// RED TEST 6: Detect exit with specific code
#[test]
fn test_BASH008_detects_exit_with_code() {
let script = r#"#!/bin/bash
exit 2
"#;
let result = check(script);
assert_eq!(result.diagnostics.len(), 1);
let diag = &result.diagnostics[0];
assert_eq!(diag.code, "BASH008");
}
/// RED TEST 7: Ignore exit in comments
#[test]
fn test_BASH008_ignores_comments() {
let script = r#"#!/bin/bash
# exit 1 if something fails
echo "Running"
"#;
let result = check(script);
assert_eq!(result.diagnostics.len(), 0, "Comments should be ignored");
}
/// RED TEST 8: Pass with printf to stderr
#[test]
fn test_BASH008_passes_printf_stderr() {
let script = r#"#!/bin/bash
printf "Error: Failed\n" >&2
exit 1
"#;
let result = check(script);
assert_eq!(result.diagnostics.len(), 0, "printf to stderr should pass");
}
}
#[cfg(test)]
mod property_tests {
use super::*;
use proptest::prelude::*;
proptest! {
#![proptest_config(proptest::test_runner::Config::with_cases(10))]
/// PROPERTY TEST 1: Never panics on any input
#[test]
fn prop_bash008_never_panics(s in ".*") {
let _ = check(&s);
}
/// PROPERTY TEST 2: Always detects bare exit 1
#[test]
fn prop_bash008_detects_exit_1(
code in 1u8..10,
) {
let script = format!("exit {}", code);
let result = check(&script);
prop_assert_eq!(result.diagnostics.len(), 1);
prop_assert_eq!(result.diagnostics[0].code.as_str(), "BASH008");
}
/// PROPERTY TEST 3: Passes with exit 0
#[test]
fn prop_bash008_passes_exit_0(
_x in 0..100,
) {
let script = "exit 0";
let result = check(script);
prop_assert_eq!(result.diagnostics.len(), 0);
}
}
}