fallow-engine 3.27.0

Typed analysis engine facade for fallow consumers
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
//! Health findings pipeline assembly.

use std::time::Instant;

use fallow_config::ResolvedConfig;
use fallow_output::{ComplexityViolation, FindingSeverity, RefactoringTarget};

use crate::baseline::HealthBaselineData;

use super::baseline_io::{HealthBaselineSaveInput, load_health_baseline, save_health_baseline};
use super::component_rollup::{
    ComponentTemplateUnitScope, append_component_rollup_findings, collect_component_template_units,
};
use super::filters::filter_complexity_findings_by_diff;
use super::findings::{
    CollectFindingsInput, CrapFindingMergeInput, collect_findings_with_resolver,
    merge_crap_findings, record_template_crap_override_rows,
};
use super::threshold_overrides::{ThresholdOverrideResolver, ThresholdOverrideStateTracker};
use super::{HealthError, HealthOptions, scoring, sort_findings};

pub(super) struct HealthFindingsData {
    pub(super) findings: Vec<ComplexityViolation>,
    pub(super) threshold_overrides: Vec<fallow_output::ThresholdOverrideState>,
    pub(super) files_analyzed: usize,
    pub(super) total_functions: usize,
    pub(super) complexity_ms: f64,
    pub(super) total_above_threshold: usize,
    pub(super) sev_critical: usize,
    pub(super) sev_high: usize,
    pub(super) sev_moderate: usize,
    pub(super) loaded_baseline: Option<HealthBaselineData>,
    pub(super) baseline_staleness: Option<fallow_output::BaselineStaleness>,
}

struct CollectedHealthFindings {
    findings: Vec<ComplexityViolation>,
    files_analyzed: usize,
    total_functions: usize,
    complexity_ms: f64,
}

#[derive(Clone, Copy)]
pub(super) struct HealthFindingsInput<'a> {
    pub(super) opts: &'a HealthOptions<'a>,
    pub(super) config: &'a ResolvedConfig,
    pub(super) modules: &'a [crate::source::ModuleInfo],
    pub(super) file_paths:
        &'a rustc_hash::FxHashMap<crate::discover::FileId, &'a std::path::PathBuf>,
    pub(super) ignore_set: &'a globset::GlobSet,
    pub(super) changed_files: Option<&'a rustc_hash::FxHashSet<std::path::PathBuf>>,
    pub(super) ws_roots: Option<&'a [std::path::PathBuf]>,
    pub(super) diff_index: Option<&'a fallow_output::DiffIndex>,
    pub(super) enforce_crap: bool,
    /// Run-wide override resolver, constructed once in
    /// `prepare_health_core_sections` so file scoring and findings resolve the
    /// same effective ceilings.
    pub(super) threshold_resolver: &'a ThresholdOverrideResolver,
    pub(super) score_output: Option<&'a scoring::FileScoreOutput>,
}

pub(super) fn prepare_health_findings(
    input: HealthFindingsInput<'_>,
) -> Result<HealthFindingsData, HealthError> {
    let t = Instant::now();
    let threshold_resolver = input.threshold_resolver;
    let mut threshold_state_tracker = ThresholdOverrideStateTracker::default();
    let mut collected =
        collect_health_findings(input, threshold_resolver, &mut threshold_state_tracker, t);

    let mut crap_ctx = HealthCrapMergeContext {
        modules: input.modules,
        file_paths: input.file_paths,
        ignore_set: input.ignore_set,
        changed_files: input.changed_files,
        ws_roots: input.ws_roots,
        enforce_crap: input.enforce_crap,
        score_output: input.score_output,
        config_root: &input.config.root,
        threshold_resolver,
        threshold_state_tracker: &mut threshold_state_tracker,
    };
    apply_optional_crap_findings(input.opts, &mut collected.findings, &mut crap_ctx);
    let HealthFindingFinalizeResult {
        total_above_threshold,
        sev_critical,
        sev_high,
        sev_moderate,
        loaded_baseline,
        baseline_staleness,
    } = finalize_health_findings(
        input.opts,
        input.config,
        &mut collected.findings,
        input.diff_index,
        is_change_scoped(&input),
        &mut threshold_state_tracker,
    )?;
    threshold_state_tracker.record_no_match_entries(
        threshold_resolver,
        should_emit_no_match_threshold_overrides(
            input.opts,
            input.changed_files,
            input.ws_roots,
            input.diff_index,
        ),
    );

    Ok(HealthFindingsData {
        findings: collected.findings,
        threshold_overrides: threshold_state_tracker.into_states(),
        files_analyzed: collected.files_analyzed,
        total_functions: collected.total_functions,
        complexity_ms: collected.complexity_ms,
        total_above_threshold,
        sev_critical,
        sev_high,
        sev_moderate,
        loaded_baseline,
        baseline_staleness,
    })
}

