Skip to main content

cha_core/plugins/
cognitive_complexity.rs

1use crate::{AnalysisContext, Finding, Plugin, Severity, SmellCategory, func_location};
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| {
39                let severity = if f.cognitive_complexity > self.threshold * 2 {
40                    Severity::Error
41                } else {
42                    Severity::Warning
43                };
44                Finding {
45                    smell_name: "cognitive_complexity".into(),
46                    category: SmellCategory::Bloaters,
47                    severity,
48                    location: func_location(&ctx.file.path, f),
49                    message: format!(
50                        "Function `{}` has cognitive complexity {} (threshold: {})",
51                        f.name, f.cognitive_complexity, self.threshold
52                    ),
53                    suggested_refactorings: vec![
54                        "Extract Method".into(),
55                        "Replace Nested Conditional with Guard Clauses".into(),
56                    ],
57                    actual_value: Some(f.cognitive_complexity as f64),
58                    threshold: Some(self.threshold as f64),
59                }
60            })
61            .collect()
62    }
63}