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 description(&self) -> &str {
20 "Excessive switch/match arms"
21 }
22
23 fn analyze(&self, ctx: &AnalysisContext) -> Vec<Finding> {
24 ctx.model
25 .functions
26 .iter()
27 .filter(|f| f.switch_arms > self.max_arms)
28 .map(|f| Finding {
29 smell_name: "switch_statement".into(),
30 category: SmellCategory::OoAbusers,
31 severity: Severity::Warning,
32 location: Location {
33 path: ctx.file.path.clone(),
34 start_line: f.start_line,
35 start_col: f.name_col,
36 end_line: f.start_line,
37 end_col: f.name_end_col,
38 name: Some(f.name.clone()),
39 },
40 message: format!(
41 "Function `{}` has {} switch/match arms (threshold: {})",
42 f.name, f.switch_arms, self.max_arms
43 ),
44 suggested_refactorings: vec!["Replace Conditional with Polymorphism".into()],
45 actual_value: Some(f.switch_arms as f64),
46 threshold: Some(self.max_arms as f64),
47 })
48 .collect()
49 }
50}