cha_core/plugins/
switch_statement.rs1use crate::{AnalysisContext, Finding, Location, Plugin, Severity, SmellCategory};
2
3pub struct SwitchStatementAnalyzer {
5 pub max_arms: usize,
6}
7
8impl Default for SwitchStatementAnalyzer {
9 fn default() -> Self {
10 Self { max_arms: 8 }
11 }
12}
13
14impl Plugin for SwitchStatementAnalyzer {
15 fn name(&self) -> &str {
16 "switch_statement"
17 }
18
19 fn smells(&self) -> Vec<String> {
20 vec!["switch_statement".into()]
21 }
22
23 fn description(&self) -> &str {
24 "Excessive switch/match arms"
25 }
26
27 fn analyze(&self, ctx: &AnalysisContext) -> Vec<Finding> {
28 let lines: Vec<&str> = ctx.file.content.lines().collect();
29 ctx.model
30 .functions
31 .iter()
32 .filter(|f| f.switch_arms > self.max_arms)
33 .map(|f| {
34 let loc = find_switch_keyword(&lines, f.start_line, f.end_line).unwrap_or((
35 f.start_line,
36 f.name_col,
37 f.name_end_col,
38 ));
39 Finding {
40 smell_name: "switch_statement".into(),
41 category: SmellCategory::OoAbusers,
42 severity: Severity::Warning,
43 location: Location {
44 path: ctx.file.path.clone(),
45 start_line: loc.0,
46 start_col: loc.1,
47 end_line: loc.0,
48 end_col: loc.2,
49 name: Some(f.name.clone()),
50 },
51 message: format!(
52 "Function `{}` has {} switch/match arms (threshold: {})",
53 f.name, f.switch_arms, self.max_arms
54 ),
55 suggested_refactorings: vec!["Replace Conditional with Polymorphism".into()],
56 actual_value: Some(f.switch_arms as f64),
57 threshold: Some(self.max_arms as f64),
58 }
59 })
60 .collect()
61 }
62}
63
64fn find_switch_keyword(lines: &[&str], start: usize, end: usize) -> Option<(usize, usize, usize)> {
68 let keywords = ["switch", "match"];
69 for (idx, line) in lines
70 .iter()
71 .enumerate()
72 .take(end.min(lines.len()))
73 .skip(start.saturating_sub(1))
74 {
75 for kw in &keywords {
76 if let Some(col) = find_keyword(line, kw) {
77 return Some((idx + 1, col, col + kw.len()));
78 }
79 }
80 }
81 None
82}
83
84fn find_keyword(line: &str, keyword: &str) -> Option<usize> {
88 let trimmed = line.trim_start();
89 if trimmed.starts_with("//") || trimmed.starts_with('#') || trimmed.starts_with("/*") {
90 return None;
91 }
92 let bytes = line.as_bytes();
93 let klen = keyword.len();
94 let mut i = 0;
95 while i + klen <= bytes.len() {
96 if &bytes[i..i + klen] == keyword.as_bytes() {
97 let before_ok = i == 0 || !is_ident_byte(bytes[i - 1]);
98 let after_ok = i + klen == bytes.len() || !is_ident_byte(bytes[i + klen]);
99 if before_ok && after_ok {
100 return Some(i);
101 }
102 }
103 i += 1;
104 }
105 None
106}
107
108fn is_ident_byte(b: u8) -> bool {
109 b.is_ascii_alphanumeric() || b == b'_'
110}