fn collect_health_findings(
    input: HealthFindingsInput<'_>,
    threshold_resolver: &ThresholdOverrideResolver,
    threshold_state_tracker: &mut ThresholdOverrideStateTracker,
    started_at: Instant,
) -> CollectedHealthFindings {
    let mut collect_input = CollectFindingsInput {
        modules: input.modules,
        file_paths: input.file_paths,
        config_root: &input.config.root,
        ignore_set: input.ignore_set,
        changed_files: input.changed_files,
        ws_roots: input.ws_roots,
        threshold_resolver,
        threshold_state_tracker,
        complexity_breakdown: input.opts.complexity_breakdown,
    };
    let (findings, files_analyzed, total_functions) =
        collect_findings_with_resolver(&mut collect_input);

    CollectedHealthFindings {
        findings,
        files_analyzed,
        total_functions,
        complexity_ms: started_at.elapsed().as_secs_f64() * 1000.0,
    }
}

struct HealthCrapMergeContext<'a> {
    modules: &'a [crate::source::ModuleInfo],
    file_paths: &'a rustc_hash::FxHashMap<crate::discover::FileId, &'a std::path::PathBuf>,
    ignore_set: &'a globset::GlobSet,
    changed_files: Option<&'a rustc_hash::FxHashSet<std::path::PathBuf>>,
    ws_roots: Option<&'a [std::path::PathBuf]>,
    enforce_crap: bool,
    score_output: Option<&'a scoring::FileScoreOutput>,
    config_root: &'a std::path::Path,
    threshold_resolver: &'a ThresholdOverrideResolver,
    threshold_state_tracker: &'a mut ThresholdOverrideStateTracker,
}

fn apply_optional_crap_findings(
    opts: &HealthOptions<'_>,
    findings: &mut Vec<ComplexityViolation>,
    ctx: &mut HealthCrapMergeContext<'_>,
) {
    if ctx.enforce_crap
        && let Some(score_out) = ctx.score_output
    {
        let mut input = CrapFindingMergeInput {
            modules: ctx.modules,
            file_paths: ctx.file_paths,
            config_root: ctx.config_root,
            ignore_set: ctx.ignore_set,
            changed_files: ctx.changed_files,
            ws_roots: ctx.ws_roots,
            per_function_crap: &score_out.per_function_crap,
            template_inherit_provenance: &score_out.template_inherit_provenance,
            complexity_breakdown: opts.complexity_breakdown,
            threshold_resolver: ctx.threshold_resolver,
            threshold_state_tracker: ctx.threshold_state_tracker,
        };
        merge_crap_findings(findings, &mut input);
        record_template_crap_override_rows(&mut input);
    }
    let template_units = collect_component_template_units(
        ctx.modules,
        ctx.file_paths,
        ctx.score_output
            .map(|output| &output.template_inherit_provenance),
        &ComponentTemplateUnitScope {
            config_root: ctx.config_root,
            ignore_set: ctx.ignore_set,
            changed_files: ctx.changed_files,
            ws_roots: ctx.ws_roots,
        },
    );
    append_component_rollup_findings(
        findings,
        &template_units,
        ctx.threshold_resolver,
        ctx.config_root,
        ctx.threshold_state_tracker,
    );
}

