also check let-else

This commit is contained in:
Jana Dönszelmann
2026-04-14 10:47:37 +02:00
parent 14196dbfa3
commit 778d27441d
3 changed files with 45 additions and 4 deletions
+24 -2
View File
@@ -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`)",
},
);
}
@@ -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() {}
@@ -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