cargo-mend 0.21.0

Opinionated visibility auditing for Rust crates and workspaces
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
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
use std::collections::BTreeSet;
use std::ffi::OsStr;
use std::fs;
use std::path::Path;
use std::path::PathBuf;

use anyhow::Context;
use anyhow::Result;
use serde_json::from_str;

use super::StoredFinding;
use super::StoredPubUseFixFact;
use super::StoredReport;
use super::caller_aware;
use super::intersection;
use super::visibility_constraint;
use super::visibility_priority;
use crate::compiler::constants::FINDINGS_SCHEMA_VERSION;
use crate::compiler::constants::JSON_FILE_EXTENSION;
use crate::compiler::settings;
use crate::reporting::AllFeaturesCoverage;
use crate::reporting::CompilerWarningFacts;
use crate::reporting::Finding;
use crate::reporting::FixSupport;
use crate::reporting::ItemVisibility;
use crate::reporting::NarrowerScope;
use crate::reporting::PubUseFixFact;
use crate::reporting::Report;
use crate::reporting::ReportFacts;
use crate::reporting::ReportSummary;
use crate::selection::Selection;

/// Whether any stored report reached this run.
///
/// An empty `Report::findings` means the code is clean only when at least one
/// `StoredReport` matched the selection and passed `stored_report_is_compatible`.
/// With none, the compiler examined no code at all — cargo found nothing to
/// rebuild, so the driver never ran, and no cached report survived the
/// compatibility check — and the empty findings list is indistinguishable from a
/// clean crate without this.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(in crate::compiler) enum AnalysisEvidence {
    Present,
    Absent,
}

/// `load_report`'s result: the merged report, and whether anything produced it.
pub(in crate::compiler) struct LoadedReport {
    pub report:            Report,
    pub analysis_evidence: AnalysisEvidence,
}

/// The child declaration a `--fix pub-use` narrowing would rewrite.
///
/// The driver writes a `StoredPubUseFixFact` next to the finding that
/// authorizes it — `SuspiciousPub` on the child declaration, or
/// `InternalParentPubUseFacade` on the parent `pub use` line. The four
/// suppression stages inside `reconcile_cross_target_reports` can drop that
/// finding afterwards; a fact whose site no longer appears in
/// `StoredReport::findings` would let the fixer apply a narrowing the
/// cross-target analysis already rejected.
#[derive(PartialEq, Eq, PartialOrd, Ord)]
struct PubUseNarrowingSite {
    path:      String,
    line:      usize,
    item_name: String,
}

impl PubUseNarrowingSite {
    /// The two lines a single `pub use` fix can be advertised on. One edit,
    /// two diagnostics: `suspicious_pub` reports it on the child declaration,
    /// `internal_parent_pub_use_facade` reports it on the parent `pub use`
    /// line. Either surviving finding keeps the fact.
    fn advertising_sites(fact: &StoredPubUseFixFact) -> [Self; 2] {
        [
            Self {
                path:      fact.child_path.clone(),
                line:      fact.child_line,
                item_name: fact.child_item_name.clone(),
            },
            Self {
                path:      fact.parent_path.clone(),
                line:      fact.parent_line,
                item_name: fact.child_item_name.clone(),
            },
        ]
    }
}

