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/// - each `circularDependencies.allow` entry that matched no cycle.
22///
23/// The `ignoreDependencies` entries need `reports_dependencies`. A surface
24/// passes `reports_dependencies = false` when its run does not report
25/// dependency findings (an issue-type filter without the dependency types, or
26/// a file scope), because a dependency entry is then not relevant to what the
27/// run shows. A `--production` run checks fewer files and dependency
28/// sections, so it reports no redundant entry (issue #3824) and no unmatched
29/// `circularDependencies.allow` entry: a cycle through a test file is not
30/// in that run.
31/// A normal run does not report an `ignoreDependencies` entry that a
32/// `--production` run could need, because following the note would add a
33/// finding to that run.
34///
35/// An `overrides` rule reports also in a `--production` run, because its cause
36/// does not depend on the files of the run. The `reports_dependencies` gate
37/// removes the dependency rules (issue #4065). Read `config` after CLI flags
38/// changed its rules, because a flag can start a detector.
39///
40/// The CLI, the programmatic API and the MCP typed path all build their
41/// envelope from this one function, and the human note reads the same
42/// entries, so every output states the same patterns.
43#[must_use]
44pub fn config_pattern_diagnostics(
45    config: &ResolvedConfig,
46    reports_dependencies: bool,
47) -> Vec<WorkspaceDiagnostic> {
48    let dependencies_reported = reports_dependencies && dependency_rules_on(&config.rules);
49    let dependency_globs = if dependencies_reported {
50        config.ignore_dependencies.unmatched_globs()
51    } else {
52        Vec::new()
53    };
54    let checks_redundancy = !config.production;
55    let redundant_dependencies = if dependencies_reported && checks_redundancy {
56        config.ignore_dependencies.redundant_entries()
57    } else {
58        Vec::new()
59    };
60    let (redundant_entries, redundant_ignores) = if checks_redundancy {
61        (
62            config
63                .entry_pattern_effect
64                .ineffective(&config.entry_patterns),
65            config.ignore_patterns.redundant_user_patterns(),
66        )
67    } else {
68        (Vec::new(), Vec::new())
69    };
70    let finding_patterns = config.ignore_findings.unmatched_patterns();
71    let unmatched_cycles = if checks_redundancy {
72        config
73            .circular_dependencies_allow_effect
74            .ineffective(&config.circular_dependencies.allow)
75    } else {
76        Vec::new()
77    };
78    let ineffective_overrides = config
79        .ineffective_override_rules()
80        .into_iter()
81        .filter(|entry| reports_dependencies || !entry.dependency_rule);
82    let kinds = finding_patterns
83        .into_iter()
84        .map(
85            |pattern| WorkspaceDiagnosticKind::IgnoreFindingsPatternUnmatched {
86                pattern: pattern.to_owned(),
87            },
88        )
89        .chain(dependency_globs.into_iter().map(|pattern| {
90            WorkspaceDiagnosticKind::IgnoreDependenciesGlobUnmatched {
91                pattern: pattern.to_owned(),
92            }
93        }))
94        .chain(redundant_entries.into_iter().map(|pattern| {
95            WorkspaceDiagnosticKind::EntryPatternRedundant {
96                pattern: pattern.to_owned(),
97            }
98        }))
99        .chain(redundant_dependencies.into_iter().map(|pattern| {
100            WorkspaceDiagnosticKind::IgnoreDependenciesEntryRedundant {
101                pattern: pattern.to_owned(),
102            }
103        }))
104        .chain(redundant_ignores.into_iter().map(|pattern| {
105            WorkspaceDiagnosticKind::IgnorePatternsEntryRedundant {
106                pattern: pattern.to_owned(),
107            }
108        }))
109        .chain(unmatched_cycles.into_iter().map(|files| {
110            WorkspaceDiagnosticKind::CircularDependenciesAllowUnmatched {
111                pattern: format!("[{}]", files.join(", ")),
112                files: files.clone(),
113            }
114        }))
115        .chain(ineffective_overrides.map(|entry| {
116            WorkspaceDiagnosticKind::OverrideRuleIneffective {
117                rule: entry.rule.to_owned(),
118                files: entry.files,
119                reason: entry.reason.to_owned(),
120            }
121        }));
122    kinds
123        .map(|kind| {
124            WorkspaceDiagnostic::new(&config.root, config.root.clone(), kind)
125                .into_root_relative(&config.root)
126        })
127        .collect()
128}
129
130/// Whether at least one rule that `ignoreDependencies` controls is on. With
131/// every such rule off, the run reports no dependency finding at all.
132fn dependency_rules_on(rules: &RulesConfig) -> bool {
133    [
134        rules.unused_dependencies,
135        rules.unused_dev_dependencies,
136        rules.unused_optional_dependencies,
137        rules.unlisted_dependencies,
138        rules.type_only_dependencies,
139        rules.test_only_dependencies,
140        rules.dev_dependencies_in_production,
141    ]
142    .into_iter()
143    .any(|severity| severity != Severity::Off)
144}