fallow_engine/health/
inline.rs1use std::path::PathBuf;
5
6use fallow_config::{ResolvedConfig, Severity};
7use fallow_types::discover::DiscoveredFile;
8
9use super::threshold_overrides::{GlobalHealthThresholds, ThresholdOverrideResolver};
10use crate::source::ModuleInfo;
11
12#[derive(Debug, Clone, PartialEq, Eq)]
14pub struct InlineComplexity {
15 pub path: PathBuf,
17 pub name: String,
19 pub line: u32,
21 pub col: u32,
23 pub cyclomatic: u16,
26 pub cognitive: u16,
28 pub exceeds_cyclomatic: bool,
30 pub exceeds_cognitive: bool,
32}
33
34#[must_use]
48pub fn inline_complexity(
49 config: &ResolvedConfig,
50 modules: &[ModuleInfo],
51 files: &[DiscoveredFile],
52) -> Vec<InlineComplexity> {
53 let file_paths: rustc_hash::FxHashMap<_, _> =
54 files.iter().map(|file| (file.id, &file.path)).collect();
55 let ignore_set = super::ignore::build_ignore_set(&config.health.ignore);
56 let resolver = ThresholdOverrideResolver::new(
57 &config.health.threshold_overrides,
58 GlobalHealthThresholds {
59 cyclomatic: config.health.max_cyclomatic,
60 cognitive: config.health.max_cognitive,
61 crap: config.health.max_crap,
62 unit_size: config.health.max_unit_size,
63 },
64 )
65 .with_cyclomatic_policy(config.health.cyclomatic_policy)
66 .with_inline_overrides(
67 config.health.inline_threshold_overrides,
68 modules,
69 &file_paths,
70 &config.root,
71 );
72 let mut findings = Vec::new();
73
74 for module in modules {
75 let Some(path) = file_paths.get(&module.file_id) else {
76 continue;
77 };
78 let relative = path.strip_prefix(&config.root).unwrap_or(path);
79 if ignore_set.is_match(relative) {
80 continue;
81 }
82 let path_rules =
84 (!config.overrides.is_empty()).then(|| config.resolve_rules_for_path(path));
85 let rules = path_rules.as_ref().unwrap_or(&config.rules);
86 for function in &module.complexity {
87 if fallow_types::extract::is_synthetic_module_unit(&function.name)
88 || crate::suppress::is_suppressed(
89 &module.suppressions,
90 function.line,
91 crate::suppress::IssueKind::Complexity,
92 )
93 {
94 continue;
95 }
96 let (applied, _, _) = resolver.resolve_unit(relative, &function.name, function.line);
97 let Some((exceeds_cyclomatic, exceeds_cognitive)) =
98 super::findings::complexity_exceeded(
99 function,
100 resolver.cyclomatic_policy(),
101 applied.effective,
102 )
103 else {
104 continue;
105 };
106 if rules.complexity_severity(exceeds_cyclomatic, exceeds_cognitive, false)
108 == Severity::Off
109 {
110 continue;
111 }
112 findings.push(InlineComplexity {
113 path: (*path).clone(),
114 name: function.name.clone(),
115 line: function.line,
116 col: function.col,
117 cyclomatic: resolver.gate_cyclomatic(function),
118 cognitive: function.cognitive,
119 exceeds_cyclomatic,
120 exceeds_cognitive,
121 });
122 }
123 }
124
125 findings
126}