Skip to main content

fallow_engine/
ignore_list_notes.rs

1//! The `duplicates.ignore` and `health.ignore` globs that match no file, as
2//! `workspace_diagnostics[]` entries of the run that reads the setting
3//! (issue #5202). The dead-code envelope reports the same entries through
4//! [`crate::dead_code::config_pattern_diagnostics`].
5
6use fallow_config::{ResolvedConfig, WorkspaceDiagnostic, WorkspaceDiagnosticKind};
7use fallow_types::discover::DiscoveredFile;
8
9/// One `duplicates-ignore-unmatched` entry per `duplicates.ignore` glob that
10/// matches none of `files` and no other project file, in config order.
11#[must_use]
12pub fn duplicates_ignore_diagnostics(
13    config: &ResolvedConfig,
14    files: &[DiscoveredFile],
15) -> Vec<WorkspaceDiagnostic> {
16    unmatched(config, &config.duplicates.ignore, files, |pattern| {
17        WorkspaceDiagnosticKind::DuplicatesIgnoreUnmatched { pattern }
18    })
19}
20
21/// One `health-ignore-unmatched` entry per `health.ignore` glob that matches
22/// none of `files` and no other project file, in config order.
23#[must_use]
24pub fn health_ignore_diagnostics(
25    config: &ResolvedConfig,
26    files: &[DiscoveredFile],
27) -> Vec<WorkspaceDiagnostic> {
28    unmatched(config, &config.health.ignore, files, |pattern| {
29        WorkspaceDiagnosticKind::HealthIgnoreUnmatched { pattern }
30    })
31}
32
33/// A `--production` run analyzes fewer files, so a glob for test files
34/// matches no file of that run. Such a run reports no entry, like the
35/// dead-code envelope.
36fn unmatched(
37    config: &ResolvedConfig,
38    patterns: &[String],
39    files: &[DiscoveredFile],
40    kind: impl Fn(String) -> WorkspaceDiagnosticKind,
41) -> Vec<WorkspaceDiagnostic> {
42    if config.production || patterns.is_empty() {
43        return Vec::new();
44    }
45    let hits = config
46        .path_glob_effect(&[patterns], files.iter().map(|file| file.path.as_path()))
47        .into_iter()
48        .next()
49        .unwrap_or_default();
50    patterns
51        .iter()
52        .zip(hits)
53        .filter(|(_, hit)| !hit)
54        .map(|(pattern, _)| {
55            WorkspaceDiagnostic::new(&config.root, config.root.clone(), kind(pattern.clone()))
56                .into_root_relative(&config.root)
57        })
58        .collect()
59}