pub(in crate::compiler) fn load_report(
    findings_dir: &Path,
    selection: &Selection,
    config_fingerprint: &str,
) -> Result<LoadedReport> {
    let selected_roots: Vec<PathBuf> = selection.package_roots.clone();
    let selected_root_strings: Vec<String> = selected_roots
        .iter()
        .map(|root| root.to_string_lossy().into_owned())
        .collect();
    let selected_canonical_roots: Vec<PathBuf> = selected_roots
        .iter()
        .filter_map(|root| fs::canonicalize(root).ok())
        .collect();
    let mut matched_reports: Vec<StoredReport> = Vec::new();
    let mut all_features_coverage = AllFeaturesCoverage::Superset;

    for entry in fs::read_dir(findings_dir).with_context(|| {
        format!(
            "failed to read findings directory {}",
            findings_dir.display()
        )
    })? {
        let Ok(entry) = entry else {
            all_features_coverage = AllFeaturesCoverage::NotGuaranteed;
            continue;
        };
        if entry.path().extension().and_then(OsStr::to_str) != Some(JSON_FILE_EXTENSION) {
            continue;
        }

        let Ok(text) = fs::read_to_string(entry.path()) else {
            all_features_coverage = AllFeaturesCoverage::NotGuaranteed;
            continue;
        };
        let Ok(stored) = from_str::<StoredReport>(&text) else {
            all_features_coverage = AllFeaturesCoverage::NotGuaranteed;
            continue;
        };
        if !stored_matches_selected_root(
            &stored,
            &selected_roots,
            &selected_root_strings,
            &selected_canonical_roots,
        ) {
            continue;
        }
        if !stored_report_is_compatible(&stored, config_fingerprint) {
            all_features_coverage = AllFeaturesCoverage::NotGuaranteed;
            continue;
        }
        all_features_coverage = all_features_coverage.merge(stored.all_features_coverage);
        matched_reports.push(stored);
    }

    reconcile_cross_target_reports(&mut matched_reports);
    discard_fix_facts_for_suppressed_findings(&mut matched_reports);

    let analysis_evidence = if matched_reports.is_empty() {
        all_features_coverage = AllFeaturesCoverage::default();
        AnalysisEvidence::Absent
    } else {
        AnalysisEvidence::Present
    };

    let mut findings = Vec::new();
    let mut pub_use_fix_facts = Vec::new();
    for stored in matched_reports {
        extend_report_from_stored(
            &mut findings,
            &mut pub_use_fix_facts,
            stored,
            selection.analysis_root.as_path(),
        );
    }

    sort_and_dedup_findings(&mut findings);
    sort_and_dedup_pub_use_fix_facts(&mut pub_use_fix_facts);

    Ok(LoadedReport {
        report: Report {
            root: selection_root_string(selection.analysis_root.as_path()),
            summary: ReportSummary::default(),
            findings,
            facts: ReportFacts {
                pub_use_fix_facts: pub_use_fix_facts.into(),
                all_features_coverage,
                compiler_warning_facts: CompilerWarningFacts::None,
            },
        },
        analysis_evidence,
    })
}

fn reconcile_cross_target_reports(reports: &mut [StoredReport]) {
    intersection::apply_cross_compilation_intersection(reports);
    let callers = caller_aware::apply_caller_aware_suppression(reports);
    visibility_constraint::reconcile_visibility_constraints(reports, &callers);
    visibility_priority::apply_visibility_narrowing_priority(reports);
}

/// Drops every stored fix fact whose authorizing finding did not survive
/// `reconcile_cross_target_reports`. Pruning happens once, at that boundary:
/// the surviving finding set is settled the moment those four stages return,
/// and the facts are still untouched here.
fn discard_fix_facts_for_suppressed_findings(reports: &mut [StoredReport]) {
    for report in reports {
        let narrowing_sites: BTreeSet<PubUseNarrowingSite> = report
            .findings
            .iter()
            .filter(|finding| finding.fix_support == FixSupport::PubUse)
            .filter_map(|finding| {
                finding.item.as_deref().map(|item| PubUseNarrowingSite {
                    path:      finding.path.clone(),
                    line:      finding.line,
                    // `StoredFinding::item` is a rendering; the fact stores the
                    // bare name, so recover it the sanctioned way.
                    item_name: StoredFinding::item_name(item).to_string(),
                })
            })
            .collect();
        report.pub_use_fix_facts.retain(|fact| {
            PubUseNarrowingSite::advertising_sites(fact)
                .iter()
                .any(|site| narrowing_sites.contains(site))
        });
    }
}

