cargo-mend 0.19.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
use serde::Deserialize;
use serde::Serialize;
use serde::Serializer;

use super::constants::HINT_ERROR_FIXABLE_WITH_FIX;
use super::constants::HINT_ERROR_FIXABLE_WITH_FIX_PUB_USE;
use super::constants::HINT_WARNING_FIXABLE_WITH_FIX;
use super::constants::HINT_WARNING_FIXABLE_WITH_FIX_PUB_USE;
use crate::config::DiagnosticCode;
use crate::constants::HELP_URL_BASE;

// --- FixSupport (folded from former fix_support.rs) ---

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "snake_case")]
pub(crate) enum FixSupport {
    #[default]
    None,
    ShortenImport,
    PreferModuleImport,
    InlinePathQualifiedType,
    #[serde(rename = "fix_pub_use")]
    PubUse,
    NeedsManualPubUseCleanup,
    BlockedByPrivateImport,
    InternalParentFacade,
    UnusedPub,
    NarrowToPubCrate,
    RestrictedAnnotation,
    #[serde(rename = "fix_field_visibility")]
    FieldVisibility,
    #[serde(rename = "fix_imports_at_top")]
    ImportsAtTop,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum FixSummaryBucket {
    Standard,
    PubUse,
}

impl FixSupport {
    const fn note(self, severity: Severity) -> Option<&'static str> {
        match (severity, self.summary_bucket()) {
            (_, None) => None,
            (Severity::Warning, Some(FixSummaryBucket::Standard)) => {
                Some(HINT_WARNING_FIXABLE_WITH_FIX)
            },
            (Severity::Warning, Some(FixSummaryBucket::PubUse)) => {
                Some(HINT_WARNING_FIXABLE_WITH_FIX_PUB_USE)
            },
            (Severity::Error, Some(FixSummaryBucket::Standard)) => {
                Some(HINT_ERROR_FIXABLE_WITH_FIX)
            },
            (Severity::Error, Some(FixSummaryBucket::PubUse)) => {
                Some(HINT_ERROR_FIXABLE_WITH_FIX_PUB_USE)
            },
        }
    }

