rust/compiler/rustc_pattern_analysis/src/lints.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

110 lines
4.7 KiB
Rust
Raw Normal View History

2023-12-11 09:40:31 +00:00
use rustc_session::lint::builtin::NON_EXHAUSTIVE_OMITTED_PATTERNS;
use rustc_span::ErrorGuaranteed;
2023-12-11 09:40:31 +00:00
use crate::constructor::Constructor;
use crate::errors::{NonExhaustiveOmittedPattern, NonExhaustiveOmittedPatternLintOnArm, Uncovered};
use crate::pat_column::PatternColumn;
2024-01-24 20:16:57 +00:00
use crate::rustc::{RevealedTy, RustcMatchCheckCtxt, WitnessPat};
use crate::MatchArm;
2023-12-11 09:40:31 +00:00
/// Traverse the patterns to collect any variants of a non_exhaustive enum that fail to be mentioned
/// in a given column.
2023-12-15 15:53:29 +00:00
#[instrument(level = "debug", skip(cx), ret)]
2024-02-02 22:45:25 +00:00
fn collect_nonexhaustive_missing_variants<'p, 'tcx>(
2024-01-24 20:16:57 +00:00
cx: &RustcMatchCheckCtxt<'p, 'tcx>,
2024-01-24 19:38:21 +00:00
column: &PatternColumn<'p, RustcMatchCheckCtxt<'p, 'tcx>>,
2024-01-07 20:20:16 +00:00
) -> Result<Vec<WitnessPat<'p, 'tcx>>, ErrorGuaranteed> {
2024-01-24 19:38:21 +00:00
let Some(&ty) = column.head_ty() else {
2024-01-07 20:20:16 +00:00
return Ok(Vec::new());
2023-12-11 09:40:31 +00:00
};
2024-01-24 20:16:57 +00:00
let set = column.analyze_ctors(cx, &ty)?;
2023-12-11 09:40:31 +00:00
if set.present.is_empty() {
// We can't consistently handle the case where no constructors are present (since this would
// require digging deep through any type in case there's a non_exhaustive enum somewhere),
// so for consistency we refuse to handle the top-level case, where we could handle it.
2024-01-07 20:20:16 +00:00
return Ok(Vec::new());
2023-12-11 09:40:31 +00:00
}
let mut witnesses = Vec::new();
2024-01-24 20:16:57 +00:00
if cx.is_foreign_non_exhaustive_enum(ty) {
2023-12-11 09:40:31 +00:00
witnesses.extend(
set.missing
.into_iter()
// This will list missing visible variants.
.filter(|c| !matches!(c, Constructor::Hidden | Constructor::NonExhaustive))
2024-01-24 20:16:57 +00:00
.map(|missing_ctor| WitnessPat::wild_from_ctor(cx, missing_ctor, ty)),
2023-12-11 09:40:31 +00:00
)
}
// Recurse into the fields.
for ctor in set.present {
2024-01-24 20:16:57 +00:00
let specialized_columns = column.specialize(cx, &ty, &ctor);
let wild_pat = WitnessPat::wild_from_ctor(cx, ctor, ty);
2023-12-11 09:40:31 +00:00
for (i, col_i) in specialized_columns.iter().enumerate() {
// Compute witnesses for each column.
2024-01-07 20:20:16 +00:00
let wits_for_col_i = collect_nonexhaustive_missing_variants(cx, col_i)?;
2023-12-11 09:40:31 +00:00
// For each witness, we build a new pattern in the shape of `ctor(_, _, wit, _, _)`,
// adding enough wildcards to match `arity`.
for wit in wits_for_col_i {
let mut pat = wild_pat.clone();
pat.fields[i] = wit;
witnesses.push(pat);
}
}
}
2024-01-07 20:20:16 +00:00
Ok(witnesses)
2023-12-11 09:40:31 +00:00
}
2024-01-24 20:16:57 +00:00
pub(crate) fn lint_nonexhaustive_missing_variants<'p, 'tcx>(
rcx: &RustcMatchCheckCtxt<'p, 'tcx>,
2024-01-24 19:38:21 +00:00
arms: &[MatchArm<'p, RustcMatchCheckCtxt<'p, 'tcx>>],
pat_column: &PatternColumn<'p, RustcMatchCheckCtxt<'p, 'tcx>>,
scrut_ty: RevealedTy<'tcx>,
2024-01-07 20:20:16 +00:00
) -> Result<(), ErrorGuaranteed> {
2023-12-11 09:40:31 +00:00
if !matches!(
2023-12-15 15:53:29 +00:00
rcx.tcx.lint_level_at_node(NON_EXHAUSTIVE_OMITTED_PATTERNS, rcx.match_lint_level).0,
2023-12-11 09:40:31 +00:00
rustc_session::lint::Level::Allow
) {
2024-01-24 20:16:57 +00:00
let witnesses = collect_nonexhaustive_missing_variants(rcx, pat_column)?;
2023-12-11 09:40:31 +00:00
if !witnesses.is_empty() {
// Report that a match of a `non_exhaustive` enum marked with `non_exhaustive_omitted_patterns`
// is not exhaustive enough.
//
// NB: The partner lint for structs lives in `compiler/rustc_hir_analysis/src/check/pat.rs`.
rcx.tcx.emit_node_span_lint(
2023-12-11 09:40:31 +00:00
NON_EXHAUSTIVE_OMITTED_PATTERNS,
2023-12-15 15:53:29 +00:00
rcx.match_lint_level,
rcx.scrut_span,
2023-12-11 09:40:31 +00:00
NonExhaustiveOmittedPattern {
scrut_ty: scrut_ty.inner(),
2023-12-15 15:53:29 +00:00
uncovered: Uncovered::new(rcx.scrut_span, rcx, witnesses),
2023-12-11 09:40:31 +00:00
},
);
}
} else {
// We used to allow putting the `#[allow(non_exhaustive_omitted_patterns)]` on a match
// arm. This no longer makes sense so we warn users, to avoid silently breaking their
// usage of the lint.
for arm in arms {
let (lint_level, lint_level_source) =
2023-12-15 15:53:29 +00:00
rcx.tcx.lint_level_at_node(NON_EXHAUSTIVE_OMITTED_PATTERNS, arm.arm_data);
2023-12-11 09:40:31 +00:00
if !matches!(lint_level, rustc_session::lint::Level::Allow) {
let decorator = NonExhaustiveOmittedPatternLintOnArm {
lint_span: lint_level_source.span(),
2023-12-15 15:53:29 +00:00
suggest_lint_on_match: rcx.whole_match_span.map(|span| span.shrink_to_lo()),
2023-12-11 09:40:31 +00:00
lint_level: lint_level.as_str(),
lint_name: "non_exhaustive_omitted_patterns",
};
use rustc_errors::LintDiagnostic;
let mut err = rcx.tcx.dcx().struct_span_warn(arm.pat.data().unwrap().span, "");
err.primary_message(decorator.msg());
2023-12-11 09:40:31 +00:00
decorator.decorate_lint(&mut err);
err.emit();
}
}
}
2024-01-07 20:20:16 +00:00
Ok(())
2023-12-11 09:40:31 +00:00
}