fn sort_and_dedup_findings(findings: &mut Vec<Finding>) {
    findings.sort_by(|a, b| {
        (
            a.severity,
            &a.path,
            a.line,
            a.column,
            &a.diagnostic_code,
            &a.item,
            &a.message,
        )
            .cmp(&(
                b.severity,
                &b.path,
                b.line,
                b.column,
                &b.diagnostic_code,
                &b.item,
                &b.message,
            ))
    });
    findings.dedup_by(|a, b| {
        a.severity == b.severity
            && a.diagnostic_code == b.diagnostic_code
            && a.path == b.path
            && a.line == b.line
            && a.column == b.column
            && a.message == b.message
            && a.item == b.item
    });
    retain_one_restricted_annotation_fix_per_site(findings);
}

/// Leaves one `restricted_annotation` fix advertised per declaration site.
///
/// `suspicious_pub` words its message differently for a binary than for a
/// library, so a declaration compiled into both targets keeps two findings
/// through the dedup above — correctly, since they are two distinct
/// diagnostics. Both would otherwise claim the fix, and the summary would
/// advertise two available fixes where `fixes::restricted_annotation` rewrites
/// the declaration once. The findings are already sorted by position, so the
/// duplicates of a site are adjacent.
fn retain_one_restricted_annotation_fix_per_site(findings: &mut [Finding]) {
    let sites =
        findings.chunk_by_mut(|a, b| a.path == b.path && a.line == b.line && a.column == b.column);
    for site in sites {
        site.iter_mut()
            .filter(|finding| finding.fix_support == FixSupport::RestrictedAnnotation)
            .skip(1)
            .for_each(|finding| finding.fix_support = FixSupport::None);
    }
}

fn sort_and_dedup_pub_use_fix_facts(pub_use_fix_facts: &mut Vec<PubUseFixFact>) {
    pub_use_fix_facts.sort_by(|a, b| {
        (
            &a.child_path,
            a.child_line,
            &a.child_item_name,
            &a.parent_path,
            a.parent_line,
            &a.child_module,
        )
            .cmp(&(
                &b.child_path,
                b.child_line,
                &b.child_item_name,
                &b.parent_path,
                b.parent_line,
                &b.child_module,
            ))
    });
    pub_use_fix_facts.dedup_by(|a, b| {
        a.child_path == b.child_path
            && a.child_line == b.child_line
            && a.child_item_name == b.child_item_name
            && a.parent_path == b.parent_path
            && a.parent_line == b.parent_line
            && a.child_module == b.child_module
    });
}

fn stored_report_is_compatible(stored: &StoredReport, config_fingerprint: &str) -> bool {
    stored.version == FINDINGS_SCHEMA_VERSION
        && stored.analysis_fingerprint == settings::current_analysis_fingerprint()
        && stored.config_fingerprint == config_fingerprint
        && stored_crate_root_exists(stored)
}

fn stored_crate_root_exists(stored: &StoredReport) -> bool {
    stored.crate_root_file.is_empty() || {
        let crate_root = Path::new(&stored.crate_root_file);
        if crate_root.is_absolute() {
            crate_root.exists()
        } else {
            Path::new(&stored.package_root).join(crate_root).exists()
        }
    }
}

fn stored_matches_selected_root(
    stored: &StoredReport,
    selected_roots: &[PathBuf],
    selected_root_strings: &[String],
    selected_canonical_roots: &[PathBuf],
) -> bool {
    selected_root_strings
        .iter()
        .any(|root| root == &stored.package_root)
        || fs::canonicalize(Path::new(&stored.package_root)).is_ok_and(|stored_root| {
            selected_canonical_roots
                .iter()
                .any(|selected_root| selected_root == &stored_root)
        })
        || (stored.package_root.is_empty() && selected_roots.len() == 1)
}