/// Complete each matched override row's `outstanding` list with every dimension
/// on which the unit still has a live finding.
///
/// The list is not filtered by what the entry configures. An override that
/// raises `maxCrap` too little, or that raises `maxCyclomatic` while the unit
/// breaches on cognitive, still leaves a live finding, and suppressing the
/// disclosure in those cases is what made issue #2163 look like a
/// threshold-resolution bug.
///
/// The finding-backed dimensions cannot be derived inside the tracker.
/// `record_complexity` runs during collection, before the CRAP merge, so at
/// record time it is unknown whether a finding survives at all, let alone on
/// which dimension. The unit-size term is the mirror image and is seeded by the
/// tracker instead, because it never produces a finding to read back here.
pub(super) fn annotate_outstanding_dimensions(
    tracker: &mut ThresholdOverrideStateTracker,
    findings: &[ComplexityViolation],
) {
    // Both sides carry the discovery path and the same `(line, col)` the
    // finding is built from, so no relativization is needed. Name and position
    // are BOTH part of the key, and neither alone is an identity: one file can
    // hold several units sharing a name, and the synthetic `<component>` rollup
    // is deliberately anchored at the worst class method's exact `(path, line,
    // col)` while that method keeps its own finding, so keying on either half
    // alone let one unit overwrite the other's `exceeded` (issue #2163).
    let mut exceeded_by_unit: rustc_hash::FxHashMap<
        (&std::path::Path, u32, u32, &str),
        fallow_output::ExceededThreshold,
    > = rustc_hash::FxHashMap::default();
    for finding in findings {
        exceeded_by_unit.insert(
            (
                finding.path.as_path(),
                finding.line,
                finding.col,
                finding.name.as_str(),
            ),
            finding.exceeded,
        );
    }

    for state in tracker.states_mut() {
        let (Some(path), Some(line), Some(col), Some(function)) = (
            state.path.as_deref(),
            state.line,
            state.col,
            state.function.as_deref(),
        ) else {
            continue;
        };
        let Some(exceeded) = exceeded_by_unit.get(&(path, line, col, function)).copied() else {
            continue;
        };
        // `record_complexity` may already have seeded `Complexity` for a
        // surviving unit-size breach, which produces no finding of its own, so
        // the dimension is added only when it is not there yet.
        if (exceeded.includes_cyclomatic() || exceeded.includes_cognitive())
            && !state
                .outstanding
                .contains(&fallow_output::ThresholdOverrideDimension::Complexity)
        {
            state
                .outstanding
                .push(fallow_output::ThresholdOverrideDimension::Complexity);
        }
        if exceeded.includes_crap()
            && !state
                .outstanding
                .contains(&fallow_output::ThresholdOverrideDimension::Crap)
        {
            state
                .outstanding
                .push(fallow_output::ThresholdOverrideDimension::Crap);
        }
    }
}

/// `no_match` rows are the only feedback a user gets that an override glob
/// matched nothing, so a full-repo run must emit them. The `diff_index`
/// parameter is the RESOLVED index, so a run that actually loaded a shared diff
/// is already excluded by the `diff_index.is_none()` clause; gating on the
/// `use_shared_diff_index` request instead made the rows unreachable from every
/// CLI entry point (issue #2163).
fn should_emit_no_match_threshold_overrides(
    opts: &HealthOptions<'_>,
    changed_files: Option<&rustc_hash::FxHashSet<std::path::PathBuf>>,
    ws_roots: Option<&[std::path::PathBuf]>,
    diff_index: Option<&fallow_output::DiffIndex>,
) -> bool {
    opts.changed_since.is_none()
        && opts.diff_index.is_none()
        && opts.workspace.is_none()
        && opts.changed_workspaces.is_none()
        && changed_files.is_none()
        && ws_roots.is_none()
        && diff_index.is_none()
}

/// True when this run analyzes a subset of the project, so a loaded full-repo
/// baseline would look stale for reasons that have nothing to do with rot.
///
/// Production mode counts: it drops test, story and dev files before analysis,
/// so their baseline entries match nothing on a project nobody touched. The
/// resolved config carries the effective flag whether it came from the CLI or
/// from the project config, which is the same reading `dead-code` takes in
/// `baseline_scope_is_narrowed` and `dupes` in `duplication_comparison_is_narrowed`.
fn is_change_scoped(input: &HealthFindingsInput<'_>) -> bool {
    input.diff_index.is_some()
        || input.changed_files.is_some()
        || input.ws_roots.is_some()
        || input.config.production
}

