mirror of
https://github.com/allankoechke/rust.git
synced 2026-12-01 17:40:16 +00:00
add #[rustc_must_match_exhaustively]
This commit is contained in:
@@ -1349,3 +1349,12 @@ impl<S: Stage> NoArgsAttributeParser<S> for RustcIntrinsicConstStableIndirectPar
|
||||
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Fn)]);
|
||||
const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcIntrinsicConstStableIndirect;
|
||||
}
|
||||
|
||||
pub(crate) struct RustcExhaustiveParser;
|
||||
|
||||
impl<S: Stage> NoArgsAttributeParser<S> for RustcExhaustiveParser {
|
||||
const PATH: &'static [Symbol] = &[sym::rustc_must_match_exhaustively];
|
||||
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
|
||||
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Enum)]);
|
||||
const CREATE: fn(Span) -> AttributeKind = AttributeKind::RustcMustMatchExhaustively;
|
||||
}
|
||||
|
||||
@@ -297,6 +297,7 @@ attribute_parsers!(
|
||||
Single<WithoutArgs<RustcEffectiveVisibilityParser>>,
|
||||
Single<WithoutArgs<RustcEiiForeignItemParser>>,
|
||||
Single<WithoutArgs<RustcEvaluateWhereClausesParser>>,
|
||||
Single<WithoutArgs<RustcExhaustiveParser>>,
|
||||
Single<WithoutArgs<RustcHasIncoherentInherentImplsParser>>,
|
||||
Single<WithoutArgs<RustcHiddenTypeOfOpaquesParser>>,
|
||||
Single<WithoutArgs<RustcInheritOverflowChecksParser>>,
|
||||
|
||||
@@ -1414,6 +1414,10 @@ pub static BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
|
||||
rustc_scalable_vector, Normal, template!(List: &["count"]), WarnFollowing, EncodeCrossCrate::Yes,
|
||||
"`#[rustc_scalable_vector]` defines a scalable vector type"
|
||||
),
|
||||
rustc_attr!(
|
||||
rustc_must_match_exhaustively, Normal, template!(Word), WarnFollowing, EncodeCrossCrate::Yes,
|
||||
"enums with `#[rustc_must_match_exhaustively]` must be matched on with a match block that mentions all variants explicitly"
|
||||
),
|
||||
|
||||
// ==========================================================================
|
||||
// Internal attributes, Testing:
|
||||
|
||||
@@ -1471,6 +1471,9 @@ pub enum AttributeKind {
|
||||
fn_names: ThinVec<Ident>,
|
||||
},
|
||||
|
||||
/// Represents `#[rustc_must_match_exhaustively]`
|
||||
RustcMustMatchExhaustively(Span),
|
||||
|
||||
/// Represents `#[rustc_never_returns_null_ptr]`
|
||||
RustcNeverReturnsNullPtr,
|
||||
|
||||
|
||||
@@ -156,6 +156,7 @@ impl AttributeKind {
|
||||
RustcMain => No,
|
||||
RustcMir(..) => Yes,
|
||||
RustcMustImplementOneOf { .. } => No,
|
||||
RustcMustMatchExhaustively(..) => Yes,
|
||||
RustcNeverReturnsNullPtr => Yes,
|
||||
RustcNeverTypeOptions { .. } => No,
|
||||
RustcNoImplicitAutorefs => Yes,
|
||||
|
||||
@@ -15,8 +15,9 @@ use rustc_span::{Span, sym};
|
||||
use crate::lints::{
|
||||
AttributeKindInFindAttr, BadOptAccessDiag, DefaultHashTypesDiag,
|
||||
ImplicitSysrootCrateImportDiag, LintPassByHand, NonGlobImportTypeIrInherent, QueryInstability,
|
||||
QueryUntracked, SpanUseEqCtxtDiag, SymbolInternStringLiteralDiag, TyQualified, TykindDiag,
|
||||
TykindKind, TypeIrDirectUse, TypeIrInherentUsage, TypeIrTraitUsage,
|
||||
QueryUntracked, RustcMustMatchExhaustivelyNotExhaustive, SpanUseEqCtxtDiag,
|
||||
SymbolInternStringLiteralDiag, TyQualified, TykindDiag, TykindKind, TypeIrDirectUse,
|
||||
TypeIrInherentUsage, TypeIrTraitUsage,
|
||||
};
|
||||
use crate::{EarlyContext, EarlyLintPass, LateContext, LateLintPass, LintContext};
|
||||
|
||||
@@ -713,3 +714,89 @@ impl EarlyLintPass for BadUseOfFindAttr {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
declare_tool_lint! {
|
||||
pub rustc::RUSTC_MUST_MATCH_EXHAUSTIVELY,
|
||||
Allow,
|
||||
"Forbids matches with wildcards, or if-let matching on enums marked with `#[rustc_must_match_exhaustively]`",
|
||||
report_in_external_macro: true
|
||||
}
|
||||
declare_lint_pass!(RustcMustMatchExhaustively => [RUSTC_MUST_MATCH_EXHAUSTIVELY]);
|
||||
|
||||
fn is_rustc_must_match_exhaustively(cx: &LateContext<'_>, id: HirId) -> Option<Span> {
|
||||
let res = cx.typeck_results();
|
||||
|
||||
let ty = res.node_type(id);
|
||||
|
||||
let ty = if let ty::Ref(_, ty, _) = ty.kind() { *ty } else { ty };
|
||||
|
||||
if let Some(adt_def) = ty.ty_adt_def()
|
||||
&& adt_def.is_enum()
|
||||
{
|
||||
find_attr!(cx.tcx, adt_def.did(), RustcMustMatchExhaustively(span) => *span)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
fn pat_is_not_exhaustive_heuristic(pat: &hir::Pat<'_>) -> Option<(Span, &'static str)> {
|
||||
match pat.kind {
|
||||
hir::PatKind::Missing => None,
|
||||
hir::PatKind::Wild => Some((pat.span, "because of this wildcard pattern")),
|
||||
hir::PatKind::Binding(_, _, _, Some(pat)) => pat_is_not_exhaustive_heuristic(pat),
|
||||
hir::PatKind::Binding(..) => Some((pat.span, "because of this variable binding")),
|
||||
hir::PatKind::Struct(..) => None,
|
||||
hir::PatKind::TupleStruct(..) => None,
|
||||
hir::PatKind::Or(..) => None,
|
||||
hir::PatKind::Never => None,
|
||||
hir::PatKind::Tuple(..) => None,
|
||||
hir::PatKind::Box(pat) => pat_is_not_exhaustive_heuristic(&*pat),
|
||||
hir::PatKind::Deref(pat) => pat_is_not_exhaustive_heuristic(&*pat),
|
||||
hir::PatKind::Ref(pat, _, _) => pat_is_not_exhaustive_heuristic(&*pat),
|
||||
hir::PatKind::Expr(..) => None,
|
||||
hir::PatKind::Guard(..) => None,
|
||||
hir::PatKind::Range(..) => None,
|
||||
hir::PatKind::Slice(..) => None,
|
||||
hir::PatKind::Err(..) => None,
|
||||
}
|
||||
}
|
||||
|
||||
impl<'tcx> LateLintPass<'tcx> for RustcMustMatchExhaustively {
|
||||
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &hir::Expr<'_>) {
|
||||
match expr.kind {
|
||||
// This is not perfect exhaustiveness checking, that's why this is just a rustc internal
|
||||
// attribute. But it catches most reasonable cases
|
||||
hir::ExprKind::Match(expr, arms, _) => {
|
||||
if let Some(attr_span) = is_rustc_must_match_exhaustively(cx, expr.hir_id) {
|
||||
for arm in arms {
|
||||
if let Some((span, message)) = pat_is_not_exhaustive_heuristic(arm.pat) {
|
||||
cx.emit_span_lint(
|
||||
RUSTC_MUST_MATCH_EXHAUSTIVELY,
|
||||
expr.span,
|
||||
RustcMustMatchExhaustivelyNotExhaustive {
|
||||
attr_span,
|
||||
pat_span: span,
|
||||
message,
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
hir::ExprKind::If(expr, ..) if let ExprKind::Let(expr) = expr.kind => {
|
||||
if let Some(attr_span) = is_rustc_must_match_exhaustively(cx, expr.init.hir_id) {
|
||||
cx.emit_span_lint(
|
||||
RUSTC_MUST_MATCH_EXHAUSTIVELY,
|
||||
expr.span,
|
||||
RustcMustMatchExhaustivelyNotExhaustive {
|
||||
attr_span,
|
||||
pat_span: expr.span,
|
||||
message: "using if let only matches on one variant (try using `match`)",
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -668,6 +668,8 @@ fn register_internals(store: &mut LintStore) {
|
||||
store.register_early_pass(|| Box::new(ImplicitSysrootCrateImport));
|
||||
store.register_lints(&BadUseOfFindAttr::lint_vec());
|
||||
store.register_early_pass(|| Box::new(BadUseOfFindAttr));
|
||||
store.register_lints(&RustcMustMatchExhaustively::lint_vec());
|
||||
store.register_late_pass(|_| Box::new(RustcMustMatchExhaustively));
|
||||
store.register_group(
|
||||
false,
|
||||
"rustc::internal",
|
||||
@@ -688,6 +690,7 @@ fn register_internals(store: &mut LintStore) {
|
||||
LintId::of(DIRECT_USE_OF_RUSTC_TYPE_IR),
|
||||
LintId::of(IMPLICIT_SYSROOT_CRATE_IMPORT),
|
||||
LintId::of(BAD_USE_OF_FIND_ATTR),
|
||||
LintId::of(RUSTC_MUST_MATCH_EXHAUSTIVELY),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1162,6 +1162,18 @@ pub(crate) struct ImplicitSysrootCrateImportDiag<'a> {
|
||||
#[help("remove `AttributeKind`")]
|
||||
pub(crate) struct AttributeKindInFindAttr;
|
||||
|
||||
#[derive(Diagnostic)]
|
||||
#[diag("match is not exhaustive")]
|
||||
#[help("explicitly list all variants of the enum in a `match`")]
|
||||
pub(crate) struct RustcMustMatchExhaustivelyNotExhaustive {
|
||||
#[label("required because of this attribute")]
|
||||
pub attr_span: Span,
|
||||
|
||||
#[note("{$message}")]
|
||||
pub pat_span: Span,
|
||||
pub message: &'static str,
|
||||
}
|
||||
|
||||
// let_underscore.rs
|
||||
#[derive(Diagnostic)]
|
||||
pub(crate) enum NonBindingLet {
|
||||
|
||||
@@ -339,6 +339,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
|
||||
| AttributeKind::RustcMacroTransparency(_)
|
||||
| AttributeKind::RustcMain
|
||||
| AttributeKind::RustcMir(_)
|
||||
| AttributeKind::RustcMustMatchExhaustively(..)
|
||||
| AttributeKind::RustcNeverReturnsNullPtr
|
||||
| AttributeKind::RustcNeverTypeOptions {..}
|
||||
| AttributeKind::RustcNoImplicitAutorefs
|
||||
|
||||
@@ -1753,6 +1753,7 @@ symbols! {
|
||||
rustc_main,
|
||||
rustc_mir,
|
||||
rustc_must_implement_one_of,
|
||||
rustc_must_match_exhaustively,
|
||||
rustc_never_returns_null_ptr,
|
||||
rustc_never_type_options,
|
||||
rustc_no_implicit_autorefs,
|
||||
|
||||
49
tests/ui-fulldeps/internal-lints/must_match_exhaustively.rs
Normal file
49
tests/ui-fulldeps/internal-lints/must_match_exhaustively.rs
Normal file
@@ -0,0 +1,49 @@
|
||||
//@ compile-flags: -Z unstable-options
|
||||
//@ ignore-stage1
|
||||
|
||||
#![feature(rustc_private)]
|
||||
#![feature(rustc_attrs)]
|
||||
#![deny(rustc::rustc_must_match_exhaustively)]
|
||||
|
||||
|
||||
#[rustc_must_match_exhaustively]
|
||||
#[derive(Copy, Clone)]
|
||||
enum Foo {
|
||||
A {field: u32},
|
||||
B,
|
||||
}
|
||||
|
||||
fn foo(f: Foo) {
|
||||
match f {
|
||||
Foo::A {..}=> {}
|
||||
Foo::B => {}
|
||||
}
|
||||
|
||||
match f {
|
||||
//~^ ERROR match is not exhaustive
|
||||
Foo::A {..} => {}
|
||||
_ => {}
|
||||
}
|
||||
|
||||
match f {
|
||||
//~^ ERROR match is not exhaustive
|
||||
Foo::A {..} => {}
|
||||
a => {}
|
||||
}
|
||||
|
||||
match &f {
|
||||
//~^ ERROR match is not exhaustive
|
||||
Foo::A {..} => {}
|
||||
a => {}
|
||||
}
|
||||
|
||||
match f {
|
||||
Foo::A {..} => {}
|
||||
a@Foo::B => {}
|
||||
}
|
||||
|
||||
if let Foo::A {..} = f {}
|
||||
//~^ ERROR match is not exhaustive
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
@@ -0,0 +1,71 @@
|
||||
error: match is not exhaustive
|
||||
--> $DIR/must_match_exhaustively.rs:22:11
|
||||
|
|
||||
LL | #[rustc_must_match_exhaustively]
|
||||
| -------------------------------- required because of this attribute
|
||||
...
|
||||
LL | match f {
|
||||
| ^
|
||||
|
|
||||
= help: explicitly list all variants of the enum in a `match`
|
||||
note: because of this wildcard pattern
|
||||
--> $DIR/must_match_exhaustively.rs:25:9
|
||||
|
|
||||
LL | _ => {}
|
||||
| ^
|
||||
note: the lint level is defined here
|
||||
--> $DIR/must_match_exhaustively.rs:6:9
|
||||
|
|
||||
LL | #![deny(rustc::rustc_must_match_exhaustively)]
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: match is not exhaustive
|
||||
--> $DIR/must_match_exhaustively.rs:28:11
|
||||
|
|
||||
LL | #[rustc_must_match_exhaustively]
|
||||
| -------------------------------- required because of this attribute
|
||||
...
|
||||
LL | match f {
|
||||
| ^
|
||||
|
|
||||
= help: explicitly list all variants of the enum in a `match`
|
||||
note: because of this variable binding
|
||||
--> $DIR/must_match_exhaustively.rs:31:9
|
||||
|
|
||||
LL | a => {}
|
||||
| ^
|
||||
|
||||
error: match is not exhaustive
|
||||
--> $DIR/must_match_exhaustively.rs:34:11
|
||||
|
|
||||
LL | #[rustc_must_match_exhaustively]
|
||||
| -------------------------------- required because of this attribute
|
||||
...
|
||||
LL | match &f {
|
||||
| ^^
|
||||
|
|
||||
= help: explicitly list all variants of the enum in a `match`
|
||||
note: because of this variable binding
|
||||
--> $DIR/must_match_exhaustively.rs:37:9
|
||||
|
|
||||
LL | a => {}
|
||||
| ^
|
||||
|
||||
error: match is not exhaustive
|
||||
--> $DIR/must_match_exhaustively.rs:45:8
|
||||
|
|
||||
LL | #[rustc_must_match_exhaustively]
|
||||
| -------------------------------- required because of this attribute
|
||||
...
|
||||
LL | if let Foo::A {..} = f {}
|
||||
| ^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
= help: explicitly list all variants of the enum in a `match`
|
||||
note: using if let only matches on one variant (try using `match`)
|
||||
--> $DIR/must_match_exhaustively.rs:45:8
|
||||
|
|
||||
LL | if let Foo::A {..} = f {}
|
||||
| ^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: aborting due to 4 previous errors
|
||||
|
||||
Reference in New Issue
Block a user