rigsql_rules/structure/
st02.rs1use rigsql_core::{Segment, SegmentType};
2
3use crate::rule::{CrawlType, Rule, RuleContext, RuleGroup};
4use crate::violation::LintViolation;
5
6#[derive(Debug, Default)]
12pub struct RuleST02;
13
14impl Rule for RuleST02 {
15 fn code(&self) -> &'static str {
16 "ST02"
17 }
18 fn name(&self) -> &'static str {
19 "structure.simple_case"
20 }
21 fn description(&self) -> &'static str {
22 "Unnecessary CASE expression."
23 }
24 fn explanation(&self) -> &'static str {
25 "A CASE expression is unnecessary when it can be replaced by a simpler construct: \
26 (1) A single WHEN returning TRUE/FALSE (or 1/0) with an opposite ELSE can use the \
27 condition directly. (2) A CASE WHEN x IS NULL THEN y ELSE x END can be replaced \
28 with COALESCE(x, y)."
29 }
30 fn groups(&self) -> &[RuleGroup] {
31 &[RuleGroup::Structure]
32 }
33 fn is_fixable(&self) -> bool {
34 false
35 }
36
37 fn crawl_type(&self) -> CrawlType {
38 CrawlType::Segment(vec![SegmentType::CaseExpression])
39 }
40
41 fn eval(&self, ctx: &RuleContext) -> Vec<LintViolation> {
42 let children = ctx.segment.children();
43 let non_trivia: Vec<_> = children
44 .iter()
45 .filter(|s| !s.segment_type().is_trivia())
46 .collect();
47
48 let when_clauses: Vec<_> = non_trivia
50 .iter()
51 .filter(|s| s.segment_type() == SegmentType::WhenClause)
52 .collect();
53 let else_clauses: Vec<_> = non_trivia
54 .iter()
55 .filter(|s| s.segment_type() == SegmentType::ElseClause)
56 .collect();
57
58 if when_clauses.len() != 1 || else_clauses.len() != 1 {
59 return vec![];
60 }
61
62 let when_clause = when_clauses[0];
63 let else_clause = else_clauses[0];
64
65 let then_value = extract_then_value(when_clause);
67 let else_value = extract_else_value(else_clause);
68
69 if let (Some(ref then_val), Some(ref else_val)) = (then_value, else_value) {
70 let is_bool_pair = (is_truthy(then_val) && is_falsy(else_val))
71 || (is_falsy(then_val) && is_truthy(else_val));
72
73 if is_bool_pair {
74 return vec![LintViolation::new(
75 self.code(),
76 "Unnecessary CASE expression. Use the boolean condition directly.",
77 ctx.segment.span(),
78 )];
79 }
80 }
81
82 if let Some(msg) = check_is_null_coalesce_pattern(when_clause, else_clause) {
84 return vec![LintViolation::new(self.code(), msg, ctx.segment.span())];
85 }
86
87 vec![]
88 }
89}
90
91fn check_is_null_coalesce_pattern(when_clause: &Segment, else_clause: &Segment) -> Option<String> {
93 let when_children: Vec<_> = when_clause
94 .children()
95 .iter()
96 .filter(|c| !c.segment_type().is_trivia())
97 .collect();
98
99 let is_null_expr = when_children
101 .iter()
102 .find(|c| c.segment_type() == SegmentType::IsNullExpression)?;
103
104 let tested_col = get_is_null_subject(is_null_expr)?;
106
107 let else_expr = get_else_expression(else_clause)?;
109
110 if tested_col.eq_ignore_ascii_case(&else_expr) {
112 Some(
113 "Unnecessary CASE expression. Use COALESCE instead of CASE WHEN IS NULL pattern."
114 .to_string(),
115 )
116 } else {
117 None
118 }
119}
120
121fn get_is_null_subject(segment: &Segment) -> Option<String> {
123 let children = segment.children();
124 let non_trivia: Vec<_> = children
125 .iter()
126 .filter(|c| !c.segment_type().is_trivia())
127 .collect();
128
129 non_trivia.first().map(|s| s.raw().trim().to_string())
130}
131
132fn get_else_expression(segment: &Segment) -> Option<String> {
134 let children = segment.children();
135 let non_trivia: Vec<_> = children
136 .iter()
137 .filter(|c| !c.segment_type().is_trivia())
138 .collect();
139
140 if non_trivia.len() >= 2 {
141 let expr_parts: String = non_trivia[1..]
142 .iter()
143 .map(|s| s.raw())
144 .collect::<Vec<_>>()
145 .join("");
146 Some(expr_parts.trim().to_string())
147 } else {
148 None
149 }
150}
151
152fn extract_then_value(when_clause: &Segment) -> Option<String> {
153 let children = when_clause.children();
154 let non_trivia: Vec<_> = children
155 .iter()
156 .filter(|s| !s.segment_type().is_trivia())
157 .collect();
158
159 let mut found_then = false;
160 for seg in &non_trivia {
161 if found_then {
162 return Some(seg.raw().trim().to_uppercase());
163 }
164 if seg.segment_type() == SegmentType::Keyword && seg.raw().eq_ignore_ascii_case("THEN") {
165 found_then = true;
166 }
167 }
168 None
169}
170
171fn extract_else_value(else_clause: &Segment) -> Option<String> {
172 let children = else_clause.children();
173 let non_trivia: Vec<_> = children
174 .iter()
175 .filter(|s| !s.segment_type().is_trivia())
176 .collect();
177
178 if non_trivia.len() >= 2 {
179 return Some(non_trivia[1].raw().trim().to_uppercase());
180 }
181 None
182}
183
184fn is_truthy(val: &str) -> bool {
185 val == "TRUE" || val == "1"
186}
187
188fn is_falsy(val: &str) -> bool {
189 val == "FALSE" || val == "0"
190}
191
192#[cfg(test)]
193mod tests {
194 use super::*;
195 use crate::test_utils::lint_sql;
196
197 #[test]
198 fn test_st02_flags_simple_boolean_case() {
199 let violations = lint_sql("SELECT CASE WHEN x > 0 THEN TRUE ELSE FALSE END;", RuleST02);
200 assert_eq!(violations.len(), 1);
201 assert!(violations[0].message.contains("boolean"));
202 }
203
204 #[test]
205 fn test_st02_accepts_non_boolean_case() {
206 let violations = lint_sql("SELECT CASE WHEN x > 0 THEN 'yes' ELSE 'no' END;", RuleST02);
207 assert_eq!(violations.len(), 0);
208 }
209
210 #[test]
211 fn test_st02_accepts_multi_when() {
212 let violations = lint_sql(
213 "SELECT CASE WHEN x > 0 THEN TRUE WHEN x < 0 THEN FALSE ELSE FALSE END;",
214 RuleST02,
215 );
216 assert_eq!(violations.len(), 0);
217 }
218
219 #[test]
220 fn test_st02_flags_is_null_coalesce_pattern() {
221 let violations = lint_sql(
222 "SELECT CASE WHEN col IS NULL THEN 'default' ELSE col END FROM t",
223 RuleST02,
224 );
225 assert_eq!(violations.len(), 1);
226 assert!(violations[0].message.contains("COALESCE"));
227 }
228
229 #[test]
230 fn test_st02_accepts_complex_case() {
231 let violations = lint_sql(
232 "SELECT CASE WHEN x = 1 THEN 'a' ELSE 'b' END FROM t",
233 RuleST02,
234 );
235 assert_eq!(violations.len(), 0);
236 }
237}