struct HealthFindingFinalizeResult {
    total_above_threshold: usize,
    sev_critical: usize,
    sev_high: usize,
    sev_moderate: usize,
    loaded_baseline: Option<HealthBaselineData>,
    baseline_staleness: Option<fallow_output::BaselineStaleness>,
}

fn finalize_health_findings(
    opts: &HealthOptions<'_>,
    config: &ResolvedConfig,
    findings: &mut Vec<ComplexityViolation>,
    diff_index: Option<&fallow_output::DiffIndex>,
    change_scoped: bool,
    threshold_state_tracker: &mut ThresholdOverrideStateTracker,
) -> Result<HealthFindingFinalizeResult, HealthError> {
    // Runs before every downstream narrowing. The override rows were recorded
    // over the whole collection pass, while `--diff-file`/`--diff-stdin`,
    // `--baseline` and `--top` all narrow the findings list for DISPLAY, so
    // annotating after any of them leaves an `insufficient` row with an empty
    // `outstanding`: the one contradiction a CI gate cannot detect.
    annotate_outstanding_dimensions(threshold_state_tracker, findings);
    if let Some(diff_index) = diff_index {
        filter_complexity_findings_by_diff(findings, diff_index, &config.root);
    }
    sort_findings(findings, opts.sort);
    let total_above_threshold = findings.len();
    let (sev_critical, sev_high, sev_moderate) = count_finding_severities(findings);
    let (loaded_baseline, baseline_staleness) =
        apply_health_baseline_and_top(opts, config, findings, change_scoped)?;
    Ok(HealthFindingFinalizeResult {
        total_above_threshold,
        sev_critical,
        sev_high,
        sev_moderate,
        loaded_baseline,
        baseline_staleness,
    })
}

fn count_finding_severities(findings: &[ComplexityViolation]) -> (usize, usize, usize) {
    let (mut critical, mut high, mut moderate) = (0usize, 0usize, 0usize);
    for finding in findings {
        match finding.severity {
            FindingSeverity::Critical => critical += 1,
            FindingSeverity::High => high += 1,
            FindingSeverity::Moderate => moderate += 1,
        }
    }
    (critical, high, moderate)
}

type LoadedBaselineParts = (
    Option<HealthBaselineData>,
    Option<fallow_output::BaselineStaleness>,
);

fn apply_health_baseline_and_top(
    opts: &HealthOptions<'_>,
    config: &ResolvedConfig,
    findings: &mut Vec<ComplexityViolation>,
    change_scoped: bool,
) -> Result<LoadedBaselineParts, HealthError> {
    let (loaded_baseline, baseline_staleness) = if let Some(load_path) = opts.baseline {
        let loaded = load_health_baseline(
            load_path,
            findings,
            &config.root,
            opts.quiet,
            opts.baseline_mode,
            change_scoped,
        )?;
        (Some(loaded.data), Some(loaded.staleness))
    } else {
        (None, None)
    };
    if let Some(top) = opts.top {
        findings.truncate(top);
    }
    Ok((loaded_baseline, baseline_staleness))
}

pub(super) fn save_health_baseline_if_requested(
    opts: &HealthOptions<'_>,
    config: &ResolvedConfig,
    findings: &[ComplexityViolation],
    runtime_coverage: Option<&fallow_output::RuntimeCoverageReport>,
    targets: &[RefactoringTarget],
) -> Result<(), HealthError> {
    if let Some(save_path) = opts.save_baseline {
        save_health_baseline(&HealthBaselineSaveInput {
            save_path,
            findings,
            runtime_coverage_findings: runtime_coverage
                .map_or(&[], |report| report.findings.as_slice()),
            targets,
            config_root: &config.root,
            quiet: opts.quiet,
            mode: opts.baseline_mode,
            mode_explicit: opts.baseline_mode_explicit,
        })?;
    }
    Ok(())
}