Skip to main content

cha_core/plugins/
cognitive_complexity.rs

1use crate::{AnalysisContext, Finding, Location, Plugin, Severity, SmellCategory};
2
3/// Detect functions with high cognitive complexity.
4///
5/// Cognitive complexity measures how hard code is to *understand*, unlike
6/// cyclomatic complexity which measures testability. It penalizes nesting
7/// and rewards linear, readable structures like `switch`.
8///
9/// ## References
10///
11/// [1] G. A. Campbell, "Cognitive Complexity: A new way of measuring
12///     understandability," SonarSource, 2017.
13///     https://www.sonarsource.com/resources/white-papers/cognitive-complexity/
14pub 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}