fallow-core 3.34.1

Internal detector backend for fallow-engine and fallow-api
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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
//! Effect dead-code rules: `unused-effect-layer`, `unused-tagged-error` and
//! `unproduced-catch-tag`.
//!
//! The rules read the [`EffectFacts`] that extraction records for modules that
//! import Effect, plus the import usage of every module. Names join across
//! modules, and every uncertain signal credits a name. A credit can only
//! remove a finding, so a missed reference gives a false negative, never a
//! false positive.
//!
//! - A layer is unused when no root reaches it through the layer graph and at
//!   least one other layer composes it. A layer with zero references is left
//!   to `unused-export` and `unused-class-member`.
//! - A tagged error is unused when some type position or `catchTag` handler
//!   references it, and no code reads the class as a value. A schema union, an
//!   API error declaration and `new X()` are all value reads.
//! - A `catchTag` handler is unproduced when every project class with its tag
//!   is unused and no `unused-tagged-error` finding covers those classes.

use std::path::Path;

use rustc_hash::{FxHashMap, FxHashSet};

use fallow_types::extract::{EffectFacts, ExportName, ImportedName, ModuleInfo};

use crate::discover::FileId;
use crate::graph::ModuleGraph;
use crate::resolve::ResolvedModule;
use crate::results::{
    EffectCatchSite, EffectLayerKind, UnproducedCatchTag, UnusedEffectLayer, UnusedTaggedError,
};

use super::{LineOffsetsMap, byte_offset_to_line_col};

/// Error tags that Effect and the `@effect/*` packages produce. A handler for
/// one of these tags is never reported, even when a project class reuses it.
const BUILTIN_TAGS: &[&str] = &[
    "And",
    "BadArgument",
    "Cause",
    "ConfigError",
    "Die",
    "Empty",
    "ExceededCapacityException",
    "Fail",
    "HttpApiDecodeError",
    "HttpBodyError",
    "IllegalArgumentException",
    "Interrupt",
    "InterruptedException",
    "InvalidData",
    "InvalidPubSubCapacityException",
    "MalformedMessage",
    "MissingData",
    "NoSuchElementException",
    "Or",
    "Parallel",
    "ParseError",
    "PlatformError",
    "RequestError",
    "ResponseError",
    "ResultLengthMismatch",
    "RpcClientError",
    "RuntimeException",
    "SchemaError",
    "Sequential",
    "SocketError",
    "SourceUnavailable",
    "SqlError",
    "SystemError",
    "TimeoutException",
    "UnknownException",
    "Unsupported",
];

/// Inputs of [`find_effect_findings`].
pub(super) struct EffectInput<'a> {
    pub(super) graph: &'a ModuleGraph,
    pub(super) modules: &'a [ModuleInfo],
    pub(super) resolved_modules: &'a [ResolvedModule],
    pub(super) declared_deps: &'a FxHashSet<String>,
    pub(super) public_api_entry_points: &'a FxHashSet<FileId>,
    pub(super) line_offsets_by_file: &'a LineOffsetsMap<'a>,
    pub(super) layers_enabled: bool,
    pub(super) errors_enabled: bool,
    pub(super) catch_tags_enabled: bool,
}

/// The raw findings of the three Effect rules.
#[derive(Default)]
pub(super) struct EffectFindings {
    pub(super) layers: Vec<UnusedEffectLayer>,
    pub(super) errors: Vec<UnusedTaggedError>,
    pub(super) catch_tags: Vec<UnproducedCatchTag>,
}

