diff --git a/compiler/rustc_lint/src/internal.rs b/compiler/rustc_lint/src/internal.rs index c0b113610f67..d637390851d9 100644 --- a/compiler/rustc_lint/src/internal.rs +++ b/compiler/rustc_lint/src/internal.rs @@ -783,7 +783,7 @@ fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &hir::Expr<'_>) { } } } - hir::ExprKind::If(expr, ..) if let ExprKind::Let(expr) = expr.kind => { + hir::ExprKind::Let(expr, ..) => { if let Some(attr_span) = is_rustc_must_match_exhaustively(cx, expr.init.hir_id) { cx.emit_span_lint( RUSTC_MUST_MATCH_EXHAUSTIVELY, @@ -791,7 +791,29 @@ fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &hir::Expr<'_>) { RustcMustMatchExhaustivelyNotExhaustive { attr_span, pat_span: expr.span, - message: "using if let only matches on one variant (try using `match`)", + message: "using `if let` only matches on one variant (try using `match`)", + }, + ); + } + } + _ => {} + } + } + + fn check_stmt(&mut self, cx: &LateContext<'tcx>, stmt: &'tcx rustc_hir::Stmt<'tcx>) { + match stmt.kind { + rustc_hir::StmtKind::Let(let_stmt) => { + if let_stmt.els.is_some() + && let Some(attr_span) = + is_rustc_must_match_exhaustively(cx, let_stmt.pat.hir_id) + { + cx.emit_span_lint( + RUSTC_MUST_MATCH_EXHAUSTIVELY, + let_stmt.span, + RustcMustMatchExhaustivelyNotExhaustive { + attr_span, + pat_span: let_stmt.pat.span, + message: "using `let else` only matches on one variant (try using `match`)", }, ); } diff --git a/tests/ui-fulldeps/internal-lints/must_match_exhaustively.rs b/tests/ui-fulldeps/internal-lints/must_match_exhaustively.rs index cc3dcebd11cd..e3a77471a0fe 100644 --- a/tests/ui-fulldeps/internal-lints/must_match_exhaustively.rs +++ b/tests/ui-fulldeps/internal-lints/must_match_exhaustively.rs @@ -43,6 +43,9 @@ fn foo(f: Foo) { if let Foo::A { .. } = f {} //~^ ERROR match is not exhaustive + + let Foo::A { .. } = f else { loop {} }; + //~^ ERROR match is not exhaustive } fn main() {} diff --git a/tests/ui-fulldeps/internal-lints/must_match_exhaustively.stderr b/tests/ui-fulldeps/internal-lints/must_match_exhaustively.stderr index e17cfcefc409..a6ef63c5aba8 100644 --- a/tests/ui-fulldeps/internal-lints/must_match_exhaustively.stderr +++ b/tests/ui-fulldeps/internal-lints/must_match_exhaustively.stderr @@ -61,11 +61,27 @@ 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`) +note: using `if let` only matches on one variant (try using `match`) --> $DIR/must_match_exhaustively.rs:44:8 | LL | if let Foo::A { .. } = f {} | ^^^^^^^^^^^^^^^^^^^^^ -error: aborting due to 4 previous errors +error: match is not exhaustive + --> $DIR/must_match_exhaustively.rs:47:5 + | +LL | #[rustc_must_match_exhaustively] + | -------------------------------- required because of this attribute +... +LL | let Foo::A { .. } = f else { loop {} }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = help: explicitly list all variants of the enum in a `match` +note: using `let else` only matches on one variant (try using `match`) + --> $DIR/must_match_exhaustively.rs:47:9 + | +LL | let Foo::A { .. } = f else { loop {} }; + | ^^^^^^^^^^^^^ + +error: aborting due to 5 previous errors