fn extend_report_from_stored(
    findings: &mut Vec<Finding>,
    pub_use_fix_facts: &mut Vec<PubUseFixFact>,
    stored: StoredReport,
    analysis_root: &Path,
) {
    for finding in stored.findings {
        findings.push(Finding {
            severity:        finding.severity,
            diagnostic_code: finding.diagnostic_code,
            path:            relativize_path(&finding.path, analysis_root),
            line:            finding.line,
            column:          finding.column,
            highlight_len:   finding.highlight_len,
            source_line:     finding.source_line,
            item:            finding.item,
            message:         finding.message,
            suggestion:      finding.suggestion,
            fix_support:     finding.fix_support,
            related:         finding
                .related
                .map(|related| relativize_path(&related, analysis_root)),
            item_visibility: ItemVisibility {
                written:        finding.visibility_annotation.into(),
                narrower_scope: NarrowerScope::resolve(
                    finding.narrower_scope_def_path,
                    finding.fix_support,
                    finding.exact_boundary_spelling,
                ),
            },
        });
    }
    for fact in stored.pub_use_fix_facts {
        pub_use_fix_facts.push(PubUseFixFact {
            child_path:      relativize_path(&fact.child_path, analysis_root),
            child_line:      fact.child_line,
            child_item_name: fact.child_item_name,
            parent_path:     relativize_path(&fact.parent_path, analysis_root),
            parent_line:     fact.parent_line,
            child_module:    fact.child_module,
        });
    }
}

fn selection_root_string(root: &Path) -> String { root.display().to_string() }

fn relativize_path(path: &str, analysis_root: &Path) -> String {
    let absolute = Path::new(path);
    absolute.strip_prefix(analysis_root).map_or_else(
        |_| path.to_string(),
        |relative| relative.to_string_lossy().replace('\\', "/"),
    )
}

#[cfg(test)]
#[allow(
    clippy::expect_used,
    reason = "tests should panic on unexpected values"
)]
mod tests {
    use std::fs;
    use std::path::Path;
    use std::path::PathBuf;

    use serde_json::to_vec_pretty;
    use tempfile::TempDir;
    use tempfile::tempdir;

    use super::AnalysisEvidence;
    use super::load_report;
    use crate::compiler::constants::FINDINGS_SCHEMA_VERSION;
    use crate::compiler::persistence::StoredFinding;
    use crate::compiler::persistence::StoredPubUseFixFact;
    use crate::compiler::persistence::StoredReport;
    use crate::compiler::settings;
    use crate::config::DiagnosticCode;
    use crate::reporting::AllFeaturesCoverage;
    use crate::reporting::CompilerWarningFacts;
    use crate::reporting::ExactBoundarySpelling;
    use crate::reporting::FixSupport;
    use crate::reporting::Severity;
    use crate::selection::Selection;
    use crate::selection::SelectionScope;

    const CONFIG_FINGERPRINT: &str = "config-fingerprint";

    struct PersistenceFixture {
        temp:         TempDir,
        findings_dir: PathBuf,
        package_root: PathBuf,
        crate_root:   PathBuf,
    }

    impl PersistenceFixture {
        fn new() -> Self {
            let temp = tempdir().expect("create persistence fixture");
            let package_root = temp.path().join("package");
            let source_dir = package_root.join("src");
            fs::create_dir_all(&source_dir).expect("create package src dir");
            let crate_root = source_dir.join("lib.rs");
            fs::write(&crate_root, "pub fn item() {}\n").expect("write crate root");
            let findings_dir = temp.path().join("findings");
            fs::create_dir_all(&findings_dir).expect("create findings dir");
            Self {
                temp,
                findings_dir,
                package_root,
                crate_root,
            }
        }

        fn selection(&self) -> Selection {
            self.selection_with_roots(vec![self.package_root.clone()])
        }

        fn selection_with_roots(&self, package_roots: Vec<PathBuf>) -> Selection {
            Selection {
                manifest_path: self.package_root.join("Cargo.toml"),
                manifest_dir: self.package_root.clone(),
                workspace_root: self.package_root.clone(),
                target_directory: self.temp.path().join("target"),
                analysis_root: self.package_root.clone(),
                scope: SelectionScope::SinglePackage,
                package_roots,
                packages: Vec::new(),
            }
        }