/// Run the Effect rules. Returns nothing unless the project declares `effect`.
///
/// `is_suppressed(file, line)` reports an `unused-tagged-error` suppression.
/// A suppressed error finding does not cover its `catchTag` handlers, so
/// `unproduced-catch-tag` can still report them.
pub(super) fn find_effect_findings(
    input: &EffectInput<'_>,
    is_suppressed: impl Fn(FileId, u32) -> bool,
) -> EffectFindings {
    let mut findings = EffectFindings::default();
    if !input.declared_deps.contains("effect") {
        return findings;
    }
    let facts: Vec<(FileId, &EffectFacts)> = input
        .modules
        .iter()
        .filter_map(|module| module.effect_facts.as_deref().map(|f| (module.file_id, f)))
        .collect();
    if facts.is_empty() {
        return findings;
    }
    let context = Context::build(input, &facts);
    if input.layers_enabled {
        findings.layers = find_unused_layers(&context);
    }
    let errors = find_unused_errors(&context, &is_suppressed);
    if input.errors_enabled {
        findings.errors = errors.findings;
    }
    if input.catch_tags_enabled {
        findings.catch_tags = find_unproduced_catch_tags(&context, &errors.covered_tags);
    }
    findings
}

struct Context<'a> {
    input: &'a EffectInput<'a>,
    facts: &'a [(FileId, &'a EffectFacts)],
    paths: FxHashMap<FileId, &'a Path>,
    reachable: FxHashSet<FileId>,
    /// Names that some module reads as a value through an import or a
    /// namespace member, after alias resolution.
    value_names: FxHashSet<String>,
    /// Names that some module reads in a type position through an import.
    type_names: FxHashSet<String>,
    /// Files whose every declaration counts as read: a namespace import used
    /// as a whole value, or a dynamic import target.
    whole_files: FxHashSet<FileId>,
    /// Exports that an entry module can expose, directly or through re-export
    /// chains. Code outside the project can use them, so they count as read.
    entry_exports: EntryExports,
    /// Layer keys that a reference outside every layer definition reads.
    layer_roots: FxHashSet<String>,
    /// Class names whose every static member counts as read: a module
    /// without Effect facts reads the class, so its member reads are not
    /// recorded precisely.
    static_wildcards: FxHashSet<String>,
    /// Re-export and local export aliases: exported name to original name.
    aliases: FxHashMap<String, Vec<String>>,
    /// Every caught tag, project-wide.
    caught_tags: FxHashSet<&'a str>,
}

