Skip to main content

fallow_engine/
dead_code_config_patterns.rs

1//! The config entries that had no effect in a dead-code pass, as
2//! `workspace_diagnostics[]` entries.
3
4use fallow_config::{
5    ResolvedConfig, RulesConfig, Severity, WorkspaceDiagnostic, WorkspaceDiagnosticKind,
6};
7
8/// `workspace_diagnostics[]` entries for the config entries that had no
9/// effect in the latest dead-code pass over `config`.
10///
11/// In config order per setting:
12///
13/// - each `ignoreFindings` pattern that matched no finding;
14/// - each `ignoreDependencies` glob that matched no declared dependency, and
15///   each `ignoreDependencies` entry that hid no dependency finding;
16/// - each `entry` pattern that matched only files that are entry points
17///   anyway;
18/// - each `ignorePatterns` glob that excluded no file the run would otherwise
19///   analyze;
20/// - each `overrides` rule that cannot change a result of this config.
21///
22/// The `ignoreDependencies` entries need `reports_dependencies`. A surface
23/// passes `reports_dependencies = false` when its run does not report
24/// dependency findings (an issue-type filter without the dependency types, or
25/// a file scope), because a dependency entry is then not relevant to what the
26/// run shows. A `--production` run checks fewer files and dependency
27/// sections, so it reports no redundant entry (issue #3824).
28/// A normal run does not report an `ignoreDependencies` entry that a
29/// `--production` run could need, because following the note would add a
30/// finding to that run.
31///
32/// An `overrides` rule reports also in a `--production` run, because its cause
33/// does not depend on the files of the run. The `reports_dependencies` gate
34/// removes the dependency rules (issue #4065). Read `config` after CLI flags
35/// changed its rules, because a flag can start a detector.
36///
37/// The CLI, the programmatic API and the MCP typed path all build their
38/// envelope from this one function, and the human note reads the same
39/// entries, so every output states the same patterns.
40#[must_use]
41pub fn config_pattern_diagnostics(
42    config: &ResolvedConfig,
43    reports_dependencies: bool,
44) -> Vec<WorkspaceDiagnostic> {
45    let dependencies_reported = reports_dependencies && dependency_rules_on(&config.rules);
46    let dependency_globs = if dependencies_reported {
47        config.ignore_dependencies.unmatched_globs()
48    } else {
49        Vec::new()
50    };
51    let checks_redundancy = !config.production;
52    let redundant_dependencies = if dependencies_reported && checks_redundancy {
53        config.ignore_dependencies.redundant_entries()
54    } else {
55        Vec::new()
56    };
57    let (redundant_entries, redundant_ignores) = if checks_redundancy {
58        (
59            config
60                .entry_pattern_effect
61                .ineffective(&config.entry_patterns),
62            config.ignore_patterns.redundant_user_patterns(),
63        )
64    } else {
65        (Vec::new(), Vec::new())
66    };
67    let finding_patterns = config.ignore_findings.unmatched_patterns();
68    let ineffective_overrides = config
69        .ineffective_override_rules()
70        .into_iter()
71        .filter(|entry| reports_dependencies || !entry.dependency_rule);
72    let kinds = finding_patterns
73        .into_iter()
74        .map(
75            |pattern| WorkspaceDiagnosticKind::IgnoreFindingsPatternUnmatched {
76                pattern: pattern.to_owned(),
77            },
78        )
79        .chain(dependency_globs.into_iter().map(|pattern| {
80            WorkspaceDiagnosticKind::IgnoreDependenciesGlobUnmatched {
81                pattern: pattern.to_owned(),
82            }
83        }))
84        .chain(redundant_entries.into_iter().map(|pattern| {
85            WorkspaceDiagnosticKind::EntryPatternRedundant {
86                pattern: pattern.to_owned(),
87            }
88        }))
89        .chain(redundant_dependencies.into_iter().map(|pattern| {
90            WorkspaceDiagnosticKind::IgnoreDependenciesEntryRedundant {
91                pattern: pattern.to_owned(),
92            }
93        }))
94        .chain(redundant_ignores.into_iter().map(|pattern| {
95            WorkspaceDiagnosticKind::IgnorePatternsEntryRedundant {
96                pattern: pattern.to_owned(),
97            }
98        }))
99        .chain(ineffective_overrides.map(|entry| {
100            WorkspaceDiagnosticKind::OverrideRuleIneffective {
101                rule: entry.rule.to_owned(),
102                files: entry.files,
103                reason: entry.reason.to_owned(),
104            }
105        }));
106    kinds
107        .map(|kind| {
108            WorkspaceDiagnostic::new(&config.root, config.root.clone(), kind)
109                .into_root_relative(&config.root)
110        })
111        .collect()
112}
113
114/// Whether at least one rule that `ignoreDependencies` controls is on. With
115/// every such rule off, the run reports no dependency finding at all.
116fn dependency_rules_on(rules: &RulesConfig) -> bool {
117    [
118        rules.unused_dependencies,
119        rules.unused_dev_dependencies,
120        rules.unused_optional_dependencies,
121        rules.unlisted_dependencies,
122        rules.type_only_dependencies,
123        rules.test_only_dependencies,
124        rules.dev_dependencies_in_production,
125    ]
126    .into_iter()
127    .any(|severity| severity != Severity::Off)
128}