        fn write_report(&self, file_name: &str, report: &StoredReport) {
            fs::write(
                self.findings_dir.join(file_name),
                to_vec_pretty(report).expect("serialize stored report"),
            )
            .expect("write stored report");
        }

        fn write_malformed_json(&self, file_name: &str) {
            fs::write(self.findings_dir.join(file_name), b"{ not json")
                .expect("write malformed report");
        }

        fn report_with_findings(&self, findings: Vec<StoredFinding>) -> StoredReport {
            StoredReport {
                version: FINDINGS_SCHEMA_VERSION,
                analysis_fingerprint: settings::current_analysis_fingerprint(),
                scope_fingerprint: "scope".to_string(),
                package_root: self.package_root.to_string_lossy().into_owned(),
                crate_root_file: self.crate_root.to_string_lossy().into_owned(),
                config_fingerprint: CONFIG_FINGERPRINT.to_string(),
                source_files: Vec::new(),
                findings,
                visibility_constraints: Vec::new(),
                pub_use_fix_facts: Vec::new(),
                all_features_coverage: AllFeaturesCoverage::default(),
                compiler_warning_facts: CompilerWarningFacts::None,
                use_sites: Vec::new(),
            }
        }
    }

    #[test]
    fn malformed_json_prevents_all_features_coverage() {
        let fixture = PersistenceFixture::new();
        let finding = stored_finding(
            DiagnosticCode::OverbroadPubCrate,
            &fixture.crate_root,
            "item",
            1,
        );
        let mut report = fixture.report_with_findings(vec![finding]);
        report.all_features_coverage = AllFeaturesCoverage::Superset;
        fixture.write_malformed_json("broken.json");
        fixture.write_report("valid.json", &report);

        let loaded = load_report(
            &fixture.findings_dir,
            &fixture.selection(),
            CONFIG_FINGERPRINT,
        )
        .expect("load report");

        assert_eq!(loaded.report.findings.len(), 1);
        assert_eq!(
            loaded.report.facts.all_features_coverage,
            AllFeaturesCoverage::NotGuaranteed
        );
    }

    #[test]
    fn a_findings_directory_with_no_report_is_not_a_clean_crate() {
        let fixture = PersistenceFixture::new();

        let loaded = load_report(
            &fixture.findings_dir,
            &fixture.selection(),
            CONFIG_FINGERPRINT,
        )
        .expect("load report");

        assert!(loaded.report.findings.is_empty());
        assert_eq!(
            loaded.analysis_evidence,
            AnalysisEvidence::Absent,
            "no stored report means nothing was analyzed, not that the crate is clean"
        );
    }

    #[test]
    fn wrong_schema_or_fingerprint_reports_are_rejected() {
        let fixture = PersistenceFixture::new();
        let mut wrong_schema = fixture.report_with_findings(vec![stored_finding(
            DiagnosticCode::OverbroadPubCrate,
            &fixture.crate_root,
            "item",
            1,
        )]);
        wrong_schema.pub_use_fix_facts.push(StoredPubUseFixFact {
            child_path:      fixture.crate_root.to_string_lossy().into_owned(),
            child_line:      2,
            child_item_name: "Child".to_string(),
            parent_path:     fixture.crate_root.to_string_lossy().into_owned(),
            parent_line:     3,
            child_module:    "child".to_string(),
        });
        wrong_schema.version = FINDINGS_SCHEMA_VERSION - 1;
        let mut wrong_analysis = fixture.report_with_findings(vec![stored_finding(
            DiagnosticCode::OverbroadPubCrate,
            &fixture.crate_root,
            "item",
            1,
        )]);
        wrong_analysis.analysis_fingerprint = "old-analysis".to_string();
        let mut wrong_config = fixture.report_with_findings(vec![stored_finding(
            DiagnosticCode::OverbroadPubCrate,
            &fixture.crate_root,
            "item",
            1,
        )]);
        wrong_config.config_fingerprint = "old-config".to_string();
        fixture.write_report("schema.json", &wrong_schema);
        fixture.write_report("analysis.json", &wrong_analysis);
        fixture.write_report("config.json", &wrong_config);

        let loaded = load_report(
            &fixture.findings_dir,
            &fixture.selection(),
            CONFIG_FINGERPRINT,
        )
        .expect("load report");

        assert!(loaded.report.findings.is_empty());
        assert!(
            loaded
                .report
                .facts
                .pub_use_fix_facts
                .iter()
                .next()
                .is_none()
        );
        assert_eq!(loaded.analysis_evidence, AnalysisEvidence::Absent);
    }