impl<'a> Context<'a> {
    fn build(input: &'a EffectInput<'a>, facts: &'a [(FileId, &'a EffectFacts)]) -> Self {
        let paths = input
            .graph
            .modules
            .iter()
            .map(|module| (module.file_id, module.path.as_path()))
            .collect();
        let reachable = input
            .graph
            .modules
            .iter()
            .filter(|module| module.is_reachable())
            .map(|module| module.file_id)
            .collect();
        let with_facts: FxHashSet<FileId> = facts.iter().map(|(file_id, _)| *file_id).collect();
        let entry_exports = EntryExports::build(input.graph, input.public_api_entry_points);
        let mut context = Self {
            input,
            facts,
            paths,
            reachable,
            value_names: FxHashSet::default(),
            type_names: FxHashSet::default(),
            whole_files: FxHashSet::default(),
            entry_exports,
            layer_roots: FxHashSet::default(),
            static_wildcards: FxHashSet::default(),
            aliases: FxHashMap::default(),
            caught_tags: FxHashSet::default(),
        };
        for resolved in input.resolved_modules {
            context.credit_imports(resolved, with_facts.contains(&resolved.file_id));
        }
        context.collect_aliases();
        for (_, module_facts) in facts {
            context
                .layer_roots
                .extend(module_facts.layer_root_keys.iter().cloned());
            context.caught_tags.extend(
                module_facts
                    .caught_tags
                    .iter()
                    .map(|caught| caught.tag.as_str()),
            );
        }
        context.close_value_names_over_aliases();
        context
    }

    fn credit_imports(&mut self, resolved: &ResolvedModule, has_facts: bool) {
        let value_locals: FxHashSet<&str> = resolved
            .value_referenced_import_bindings
            .iter()
            .map(String::as_str)
            .collect();
        let type_locals: FxHashSet<&str> = resolved
            .type_referenced_import_bindings
            .iter()
            .map(String::as_str)
            .collect();
        for import in &resolved.resolved_imports {
            // A package import cannot name a project layer or error class.
            let Some(target) = import.target.internal_file_id() else {
                continue;
            };
            let local = import.info.local_name.as_str();
            match &import.info.imported_name {
                ImportedName::Named(name) => {
                    if type_locals.contains(local) {
                        self.type_names.insert(name.clone());
                    }
                    if !value_locals.contains(local) {
                        continue;
                    }
                    self.value_names.insert(name.clone());
                    if !has_facts {
                        self.layer_roots.insert(name.clone());
                        self.static_wildcards.insert(name.clone());
                    }
                    // A namespace re-export (`export * as errors`) imported by
                    // name reads its members through this binding.
                    self.credit_members(resolved, local, has_facts);
                }
                ImportedName::Namespace => {
                    if resolved.whole_object_uses.iter().any(|use_| use_ == local) {
                        self.whole_files.insert(target);
                    }
                    self.credit_members(resolved, local, has_facts);
                }
                ImportedName::Default | ImportedName::SideEffect => {}
            }
        }
        for dynamic in &resolved.resolved_dynamic_imports {
            if let Some(file_id) = dynamic.target.internal_file_id() {
                self.whole_files.insert(file_id);
            }
        }
    }

    fn credit_members(&mut self, resolved: &ResolvedModule, local: &str, has_facts: bool) {
        for access in resolved.member_accesses.iter() {
            if access.object != local {
                continue;
            }
            self.value_names.insert(access.member.clone());
            if !has_facts {
                self.layer_roots.insert(access.member.clone());
                self.static_wildcards.insert(access.member.clone());
            }
        }
    }

    fn collect_aliases(&mut self) {
        for resolved in self.input.resolved_modules {
            for re_export in &resolved.re_exports {
                let info = &re_export.info;
                if info.imported_name != info.exported_name && info.imported_name != "*" {
                    self.aliases
                        .entry(info.exported_name.clone())
                        .or_default()
                        .push(info.imported_name.clone());
                }
            }
        }
        for module in self.input.modules {
            for export in module.exports.iter() {
                let (ExportName::Named(name), Some(local)) = (&export.name, &export.local_name)
                else {
                    continue;
                };
                if name != local {
                    self.aliases
                        .entry(name.clone())
                        .or_default()
                        .push(local.clone());
                }
            }
        }
    }

    fn close_value_names_over_aliases(&mut self) {
        let value_names = std::mem::take(&mut self.value_names);
        self.value_names = self.resolve_aliases(value_names);
        let type_names = std::mem::take(&mut self.type_names);
        self.type_names = self.resolve_aliases(type_names);
        let layer_roots = std::mem::take(&mut self.layer_roots);
        self.layer_roots = self.resolve_aliases(layer_roots);
        let static_wildcards = std::mem::take(&mut self.static_wildcards);
        self.static_wildcards = self.resolve_aliases(static_wildcards);
    }

    /// Add the original name of every alias in `names`, also for a static
    /// key (`Alias.layer` adds `Original.layer`).
    fn resolve_aliases(&self, names: FxHashSet<String>) -> FxHashSet<String> {
        let mut out = names.clone();
        let mut queue: Vec<String> = names.into_iter().collect();
        while let Some(name) = queue.pop() {
            let (head, tail) = match name.split_once('.') {
                Some((head, tail)) => (head, Some(tail)),
                None => (name.as_str(), None),
            };
            let Some(originals) = self.aliases.get(head) else {
                continue;
            };
            for original in originals {
                let resolved = match tail {
                    Some(tail) => format!("{original}.{tail}"),
                    None => original.clone(),
                };
                if out.insert(resolved.clone()) {
                    queue.push(resolved);
                }
            }
        }
        out
    }

    /// Whether an entry module can expose `name` of `file_id`, under its own
    /// name or under an export alias (`export { Foo as Bar }`).
    fn is_entry_export(&self, file_id: FileId, name: &str) -> bool {
        self.entry_exports.contains(file_id, name)
            || self.aliases.iter().any(|(exported, originals)| {
                originals.iter().any(|original| original == name)
                    && self.entry_exports.contains(file_id, exported)
            })
    }

    fn path(&self, file_id: FileId) -> Option<&'a Path> {
        self.paths.get(&file_id).copied()
    }

    fn line_col(&self, file_id: FileId, span_start: u32) -> (u32, u32) {
        byte_offset_to_line_col(self.input.line_offsets_by_file, file_id, span_start)
    }
}

