Skip to main content

cha_core/plugins/
switch_statement.rs

1use crate::{AnalysisContext, Finding, Location, Plugin, Severity, SmellCategory};
2
3/// Detect functions with excessive switch/match arms.
4pub 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                    end_line: f.end_line,
36                    name: Some(f.name.clone()),
37                },
38                message: format!(
39                    "Function `{}` has {} switch/match arms (threshold: {})",
40                    f.name, f.switch_arms, self.max_arms
41                ),
42                suggested_refactorings: vec!["Replace Conditional with Polymorphism".into()],
43                actual_value: Some(f.switch_arms as f64),
44                threshold: Some(self.max_arms as f64),
45            })
46            .collect()
47    }
48}