    #[test]
    fn missing_crate_root_report_is_rejected() {
        let fixture = PersistenceFixture::new();
        let finding = stored_finding(
            DiagnosticCode::OverbroadPubCrate,
            &fixture.crate_root,
            "item",
            1,
        );
        let mut report = fixture.report_with_findings(vec![finding]);
        report.crate_root_file = "src/missing.rs".to_string();
        fixture.write_report("missing-root.json", &report);

        let loaded = load_report(
            &fixture.findings_dir,
            &fixture.selection(),
            CONFIG_FINGERPRINT,
        )
        .expect("load report");

        assert!(loaded.report.findings.is_empty());
        assert_eq!(loaded.analysis_evidence, AnalysisEvidence::Absent);
    }

    #[test]
    fn canonical_selected_roots_are_accepted() {
        let fixture = PersistenceFixture::new();
        let finding = stored_finding(
            DiagnosticCode::OverbroadPubCrate,
            &fixture.crate_root,
            "item",
            1,
        );
        let report = fixture.report_with_findings(vec![finding]);
        fixture.write_report("canonical-root.json", &report);
        let selected_root = fixture.package_root.join("src").join("..");
        let selection = fixture.selection_with_roots(vec![selected_root]);

        let loaded = load_report(&fixture.findings_dir, &selection, CONFIG_FINGERPRINT)
            .expect("load report");

        assert_eq!(loaded.report.findings.len(), 1);
    }

    #[test]
    fn empty_package_root_compatibility_is_retained() {
        let fixture = PersistenceFixture::new();
        let finding = stored_finding(
            DiagnosticCode::OverbroadPubCrate,
            &fixture.crate_root,
            "item",
            1,
        );
        let mut report = fixture.report_with_findings(vec![finding]);
        report.package_root.clear();
        report.crate_root_file.clear();
        fixture.write_report("legacy-root.json", &report);

        let loaded = load_report(
            &fixture.findings_dir,
            &fixture.selection(),
            CONFIG_FINGERPRINT,
        )
        .expect("load report");

        assert_eq!(loaded.report.findings.len(), 1);
    }

    #[test]
    fn serialized_driver_report_loads_back_through_load_report() {
        let fixture = PersistenceFixture::new();
        // The fact rides with the finding that authorizes it: same file, same
        // line, same item name.
        let mut finding = stored_finding(
            DiagnosticCode::SuspiciousPub,
            &fixture.crate_root,
            "Child",
            2,
        );
        finding.item = Some(StoredFinding::render_item("struct", "Child"));
        finding.fix_support = FixSupport::PubUse;
        let mut report = fixture.report_with_findings(vec![finding]);
        report.pub_use_fix_facts.push(StoredPubUseFixFact {
            child_path:      fixture.crate_root.to_string_lossy().into_owned(),
            child_line:      2,
            child_item_name: "Child".to_string(),
            parent_path:     fixture.crate_root.to_string_lossy().into_owned(),
            parent_line:     3,
            child_module:    "child".to_string(),
        });
        fixture.write_report("driver-report.json", &report);

        let loaded = load_report(
            &fixture.findings_dir,
            &fixture.selection(),
            CONFIG_FINGERPRINT,
        )
        .expect("load report");
        let facts = loaded
            .report
            .facts
            .pub_use_fix_facts
            .iter()
            .collect::<Vec<_>>();

        assert_eq!(loaded.analysis_evidence, AnalysisEvidence::Present);
        assert_eq!(loaded.report.findings.len(), 1);
        assert_eq!(loaded.report.findings[0].path, "src/lib.rs");
        assert_eq!(facts.len(), 1);
        assert_eq!(facts[0].child_path, "src/lib.rs");
    }