/// The exports that entry modules can expose. The walk follows re-export
/// edges from every entry module and public API entry. It over-approximates:
/// a star re-export exposes every name of its source, and a named re-export of
/// a file that also has star re-exports exposes the name in each star source.
/// A larger set only removes findings.
#[derive(Default)]
struct EntryExports {
    /// Files whose every export is exposed.
    all: FxHashSet<FileId>,
    /// Single exposed names in files that are not in `all`.
    named: FxHashSet<(FileId, String)>,
}

impl EntryExports {
    fn build(graph: &ModuleGraph, public_api_entry_points: &FxHashSet<FileId>) -> Self {
        let mut exposed = Self::default();
        let mut queue: Vec<(FileId, Option<String>)> = graph
            .modules
            .iter()
            .filter(|module| module.is_entry_point())
            .map(|module| (module.file_id, None))
            .chain(
                public_api_entry_points
                    .iter()
                    .map(|file_id| (*file_id, None)),
            )
            .collect();
        let modules_by_id: FxHashMap<FileId, usize> = graph
            .modules
            .iter()
            .enumerate()
            .map(|(index, module)| (module.file_id, index))
            .collect();
        while let Some((file_id, name)) = queue.pop() {
            if exposed.all.contains(&file_id) {
                continue;
            }
            match &name {
                None => {
                    exposed.all.insert(file_id);
                }
                Some(name) => {
                    if !exposed.named.insert((file_id, name.clone())) {
                        continue;
                    }
                }
            }
            let Some(&index) = modules_by_id.get(&file_id) else {
                continue;
            };
            for edge in &graph.modules[index].re_exports {
                let star = edge.imported_name == "*";
                match (&name, star) {
                    (None, true) => queue.push((edge.source_file, None)),
                    (None, false) => {
                        queue.push((edge.source_file, Some(edge.imported_name.clone())));
                    }
                    (Some(name), true) if edge.exported_name == *name => {
                        queue.push((edge.source_file, None));
                    }
                    (Some(name), true) => queue.push((edge.source_file, Some(name.clone()))),
                    (Some(name), false) if edge.exported_name == *name => {
                        queue.push((edge.source_file, Some(edge.imported_name.clone())));
                    }
                    (Some(_), false) => {}
                }
            }
        }
        exposed
    }

    fn contains(&self, file_id: FileId, name: &str) -> bool {
        self.all.contains(&file_id) || self.named.contains(&(file_id, name.to_owned()))
    }
}

// ── unused-effect-layer ─────────────────────────────────────────────

