rigsql_rules/ambiguous/
am08.rs1use rigsql_core::SegmentType;
2
3use crate::rule::{CrawlType, Rule, RuleContext, RuleGroup};
4use crate::violation::LintViolation;
5
6#[derive(Debug, Default)]
11pub struct RuleAM08;
12
13impl Rule for RuleAM08 {
14 fn code(&self) -> &'static str {
15 "AM08"
16 }
17 fn name(&self) -> &'static str {
18 "ambiguous.join_condition"
19 }
20 fn description(&self) -> &'static str {
21 "Implicit cross join in FROM clause."
22 }
23 fn explanation(&self) -> &'static str {
24 "Using commas to separate tables in a FROM clause creates an implicit cross join \
25 (Cartesian product). Use explicit CROSS JOIN syntax instead, or use appropriate \
26 JOIN ... ON clauses if you intend a different join type."
27 }
28 fn groups(&self) -> &[RuleGroup] {
29 &[RuleGroup::Ambiguous]
30 }
31 fn is_fixable(&self) -> bool {
32 false
33 }
34
35 fn crawl_type(&self) -> CrawlType {
36 CrawlType::Segment(vec![SegmentType::FromClause])
37 }
38
39 fn eval(&self, ctx: &RuleContext) -> Vec<LintViolation> {
40 let children = ctx.segment.children();
41
42 children
43 .iter()
44 .filter(|c| c.segment_type() == SegmentType::Comma)
45 .map(|comma| {
46 LintViolation::new(
47 self.code(),
48 "Implicit cross join via comma in FROM clause. Use explicit JOIN.",
49 comma.span(),
50 )
51 })
52 .collect()
53 }
54}
55
56#[cfg(test)]
57mod tests {
58 use super::*;
59 use crate::test_utils::lint_sql;
60
61 #[test]
62 fn test_am08_flags_comma_join() {
63 let violations = lint_sql("SELECT a FROM t, u", RuleAM08);
64 assert_eq!(violations.len(), 1);
65 }
66
67 #[test]
68 fn test_am08_accepts_explicit_join() {
69 let violations = lint_sql("SELECT a FROM t INNER JOIN u ON t.id = u.id", RuleAM08);
70 assert_eq!(violations.len(), 0);
71 }
72
73 #[test]
74 fn test_am08_flags_multiple_commas() {
75 let violations = lint_sql("SELECT a FROM t, u, v", RuleAM08);
76 assert_eq!(violations.len(), 2);
77 }
78}