rustc_pattern_analysis/
errors.rs1use rustc_errors::{Diag, EmissionGuarantee, Subdiagnostic};
2use rustc_macros::{Diagnostic, LintDiagnostic, Subdiagnostic};
3use rustc_middle::ty::Ty;
4use rustc_span::Span;
5
6use crate::rustc::{RustcPatCtxt, WitnessPat};
7
8#[derive(Subdiagnostic)]
9#[label(pattern_analysis_uncovered)]
10pub struct Uncovered {
11    #[primary_span]
12    span: Span,
13    count: usize,
14    witness_1: String, witness_2: String, witness_3: String, remainder: usize,
18}
19
20impl Uncovered {
21    pub fn new<'p, 'tcx>(
22        span: Span,
23        cx: &RustcPatCtxt<'p, 'tcx>,
24        witnesses: Vec<WitnessPat<'p, 'tcx>>,
25    ) -> Self
26    where
27        'tcx: 'p,
28    {
29        let witness_1 = cx.print_witness_pat(witnesses.get(0).unwrap());
30        Self {
31            span,
32            count: witnesses.len(),
33            witness_2: witnesses.get(1).map(|w| cx.print_witness_pat(w)).unwrap_or_default(),
35            witness_3: witnesses.get(2).map(|w| cx.print_witness_pat(w)).unwrap_or_default(),
36            witness_1,
37            remainder: witnesses.len().saturating_sub(3),
38        }
39    }
40}
41
42#[derive(LintDiagnostic)]
43#[diag(pattern_analysis_overlapping_range_endpoints)]
44#[note]
45pub struct OverlappingRangeEndpoints {
46    #[label]
47    pub range: Span,
48    #[subdiagnostic]
49    pub overlap: Vec<Overlap>,
50}
51
52pub struct Overlap {
53    pub span: Span,
54    pub range: String, }
56
57impl Subdiagnostic for Overlap {
58    fn add_to_diag<G: EmissionGuarantee>(self, diag: &mut Diag<'_, G>) {
59        let Overlap { span, range } = self;
60
61        let message = format!("this range overlaps on `{range}`...");
64        diag.span_label(span, message);
65    }
66}
67
68#[derive(LintDiagnostic)]
69#[diag(pattern_analysis_excluside_range_missing_max)]
70pub struct ExclusiveRangeMissingMax {
71    #[label]
72    #[suggestion(code = "{suggestion}", applicability = "maybe-incorrect")]
73    pub first_range: Span,
75    pub suggestion: String,
77    pub max: String, }
79
80#[derive(LintDiagnostic)]
81#[diag(pattern_analysis_excluside_range_missing_gap)]
82pub struct ExclusiveRangeMissingGap {
83    #[label]
84    #[suggestion(code = "{suggestion}", applicability = "maybe-incorrect")]
85    pub first_range: Span,
87    pub gap: String, pub suggestion: String,
90    #[subdiagnostic]
91    pub gap_with: Vec<GappedRange>,
93}
94
95pub struct GappedRange {
96    pub span: Span,
97    pub gap: String,         pub first_range: String, }
100
101impl Subdiagnostic for GappedRange {
102    fn add_to_diag<G: EmissionGuarantee>(self, diag: &mut Diag<'_, G>) {
103        let GappedRange { span, gap, first_range } = self;
104
105        let message = format!(
108            "this could appear to continue range `{first_range}`, but `{gap}` isn't matched by \
109            either of them"
110        );
111        diag.span_label(span, message);
112    }
113}
114
115#[derive(LintDiagnostic)]
116#[diag(pattern_analysis_non_exhaustive_omitted_pattern)]
117#[help]
118#[note]
119pub(crate) struct NonExhaustiveOmittedPattern<'tcx> {
120    pub scrut_ty: Ty<'tcx>,
121    #[subdiagnostic]
122    pub uncovered: Uncovered,
123}
124
125#[derive(LintDiagnostic)]
126#[diag(pattern_analysis_non_exhaustive_omitted_pattern_lint_on_arm)]
127#[help]
128pub(crate) struct NonExhaustiveOmittedPatternLintOnArm {
129    #[label]
130    pub lint_span: Span,
131    #[suggestion(code = "#[{lint_level}({lint_name})]\n", applicability = "maybe-incorrect")]
132    pub suggest_lint_on_match: Option<Span>,
133    pub lint_level: &'static str,
134    pub lint_name: &'static str,
135}
136
137#[derive(Diagnostic)]
138#[diag(pattern_analysis_mixed_deref_pattern_constructors)]
139pub(crate) struct MixedDerefPatternConstructors<'tcx> {
140    #[primary_span]
141    pub spans: Vec<Span>,
142    pub smart_pointer_ty: Ty<'tcx>,
143    #[label(pattern_analysis_deref_pattern_label)]
144    pub deref_pattern_label: Span,
145    #[label(pattern_analysis_normal_constructor_label)]
146    pub normal_constructor_label: Span,
147}