fn find_unused_layers(context: &Context<'_>) -> Vec<UnusedEffectLayer> {
    let mut nodes_by_key: FxHashMap<&str, Vec<(FileId, usize)>> = FxHashMap::default();
    for (file_id, facts) in context.facts {
        for (index, layer) in facts.layers.iter().enumerate() {
            nodes_by_key
                .entry(layer.key.as_str())
                .or_default()
                .push((*file_id, index));
        }
    }
    if nodes_by_key.is_empty() {
        return Vec::new();
    }

    let mut queue: Vec<String> = context.layer_roots.iter().cloned().collect();
    for (file_id, facts) in context.facts {
        for layer in &facts.layers {
            let owner = layer
                .key
                .split_once('.')
                .map_or(layer.key.as_str(), |(c, _)| c);
            let is_static = layer.key.contains('.');
            let public = layer.exported && context.is_entry_export(*file_id, owner);
            if public
                || context.whole_files.contains(file_id)
                || (is_static && context.static_wildcards.contains(owner))
            {
                queue.push(layer.key.clone());
            }
        }
    }

    let reached = reach(context, &nodes_by_key, queue);
    let mut consumers: FxHashMap<&str, Vec<&str>> = FxHashMap::default();
    for (_, facts) in context.facts {
        for layer in &facts.layers {
            for dependency in &layer.dependencies {
                consumers
                    .entry(layer_key_of(dependency, &nodes_by_key))
                    .or_default()
                    .push(layer.key.as_str());
            }
        }
    }

    let mut findings = Vec::new();
    for (file_id, facts) in context.facts {
        if !context.reachable.contains(file_id) {
            continue;
        }
        let Some(path) = context.path(*file_id) else {
            continue;
        };
        for layer in &facts.layers {
            if reached.contains(layer.key.as_str()) {
                continue;
            }
            let Some(dead_consumers) = consumers.get(layer.key.as_str()) else {
                continue;
            };
            let mut dead_consumers: Vec<String> = dead_consumers
                .iter()
                .map(|key| (*key).to_string())
                .collect();
            dead_consumers.sort_unstable();
            dead_consumers.dedup();
            let (line, col) = context.line_col(*file_id, layer.span_start);
            findings.push(UnusedEffectLayer {
                path: path.to_path_buf(),
                name: layer.key.clone(),
                kind: if layer.key.contains('.') {
                    EffectLayerKind::StaticMember
                } else {
                    EffectLayerKind::TopLevel
                },
                line,
                col,
                dead_consumers,
            });
        }
    }
    findings
}

/// The layer key a reference key reads. `FooLive.pipe` reads the const
/// layer `FooLive`; `Foo.layer` reads the static layer `Foo.layer`.
fn layer_key_of<'k>(key: &'k str, nodes_by_key: &FxHashMap<&str, Vec<(FileId, usize)>>) -> &'k str {
    if nodes_by_key.contains_key(key) {
        return key;
    }
    key.split_once('.').map_or(key, |(head, _)| head)
}

fn reach<'k>(
    context: &'k Context<'_>,
    nodes_by_key: &FxHashMap<&'k str, Vec<(FileId, usize)>>,
    mut queue: Vec<String>,
) -> FxHashSet<&'k str> {
    let facts_by_file: FxHashMap<FileId, &EffectFacts> = context.facts.iter().copied().collect();
    let mut reached: FxHashSet<&'k str> = FxHashSet::default();
    while let Some(key) = queue.pop() {
        let key = layer_key_of(&key, nodes_by_key);
        let Some((&node_key, nodes)) = nodes_by_key.get_key_value(key) else {
            continue;
        };
        if !reached.insert(node_key) {
            continue;
        }
        for (file_id, index) in nodes {
            if let Some(facts) = facts_by_file.get(file_id) {
                queue.extend(facts.layers[*index].dependencies.iter().cloned());
            }
        }
    }
    reached
}

// ── unused-tagged-error ─────────────────────────────────────────────

struct ErrorOutcome {
    findings: Vec<UnusedTaggedError>,
    /// Tags whose unused classes an `unused-tagged-error` finding reports.
    covered_tags: FxHashSet<String>,
}

fn error_is_produced(
    context: &Context<'_>,
    file_id: FileId,
    error: &fallow_types::extract::EffectTaggedError,
) -> bool {
    error.local_value_reference
        || context.value_names.contains(&error.name)
        || context.whole_files.contains(&file_id)
        || (error.exported && context.is_entry_export(file_id, &error.name))
}

