Skip to content

Commit 778d274

Browse files
committed
also check let-else
1 parent 14196db commit 778d274

3 files changed

Lines changed: 45 additions & 4 deletions

File tree

compiler/rustc_lint/src/internal.rs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -783,15 +783,37 @@ impl<'tcx> LateLintPass<'tcx> for RustcMustMatchExhaustively {
783783
}
784784
}
785785
}
786-
hir::ExprKind::If(expr, ..) if let ExprKind::Let(expr) = expr.kind => {
786+
hir::ExprKind::Let(expr, ..) => {
787787
if let Some(attr_span) = is_rustc_must_match_exhaustively(cx, expr.init.hir_id) {
788788
cx.emit_span_lint(
789789
RUSTC_MUST_MATCH_EXHAUSTIVELY,
790790
expr.span,
791791
RustcMustMatchExhaustivelyNotExhaustive {
792792
attr_span,
793793
pat_span: expr.span,
794-
message: "using if let only matches on one variant (try using `match`)",
794+
message: "using `if let` only matches on one variant (try using `match`)",
795+
},
796+
);
797+
}
798+
}
799+
_ => {}
800+
}
801+
}
802+
803+
fn check_stmt(&mut self, cx: &LateContext<'tcx>, stmt: &'tcx rustc_hir::Stmt<'tcx>) {
804+
match stmt.kind {
805+
rustc_hir::StmtKind::Let(let_stmt) => {
806+
if let_stmt.els.is_some()
807+
&& let Some(attr_span) =
808+
is_rustc_must_match_exhaustively(cx, let_stmt.pat.hir_id)
809+
{
810+
cx.emit_span_lint(
811+
RUSTC_MUST_MATCH_EXHAUSTIVELY,
812+
let_stmt.span,
813+
RustcMustMatchExhaustivelyNotExhaustive {
814+
attr_span,
815+
pat_span: let_stmt.pat.span,
816+
message: "using `let else` only matches on one variant (try using `match`)",
795817
},
796818
);
797819
}

tests/ui-fulldeps/internal-lints/must_match_exhaustively.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ fn foo(f: Foo) {
4343

4444
if let Foo::A { .. } = f {}
4545
//~^ ERROR match is not exhaustive
46+
47+
let Foo::A { .. } = f else { loop {} };
48+
//~^ ERROR match is not exhaustive
4649
}
4750

4851
fn main() {}

tests/ui-fulldeps/internal-lints/must_match_exhaustively.stderr

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,27 @@ LL | if let Foo::A { .. } = f {}
6161
| ^^^^^^^^^^^^^^^^^^^^^
6262
|
6363
= help: explicitly list all variants of the enum in a `match`
64-
note: using if let only matches on one variant (try using `match`)
64+
note: using `if let` only matches on one variant (try using `match`)
6565
--> $DIR/must_match_exhaustively.rs:44:8
6666
|
6767
LL | if let Foo::A { .. } = f {}
6868
| ^^^^^^^^^^^^^^^^^^^^^
6969

70-
error: aborting due to 4 previous errors
70+
error: match is not exhaustive
71+
--> $DIR/must_match_exhaustively.rs:47:5
72+
|
73+
LL | #[rustc_must_match_exhaustively]
74+
| -------------------------------- required because of this attribute
75+
...
76+
LL | let Foo::A { .. } = f else { loop {} };
77+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
78+
|
79+
= help: explicitly list all variants of the enum in a `match`
80+
note: using `let else` only matches on one variant (try using `match`)
81+
--> $DIR/must_match_exhaustively.rs:47:9
82+
|
83+
LL | let Foo::A { .. } = f else { loop {} };
84+
| ^^^^^^^^^^^^^
85+
86+
error: aborting due to 5 previous errors
7187

0 commit comments

Comments
 (0)