    pub(crate) const fn summary_bucket(self) -> Option<FixSummaryBucket> {
        match self {
            Self::None
            | Self::NeedsManualPubUseCleanup
            | Self::BlockedByPrivateImport
            | Self::InternalParentFacade => None,
            Self::ShortenImport
            | Self::PreferModuleImport
            | Self::InlinePathQualifiedType
            | Self::UnusedPub
            | Self::NarrowToPubCrate
            | Self::RestrictedAnnotation
            | Self::FieldVisibility
            | Self::ImportsAtTop => Some(FixSummaryBucket::Standard),
            Self::PubUse => Some(FixSummaryBucket::PubUse),
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub(crate) enum Severity {
    Error,
    Warning,
}

#[derive(Debug, Clone, Copy)]
enum DetailMode {
    None,
    MessageAndRelated,
}

#[derive(Debug, Clone, Copy)]
enum HeadlineSource {
    Static(&'static str),
    FindingMessage { fallback: &'static str },
}

#[derive(Debug, Clone, Copy)]
pub(super) struct DiagnosticSpec {
    headline:        HeadlineSource,
    pub inline_help: Option<&'static str>,
    pub help_anchor: &'static str,
    detail_mode:     DetailMode,
    pub fix_support: FixSupport,
}

static OVERBROAD_PUB_CRATE: DiagnosticSpec = DiagnosticSpec {
    headline:    HeadlineSource::FindingMessage {
        fallback: "`pub(crate)` is broader than required",
    },
    inline_help: None,
    help_anchor: "overbroad-pub-crate",
    detail_mode: DetailMode::None,
    fix_support: FixSupport::None,
};
static FORBIDDEN_PUB_IN_CRATE: DiagnosticSpec = DiagnosticSpec {
    headline:    HeadlineSource::FindingMessage {
        fallback: "use of `pub(in crate::...)` is forbidden by policy",
    },
    inline_help: None,
    help_anchor: "forbidden-pub-in-crate",
    detail_mode: DetailMode::MessageAndRelated,
    fix_support: FixSupport::None,
};
static REVIEW_PUB_MOD: DiagnosticSpec = DiagnosticSpec {
    headline:    HeadlineSource::Static("`pub mod` requires explicit review or allowlisting"),
    inline_help: None,
    help_anchor: "review-pub-mod",
    detail_mode: DetailMode::None,
    fix_support: FixSupport::None,
};
static SUSPICIOUS_PUB: DiagnosticSpec = DiagnosticSpec {
    headline:    HeadlineSource::Static("`pub` is broader than this nested module boundary"),
    inline_help: None,
    help_anchor: "suspicious-pub",
    detail_mode: DetailMode::MessageAndRelated,
    fix_support: FixSupport::None,
};
static UNUSED_PUB: DiagnosticSpec = DiagnosticSpec {
    headline:    HeadlineSource::Static("`pub` item is not used outside its defining module"),
    inline_help: Some("consider removing `pub`"),
    help_anchor: "unused-pub",
    detail_mode: DetailMode::MessageAndRelated,
    fix_support: FixSupport::UnusedPub,
};
static PREFER_MODULE_IMPORT: DiagnosticSpec = DiagnosticSpec {
    headline:    HeadlineSource::Static("function import should use module-qualified form"),
    inline_help: None,
    help_anchor: "prefer-module-import",
    detail_mode: DetailMode::MessageAndRelated,
    fix_support: FixSupport::PreferModuleImport,
};
static INLINE_PATH_QUALIFIED_TYPE: DiagnosticSpec = DiagnosticSpec {
    headline:    HeadlineSource::Static("inline path-qualified type should use a `use` import"),
    inline_help: None,
    help_anchor: "inline-path-qualified-type",
    detail_mode: DetailMode::MessageAndRelated,
    fix_support: FixSupport::InlinePathQualifiedType,
};
static SHORTEN_LOCAL_CRATE_IMPORT: DiagnosticSpec = DiagnosticSpec {
    headline:    HeadlineSource::Static(
        "crate-relative import can be shortened to a local-relative import",
    ),
    inline_help: None,
    help_anchor: "shorten-local-crate-import",
    detail_mode: DetailMode::MessageAndRelated,
    fix_support: FixSupport::ShortenImport,
};
static REPLACE_DEEP_SUPER_IMPORT: DiagnosticSpec = DiagnosticSpec {
    headline:    HeadlineSource::Static("deep `super::` chain should use a `crate::` path"),
    inline_help: None,
    help_anchor: "replace-deep-super-import",
    detail_mode: DetailMode::MessageAndRelated,
    fix_support: FixSupport::ShortenImport,
};
static WILDCARD_PARENT_PUB_USE: DiagnosticSpec = DiagnosticSpec {
    headline:    HeadlineSource::Static("parent module `pub use *` should be explicit"),
    inline_help: Some("consider re-exporting explicit items instead of `*`"),
    help_anchor: "wildcard-parent-pub-use",
    detail_mode: DetailMode::None,
    fix_support: FixSupport::None,
};
static INTERNAL_PARENT_PUB_USE_FACADE: DiagnosticSpec = DiagnosticSpec {
    headline:    HeadlineSource::FindingMessage {
        fallback: "parent module re-export is acting as an internal facade",
    },
    inline_help: Some(
        "consider removing this parent facade and importing the item from its defining child module",
    ),
    help_anchor: "internal-parent-pub-use-facade",
    detail_mode: DetailMode::MessageAndRelated,
    fix_support: FixSupport::InternalParentFacade,
};
static NARROW_TO_PUB_CRATE: DiagnosticSpec = DiagnosticSpec {
    headline:    HeadlineSource::Static(
        "`pub` exceeds the item's effective reach — use `pub(crate)`",
    ),
    inline_help: Some("consider using: `pub(crate)`"),
    help_anchor: "narrow-to-pub-crate",
    detail_mode: DetailMode::MessageAndRelated,
    fix_support: FixSupport::NarrowToPubCrate,
};
static FIELD_VISIBILITY_WIDER_THAN_TYPE: DiagnosticSpec = DiagnosticSpec {
    headline:    HeadlineSource::Static("field visibility is wider than its containing type"),
    inline_help: None,
    help_anchor: "field-visibility-wider-than-type",
    detail_mode: DetailMode::MessageAndRelated,
    fix_support: FixSupport::FieldVisibility,
};
static IMPORTS_AT_TOP: DiagnosticSpec = DiagnosticSpec {
    headline:    HeadlineSource::Static(
        "`use` statement should live at the top of the file or inline module",
    ),
    inline_help: None,
    help_anchor: "imports-at-top",
    detail_mode: DetailMode::MessageAndRelated,
    fix_support: FixSupport::ImportsAtTop,
};

/// The visibility annotation the compiler pass read off the item this finding
/// is about. Fixers rewrite [`Self::Bare`] and specifically approved
/// [`Self::Restricted`] annotations. [`Self::Unknown`] means the pass captured
/// no annotation text, so there is nothing for a fixer to verify its edit
/// against.
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub(crate) enum WrittenVisibility {
    #[default]
    Unknown,
    Bare,
    /// A `pub(crate)`, `pub(super)`, `pub(self)`, or `pub(in <path>)`
    /// annotation, held as the exact source text — which may span lines.
    Restricted(String),
}

impl From<Option<String>> for WrittenVisibility {
    fn from(visibility_annotation: Option<String>) -> Self {
        match visibility_annotation {
            None => Self::Unknown,
            Some(source) if source == "pub" => Self::Bare,
            Some(source) => Self::Restricted(source),
        }
    }
}

/// The scope a finding proposes narrowing its item to, and what a fixer is
/// allowed to do with it.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub(crate) enum ExactBoundarySpelling {
    #[default]
    CratePath,
    Parent,
    Crate,
    Private,
    Public,
}

#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub(crate) enum NarrowerScope {
    /// The finding proposes no narrowing.
    #[default]
    Unproposed,
    /// Canonical def-path of the item's enclosing module, recorded so
    /// cross-compilation merge can drop the finding when a caller lives outside
    /// that module. It is a suppression key, not an annotation to write.
    SuppressionKey(String),
    /// Canonical def-path of the exact module boundary the item needs, without
    /// the leading `crate::`. `pub(in crate::<path>)` built from this is a
    /// correct rewrite of the item's supported visibility annotation.
    ExactBoundary(String),
    /// Canonical def-path of the exact module boundary when it is the item's
    /// immediate parent. `pub(super)` is the canonical rewrite for this case.
    ExactParentBoundary(String),
    /// The exact boundary is the crate root. `pub(crate)` is the canonical
    /// rewrite for this case.
    CrateBoundary,
    /// No caller or signature requires visibility outside the defining module.
    /// The visibility annotation can be removed.
    Private,
    /// The required boundary is crate-external. Bare `pub` is the only source
    /// spelling that satisfies it.
    PublicBoundary,
}

impl NarrowerScope {
    /// Rebuilds the scope from the stored boundary, fix support, and exact
    /// replacement spelling. The def-path alone cannot distinguish an
    /// immediate parent from a wider ancestor.
    pub(crate) fn resolve(
        def_path: Option<String>,
        fix_support: FixSupport,
        exact_boundary_spelling: ExactBoundarySpelling,
    ) -> Self {
        match (def_path, fix_support, exact_boundary_spelling) {
            (None, _, _) => Self::Unproposed,
            (Some(def_path), FixSupport::RestrictedAnnotation, ExactBoundarySpelling::Parent) => {
                Self::ExactParentBoundary(def_path)
            },
            (Some(_), FixSupport::RestrictedAnnotation, ExactBoundarySpelling::Crate) => {
                Self::CrateBoundary
            },
            (Some(_), FixSupport::RestrictedAnnotation, ExactBoundarySpelling::Private) => {
                Self::Private
            },
            (
                Some(def_path),
                FixSupport::RestrictedAnnotation,
                ExactBoundarySpelling::CratePath,
            ) => Self::ExactBoundary(def_path),
            (Some(_), FixSupport::RestrictedAnnotation, ExactBoundarySpelling::Public) => {
                Self::PublicBoundary
            },
            (Some(def_path), _, _) => Self::SuppressionKey(def_path),
        }
    }
}

/// What the compiler pass resolved about the visibility of the item a finding
/// is about. Findings about import syntax carry the [`Default`] value, which
/// says exactly that: no annotation was read and no narrowing was proposed.
#[derive(Debug, Clone, Default)]
pub(crate) struct ItemVisibility {
    pub written:        WrittenVisibility,
    pub narrower_scope: NarrowerScope,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub(crate) struct Finding {
    pub severity:        Severity,
    pub diagnostic_code: DiagnosticCode,
    pub path:            String,
    pub line:            usize,
    pub column:          usize,
    pub highlight_len:   usize,
    pub source_line:     String,
    pub item:            Option<String>,
    pub message:         String,
    pub suggestion:      Option<String>,
    #[serde(default, rename = "fixability")]
    pub fix_support:     FixSupport,
    #[serde(default)]
    pub related:         Option<String>,
    /// Skipped by serde on purpose: the fixers need this in-process and nothing
    /// outside cargo-mend reads it, so publishing it would create a JSON
    /// contract with no consumer.
    #[serde(skip)]
    pub item_visibility: ItemVisibility,
}

#[derive(Debug, Default, Serialize, Deserialize)]
pub(crate) struct Report {
    pub root:     String,
    pub summary:  ReportSummary,
    pub findings: Vec<Finding>,
    #[serde(default)]
    pub facts:    ReportFacts,
}

#[derive(Debug, Default, Serialize, Deserialize)]
pub(crate) struct ReportSummary {
    #[serde(rename = "error_count")]
    pub errors:                   usize,
    #[serde(rename = "warning_count")]
    pub warnings:                 usize,
    #[serde(rename = "fixable_with_fix_count")]
    pub fixable_with_fix:         usize,
    #[serde(rename = "fixable_with_fix_pub_use_count")]
    pub fixable_with_fix_pub_use: usize,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub(crate) struct ReportFacts {
    #[serde(default)]
    #[serde(rename = "pub_use")]
    pub pub_use_fix_facts:      PubUseFixFacts,
    #[serde(default)]
    pub all_features_coverage:  AllFeaturesCoverage,
    #[serde(default, rename = "compiler_warnings")]
    pub compiler_warning_facts: CompilerWarningFacts,
}

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub(crate) enum AllFeaturesCoverage {
    #[default]
    NotGuaranteed,
    Superset,
}

impl AllFeaturesCoverage {
    pub(crate) const fn merge(self, other: Self) -> Self {
        match (self, other) {
            (Self::Superset, Self::Superset) => Self::Superset,
            (Self::NotGuaranteed, _) | (_, Self::NotGuaranteed) => Self::NotGuaranteed,
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub(crate) struct PubUseFixFact {
    pub child_path:      String,
    pub child_line:      usize,
    pub child_item_name: String,
    pub parent_path:     String,
    pub parent_line:     usize,
    pub child_module:    String,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub(crate) struct PubUseFixFacts {
    #[serde(default)]
    facts: Vec<PubUseFixFact>,
}

impl PubUseFixFacts {
    pub(crate) fn iter(&self) -> impl Iterator<Item = &PubUseFixFact> { self.facts.iter() }
}

impl From<Vec<PubUseFixFact>> for PubUseFixFacts {
    fn from(facts: Vec<PubUseFixFact>) -> Self { Self { facts } }
}

#[derive(Debug, Clone, Copy, Default, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub(crate) enum CompilerWarningFacts {
    #[default]
    None,
    UnusedImportWarnings,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum BuildOutcome {
    Failed,
    Succeeded,
}

impl BuildOutcome {
    pub(crate) const fn is_success(self) -> bool { matches!(self, Self::Succeeded) }
}

impl Serialize for BuildOutcome {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        serializer.serialize_bool(self.is_success())
    }
}

impl Report {
    pub(crate) const fn outcome(&self) -> BuildOutcome {
        if self.summary.errors > 0 {
            BuildOutcome::Failed
        } else {
            BuildOutcome::Succeeded
        }
    }

    pub(crate) const fn has_warnings(&self) -> bool { self.summary.warnings > 0 }

    pub(crate) fn refresh_summary(&mut self) {
        self.summary = ReportSummary {
            errors:                   self
                .findings
                .iter()
                .filter(|f| effective_severity(f) == Severity::Error)
                .count(),
            warnings:                 self
                .findings
                .iter()
                .filter(|f| effective_severity(f) == Severity::Warning)
                .count(),
            fixable_with_fix:         self
                .findings
                .iter()
                .filter(|f| {
                    effective_fixability(f).summary_bucket() == Some(FixSummaryBucket::Standard)
                })
                .count(),
            fixable_with_fix_pub_use: self
                .findings
                .iter()
                .filter(|f| {
                    effective_fixability(f).summary_bucket() == Some(FixSummaryBucket::PubUse)
                })
                .count(),
        };
    }
}

fn diagnostic_spec(code: DiagnosticCode) -> &'static DiagnosticSpec {
    match code {
        DiagnosticCode::OverbroadPubCrate => &OVERBROAD_PUB_CRATE,
        DiagnosticCode::ForbiddenPubInCrate => &FORBIDDEN_PUB_IN_CRATE,
        DiagnosticCode::ReviewPubMod => &REVIEW_PUB_MOD,
        DiagnosticCode::SuspiciousPub => &SUSPICIOUS_PUB,
        DiagnosticCode::UnusedPub => &UNUSED_PUB,
        DiagnosticCode::PreferModuleImport => &PREFER_MODULE_IMPORT,
        DiagnosticCode::InlinePathQualifiedType => &INLINE_PATH_QUALIFIED_TYPE,
        DiagnosticCode::ShortenLocalCrateImport => &SHORTEN_LOCAL_CRATE_IMPORT,
        DiagnosticCode::ReplaceDeepSuperImport => &REPLACE_DEEP_SUPER_IMPORT,
        DiagnosticCode::WildcardParentPubUse => &WILDCARD_PARENT_PUB_USE,
        DiagnosticCode::InternalParentPubUseFacade => &INTERNAL_PARENT_PUB_USE_FACADE,
        DiagnosticCode::NarrowToPubCrate => &NARROW_TO_PUB_CRATE,
        DiagnosticCode::FieldVisibilityWiderThanType => &FIELD_VISIBILITY_WIDER_THAN_TYPE,
        DiagnosticCode::ImportsAtTop => &IMPORTS_AT_TOP,
    }
}

pub(super) fn effective_fixability(finding: &Finding) -> FixSupport {
    if matches!(finding.fix_support, FixSupport::None) {
        diagnostic_spec(finding.diagnostic_code).fix_support
    } else {
        finding.fix_support
    }
}

/// The severity to report for `finding`.
///
/// `Severity::Error` is written at the two sites whose findings need a person to
/// decide something — `forbidden_pub_in_crate` and `review_pub_mod`. The
/// cross-crate pass in `persistence::visibility_constraint` can later resolve one
/// of those to an exact boundary and mark it fixable, and the stored severity
/// does not follow. Deriving it here keeps one rule: anything mend can fix on its
/// own is a warning, and an error is work only a person can do.
pub(super) fn effective_severity(finding: &Finding) -> Severity {
    if effective_fixability(finding).summary_bucket().is_some() {
        Severity::Warning
    } else {
        finding.severity
    }
}

pub(super) fn fixability_note(finding: &Finding) -> Option<&'static str> {
    effective_fixability(finding).note(effective_severity(finding))
}

pub(super) fn finding_headline(finding: &Finding) -> String {
    match diagnostic_spec(finding.diagnostic_code).headline {
        HeadlineSource::Static(headline) => headline.to_string(),
        HeadlineSource::FindingMessage { fallback } => {
            if finding.message.is_empty() {
                fallback.to_string()
            } else {
                finding.message.clone()
            }
        },
    }
}

pub(super) fn finding_message_not_in_headline(finding: &Finding) -> Option<&str> {
    if finding.message.is_empty()
        || matches!(
            diagnostic_spec(finding.diagnostic_code).headline,
            HeadlineSource::FindingMessage { .. }
        )
    {
        None
    } else {
        Some(&finding.message)
    }
}

pub(super) fn detail_reasons(finding: &Finding) -> Vec<String> {
    let mut reasons = match diagnostic_spec(finding.diagnostic_code).detail_mode {
        DetailMode::None => Vec::new(),
        DetailMode::MessageAndRelated => {
            let mut reasons = Vec::new();
            if let Some(message) = finding_message_not_in_headline(finding) {
                reasons.push(message.to_string());
            }
            if let Some(related) = &finding.related {
                reasons.push(related.clone());
            }
            reasons
        },
    };
    if let Some(note) = fixability_note(finding) {
        reasons.push(note.to_string());
    }
    reasons
}

fn inline_help_text(finding: &Finding) -> Option<&'static str> {
    diagnostic_spec(finding.diagnostic_code).inline_help
}

fn custom_inline_help_text(finding: &Finding) -> Option<&str> { finding.suggestion.as_deref() }

pub(super) fn resolved_inline_help_text(finding: &Finding) -> Option<&str> {
    custom_inline_help_text(finding).or_else(|| inline_help_text(finding))
}

pub(super) fn finding_help_url(finding: &Finding) -> String {
    format!(
        "{HELP_URL_BASE}#{}",
        diagnostic_spec(finding.diagnostic_code).help_anchor
    )
}

#[cfg(test)]
mod tests {
    use super::Finding;
    use super::FixSupport;
    use super::ItemVisibility;
    use super::Severity;
    use super::finding_headline;
    use crate::config::DiagnosticCode;

    #[test]
    fn finding_message_headlines_use_messages_with_static_fallbacks() {
        for (diagnostic_code, fallback) in [
            (
                DiagnosticCode::OverbroadPubCrate,
                "`pub(crate)` is broader than required",
            ),
            (
                DiagnosticCode::ForbiddenPubInCrate,
                "use of `pub(in crate::...)` is forbidden by policy",
            ),
            (
                DiagnosticCode::InternalParentPubUseFacade,
                "parent module re-export is acting as an internal facade",
            ),
        ] {
            let mut finding = Finding {
                severity: Severity::Error,
                diagnostic_code,
                path: "src/lib.rs".to_string(),
                line: 1,
                column: 1,
                highlight_len: 1,
                source_line: "pub(crate) struct Example;".to_string(),
                item: Some("Example".to_string()),
                message: "custom forbidden visibility outcome".to_string(),
                suggestion: None,
                fix_support: FixSupport::None,
                related: None,
                item_visibility: ItemVisibility::default(),
            };

            assert_eq!(
                finding_headline(&finding),
                "custom forbidden visibility outcome"
            );

            finding.message.clear();
            assert_eq!(finding_headline(&finding), fallback);
        }

        for (diagnostic_code, message) in [
            (
                DiagnosticCode::OverbroadPubCrate,
                "use of `pub(crate)` does not match the parent facade boundary",
            ),
            (
                DiagnosticCode::OverbroadPubCrate,
                "`pub(in crate)` is a redundant spelling of `pub(crate)`",
            ),
            (
                DiagnosticCode::OverbroadPubCrate,
                "`pub(in crate)` is wider than the exact parent facade boundary",
            ),
            (
                DiagnosticCode::ForbiddenPubInCrate,
                "parent facade caps reach at `pub(crate)`",
            ),
            (
                DiagnosticCode::ForbiddenPubInCrate,
                "use of `pub(in crate::video_plane)` is disabled by project visibility policy",
            ),
            (
                DiagnosticCode::ForbiddenPubInCrate,
                "parent facade does not provide a resolvable visibility boundary",
            ),
            (
                DiagnosticCode::ForbiddenPubInCrate,
                "`pub(in crate::a)` is not the path callers use to name this item",
            ),
            (
                DiagnosticCode::ForbiddenPubInCrate,
                "no allowed visibility keeps this item reachable from its callers: private and `pub(super)` are too narrow, and `pub` needs a re-export to cap it",
            ),
        ] {
            let finding = Finding {
                severity: Severity::Error,
                diagnostic_code,
                path: "src/lib.rs".to_string(),
                line: 1,
                column: 1,
                highlight_len: 1,
                source_line: "pub(crate) struct Example;".to_string(),
                item: Some("Example".to_string()),
                message: message.to_string(),
                suggestion: None,
                fix_support: FixSupport::None,
                related: None,
                item_visibility: ItemVisibility::default(),
            };

            assert_eq!(finding_headline(&finding), message);
        }
    }
}