fn find_unused_errors(
    context: &Context<'_>,
    is_suppressed: &impl Fn(FileId, u32) -> bool,
) -> ErrorOutcome {
    let catch_sites = catch_sites_by_tag(context);
    let mut outcome = ErrorOutcome {
        findings: Vec::new(),
        covered_tags: FxHashSet::default(),
    };
    for (file_id, facts) in context.facts {
        if !context.reachable.contains(file_id) {
            continue;
        }
        let Some(path) = context.path(*file_id) else {
            continue;
        };
        for error in &facts.tagged_errors {
            if error_is_produced(context, *file_id, error) {
                continue;
            }
            let referenced = error.local_type_reference
                || context.type_names.contains(&error.name)
                || context.caught_tags.contains(error.tag.as_str());
            if !referenced {
                continue;
            }
            let (line, col) = context.line_col(*file_id, error.span_start);
            if !context.input.errors_enabled || is_suppressed(*file_id, line) {
                continue;
            }
            outcome.covered_tags.insert(error.tag.clone());
            outcome.findings.push(UnusedTaggedError {
                path: path.to_path_buf(),
                name: error.name.clone(),
                tag: error.tag.clone(),
                line,
                col,
                catch_sites: catch_sites
                    .get(error.tag.as_str())
                    .cloned()
                    .unwrap_or_default(),
            });
        }
    }
    outcome
}

fn catch_sites_by_tag<'a>(context: &Context<'a>) -> FxHashMap<&'a str, Vec<EffectCatchSite>> {
    let mut sites: FxHashMap<&str, Vec<EffectCatchSite>> = FxHashMap::default();
    for (file_id, facts) in context.facts {
        if !context.reachable.contains(file_id) {
            continue;
        }
        let Some(path) = context.path(*file_id) else {
            continue;
        };
        for caught in &facts.caught_tags {
            let (line, col) = context.line_col(*file_id, caught.span_start);
            sites
                .entry(caught.tag.as_str())
                .or_default()
                .push(EffectCatchSite {
                    path: path.to_path_buf(),
                    line,
                    col,
                });
        }
    }
    for list in sites.values_mut() {
        list.sort_by(|a, b| {
            a.path
                .cmp(&b.path)
                .then(a.line.cmp(&b.line))
                .then(a.col.cmp(&b.col))
        });
    }
    sites
}

// ── unproduced-catch-tag ────────────────────────────────────────────

fn find_unproduced_catch_tags(
    context: &Context<'_>,
    covered_tags: &FxHashSet<String>,
) -> Vec<UnproducedCatchTag> {
    let mut produced: FxHashSet<&str> = FxHashSet::default();
    let mut classes_by_tag: FxHashMap<&str, Vec<&str>> = FxHashMap::default();
    for (file_id, facts) in context.facts {
        produced.extend(facts.literal_tags.iter().map(String::as_str));
        for error in &facts.tagged_errors {
            classes_by_tag
                .entry(error.tag.as_str())
                .or_default()
                .push(error.name.as_str());
            if error_is_produced(context, *file_id, error) {
                produced.insert(error.tag.as_str());
            }
        }
    }

    let mut findings = Vec::new();
    for (file_id, facts) in context.facts {
        if !context.reachable.contains(file_id) {
            continue;
        }
        let Some(path) = context.path(*file_id) else {
            continue;
        };
        for caught in &facts.caught_tags {
            let tag = caught.tag.as_str();
            if BUILTIN_TAGS.contains(&tag) || produced.contains(tag) || covered_tags.contains(tag) {
                continue;
            }
            let Some(classes) = classes_by_tag.get(tag) else {
                continue;
            };
            let mut candidate_classes: Vec<String> =
                classes.iter().map(|name| (*name).to_string()).collect();
            candidate_classes.sort_unstable();
            candidate_classes.dedup();
            let (line, col) = context.line_col(*file_id, caught.span_start);
            findings.push(UnproducedCatchTag {
                path: path.to_path_buf(),
                tag: caught.tag.clone(),
                line,
                col,
                candidate_classes,
            });
        }
    }
    findings
}