    #[test]
    fn suppressed_finding_leaves_no_applicable_pub_use_fix_fact() {
        let fixture = PersistenceFixture::new();
        let mut suspicious = stored_finding(
            DiagnosticCode::SuspiciousPub,
            &fixture.crate_root,
            "Child",
            2,
        );
        suspicious.item = Some(StoredFinding::render_item("struct", "Child"));
        suspicious.fix_support = FixSupport::PubUse;
        // `apply_visibility_narrowing_priority` drops a `suspicious_pub`
        // finding that shares its path/line/column with an `unused_pub` one.
        let unused = stored_finding(DiagnosticCode::UnusedPub, &fixture.crate_root, "Child", 2);
        let mut report = fixture.report_with_findings(vec![suspicious, unused]);
        report.pub_use_fix_facts.push(StoredPubUseFixFact {
            child_path:      fixture.crate_root.to_string_lossy().into_owned(),
            child_line:      2,
            child_item_name: "Child".to_string(),
            parent_path:     fixture.crate_root.to_string_lossy().into_owned(),
            parent_line:     3,
            child_module:    "child".to_string(),
        });
        fixture.write_report("suppressed.json", &report);

        let loaded = load_report(
            &fixture.findings_dir,
            &fixture.selection(),
            CONFIG_FINGERPRINT,
        )
        .expect("load report");

        assert_eq!(loaded.report.findings.len(), 1);
        assert_eq!(
            loaded.report.findings[0].diagnostic_code,
            DiagnosticCode::UnusedPub
        );
        assert!(
            loaded
                .report
                .facts
                .pub_use_fix_facts
                .iter()
                .next()
                .is_none(),
            "a suppressed finding must not leave its pub-use fix fact behind"
        );
    }

    #[test]
    fn surviving_finding_keeps_its_pub_use_fix_fact() {
        let fixture = PersistenceFixture::new();
        let mut suspicious = stored_finding(
            DiagnosticCode::SuspiciousPub,
            &fixture.crate_root,
            "Child",
            2,
        );
        suspicious.item = Some(StoredFinding::render_item("struct", "Child"));
        suspicious.fix_support = FixSupport::PubUse;
        let mut report = fixture.report_with_findings(vec![suspicious]);
        report.pub_use_fix_facts.push(StoredPubUseFixFact {
            child_path:      fixture.crate_root.to_string_lossy().into_owned(),
            child_line:      2,
            child_item_name: "Child".to_string(),
            parent_path:     fixture.crate_root.to_string_lossy().into_owned(),
            parent_line:     3,
            child_module:    "child".to_string(),
        });
        fixture.write_report("surviving.json", &report);

        let loaded = load_report(
            &fixture.findings_dir,
            &fixture.selection(),
            CONFIG_FINGERPRINT,
        )
        .expect("load report");

        assert_eq!(loaded.report.findings.len(), 1);
        assert_eq!(loaded.report.facts.pub_use_fix_facts.iter().count(), 1);
    }

    fn stored_finding(
        diagnostic_code: DiagnosticCode,
        path: &Path,
        item: &str,
        line: usize,
    ) -> StoredFinding {
        StoredFinding {
            severity: Severity::Warning,
            diagnostic_code,
            path: path.to_string_lossy().into_owned(),
            line,
            column: 1,
            highlight_len: 3,
            source_line: "pub fn item() {}".to_string(),
            item: Some(item.to_string()),
            message: format!("{item} should change visibility"),
            suggestion: Some("use narrower visibility".to_string()),
            fix_support: FixSupport::None,
            related: None,
            visibility_annotation: None,
            item_def_path: None,
            narrower_scope_def_path: None,
            exact_boundary_spelling: ExactBoundarySpelling::CratePath,
        }
    }
}