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
//! The config entries that had no effect in a dead-code pass, as
//! `workspace_diagnostics[]` entries.
use fallow_config::{
ResolvedConfig, RulesConfig, Severity, WorkspaceDiagnostic, WorkspaceDiagnosticKind,
};
/// `workspace_diagnostics[]` entries for the config entries that had no
/// effect in the latest dead-code pass over `config`.
///
/// In config order per setting:
///
/// - each `ignoreFindings` pattern that matched no finding;
/// - each `ignoreDependencies` glob that matched no declared dependency, and
/// each `ignoreDependencies` entry that hid no dependency finding;
/// - each `entry` pattern that matched only files that are entry points
/// anyway;
/// - each `ignorePatterns` glob that excluded no file the run would otherwise
/// analyze;
/// - each `circularDependencies.allow` entry that matched no cycle;
/// - each `ignoreExports` file glob that matched no file, and each
/// `ignoreExports` name that no file of its glob declares;
/// - each `duplicates.ignore` and `health.ignore` glob that matched no file;
/// - each `overrides` rule that cannot change a result of this config;
/// - each `overrides[].files` glob that matched no project file.
///
/// The `ignoreDependencies` entries need `reports_dependencies`. A surface
/// passes `reports_dependencies = false` when its run does not report
/// dependency findings (an issue-type filter without the dependency types, or
/// a file scope), because a dependency entry is then not relevant to what the
/// run shows. A `--production` run checks fewer files and dependency
/// sections, so it reports no redundant entry (issue #3824), no unmatched
/// `circularDependencies.allow` entry (a cycle through a test file is not
/// in that run) and no unmatched ignore list entry (a glob for test files
/// matches no file of that run).
/// A normal run does not report an `ignoreDependencies` entry that a
/// `--production` run could need, because following the note would add a
/// finding to that run.
///
/// An `overrides` rule reports also in a `--production` run, because its cause
/// does not depend on the files of the run. The same is true for an
/// `overrides[].files` glob: its check reads every project file, so the
/// answer does not depend on `--production`, `--changed-since` or
/// `--workspace` (issue #5227). The `reports_dependencies` gate
/// removes the dependency rules (issue #4065). Read `config` after CLI flags
/// changed its rules, because a flag can start a detector.
///
/// The CLI, the programmatic API and the MCP typed path all build their
/// envelope from this one function, and the human note reads the same
/// entries, so every output states the same patterns.
#[must_use]
pub fn config_pattern_diagnostics(
config: &ResolvedConfig,
reports_dependencies: bool,
) -> Vec<WorkspaceDiagnostic> {
let dependencies_reported = reports_dependencies && dependency_rules_on(&config.rules);
let dependency_globs = if dependencies_reported {
config.ignore_dependencies.unmatched_globs()
} else {
Vec::new()
};
let checks_redundancy = !config.production;
let redundant_dependencies = if dependencies_reported && checks_redundancy {
config.ignore_dependencies.redundant_entries()
} else {
Vec::new()
};
let (redundant_entries, redundant_ignores) = if checks_redundancy {
(
config
.entry_pattern_effect
.ineffective(&config.entry_patterns),
config.ignore_patterns.redundant_user_patterns(),
)
} else {
(Vec::new(), Vec::new())
};
let ignore_lists = if checks_redundancy {
unmatched_ignore_list_kinds(config)
} else {
Vec::new()
};
let finding_patterns = config.ignore_findings.unmatched_patterns();
let unmatched_cycles = if checks_redundancy {
config
.circular_dependencies_allow_effect
.ineffective(&config.circular_dependencies.allow)
} else {
Vec::new()
};
let ineffective_overrides = config
.ineffective_override_rules()
.into_iter()
.filter(|entry| reports_dependencies || !entry.dependency_rule);
let unmatched_override_globs = super::override_globs::unmatched_override_globs(config);
let kinds = finding_patterns
.into_iter()
.map(
|pattern| WorkspaceDiagnosticKind::IgnoreFindingsPatternUnmatched {
pattern: pattern.to_owned(),
},
)
.chain(dependency_globs.into_iter().map(|pattern| {
WorkspaceDiagnosticKind::IgnoreDependenciesGlobUnmatched {
pattern: pattern.to_owned(),
}
}))
.chain(redundant_entries.into_iter().map(|pattern| {
WorkspaceDiagnosticKind::EntryPatternRedundant {
pattern: pattern.to_owned(),
}
}))
.chain(redundant_dependencies.into_iter().map(|pattern| {
WorkspaceDiagnosticKind::IgnoreDependenciesEntryRedundant {
pattern: pattern.to_owned(),
}
}))
.chain(redundant_ignores.into_iter().map(|pattern| {
WorkspaceDiagnosticKind::IgnorePatternsEntryRedundant {
pattern: pattern.to_owned(),
}
}))
.chain(unmatched_cycles.into_iter().map(|files| {
WorkspaceDiagnosticKind::CircularDependenciesAllowUnmatched {
pattern: format!("[{}]", files.join(", ")),
files: files.clone(),
}
}))
.chain(ignore_lists)
.chain(ineffective_overrides.map(|entry| {
WorkspaceDiagnosticKind::OverrideRuleIneffective {
rule: entry.rule.to_owned(),
files: entry.files,
reason: entry.reason.to_owned(),
}
}))
.chain(unmatched_override_globs);
kinds
.map(|kind| {
WorkspaceDiagnostic::new(&config.root, config.root.clone(), kind)
.into_root_relative(&config.root)
})
.collect()
}
/// The `ignoreExports`, `duplicates.ignore` and `health.ignore` entries that
/// matched no file or export of the latest dead-code pass, in that order and
/// in config order per setting (issue #5167).
fn unmatched_ignore_list_kinds(config: &ResolvedConfig) -> Vec<WorkspaceDiagnosticKind> {
let effect = &config.ignore_list_effect;
let unmatched_rules: Vec<&str> = effect
.ignore_export_files
.ineffective(&config.ignore_export_rules)
.into_iter()
.map(|rule| rule.file.as_str())
.collect();
let files =
unmatched_rules.iter().map(
|pattern| WorkspaceDiagnosticKind::IgnoreExportsFileUnmatched {
pattern: (*pattern).to_owned(),
},
);
// A rule whose glob matched no file already has a note; a note per name
// of that rule would repeat it.
let names = config.ignore_export_names();
let names = effect
.ignore_export_names
.ineffective(&names)
.into_iter()
.filter(|name| !unmatched_rules.contains(&name.file))
.map(|name| WorkspaceDiagnosticKind::IgnoreExportsNameUnmatched {
pattern: format!("{} in {}", name.export, name.file),
file: name.file.to_owned(),
export: name.export.to_owned(),
});
let duplicates = effect
.duplicates_ignore
.ineffective(&config.duplicates.ignore)
.into_iter()
.map(
|pattern| WorkspaceDiagnosticKind::DuplicatesIgnoreUnmatched {
pattern: pattern.clone(),
},
);
let health = effect
.health_ignore
.ineffective(&config.health.ignore)
.into_iter()
.map(|pattern| WorkspaceDiagnosticKind::HealthIgnoreUnmatched {
pattern: pattern.clone(),
});
files.chain(names).chain(duplicates).chain(health).collect()
}
/// Whether at least one rule that `ignoreDependencies` controls is on. With
/// every such rule off, the run reports no dependency finding at all.
fn dependency_rules_on(rules: &RulesConfig) -> bool {
[
rules.unused_dependencies,
rules.unused_dev_dependencies,
rules.unused_optional_dependencies,
rules.unlisted_dependencies,
rules.type_only_dependencies,
rules.test_only_dependencies,
rules.dev_dependencies_in_production,
]
.into_iter()
.any(|severity| severity != Severity::Off)
}