cha_core/plugins/
cognitive_complexity.rs1use crate::{AnalysisContext, Finding, Location, Plugin, Severity, SmellCategory};
2
3pub struct CognitiveComplexityAnalyzer {
15 pub threshold: usize,
16}
17
18impl Default for CognitiveComplexityAnalyzer {
19 fn default() -> Self {
20 Self { threshold: 15 }
21 }
22}
23
24impl Plugin for CognitiveComplexityAnalyzer {
25 fn name(&self) -> &str {
26 "cognitive_complexity"
27 }
28
29 fn description(&self) -> &str {
30 "Cognitive complexity exceeds threshold"
31 }
32
33 fn analyze(&self, ctx: &AnalysisContext) -> Vec<Finding> {
34 ctx.model
35 .functions
36 .iter()
37 .filter(|f| f.cognitive_complexity > self.threshold)
38 .map(|f| Finding {
39 smell_name: "cognitive_complexity".into(),
40 category: SmellCategory::Bloaters,
41 severity: if f.cognitive_complexity > self.threshold * 2 {
42 Severity::Error
43 } else {
44 Severity::Warning
45 },
46 location: Location {
47 path: ctx.file.path.clone(),
48 start_line: f.start_line,
49 end_line: f.end_line,
50 name: Some(f.name.clone()),
51 },
52 message: format!(
53 "Function `{}` has cognitive complexity {} (threshold: {})",
54 f.name, f.cognitive_complexity, self.threshold
55 ),
56 suggested_refactorings: vec![
57 "Extract Method".into(),
58 "Replace Nested Conditional with Guard Clauses".into(),
59 ],
60 actual_value: Some(f.cognitive_complexity as f64),
61 threshold: Some(self.threshold as f64),
62 })
63 .collect()
64 }
65}