Skip to content

Commit 5cccdb3

Browse files
Rollup merge of rust-lang#155276 - jdonszelmann:must-match-exhaustively-let-else, r=JonathanBrouwer
`#[rustc_must_match_exhaustively]` detect let else Extension of rust-lang#155047, I forgor to lint on let-else :3
2 parents 21588d4 + 26a0fdc commit 5cccdb3

6 files changed

Lines changed: 68 additions & 12 deletions

File tree

compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -659,9 +659,16 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
659659
// being stalled on a coroutine.
660660
self.select_obligations_where_possible(|_| {});
661661

662-
let ty::TypingMode::Analysis { defining_opaque_types_and_generators } = self.typing_mode()
663-
else {
664-
bug!();
662+
let defining_opaque_types_and_generators = match self.typing_mode() {
663+
ty::TypingMode::Analysis { defining_opaque_types_and_generators } => {
664+
defining_opaque_types_and_generators
665+
}
666+
ty::TypingMode::Coherence
667+
| ty::TypingMode::Borrowck { .. }
668+
| ty::TypingMode::PostBorrowckAnalysis { .. }
669+
| ty::TypingMode::PostAnalysis => {
670+
bug!()
671+
}
665672
};
666673

667674
if defining_opaque_types_and_generators

compiler/rustc_hir_typeck/src/opaque_types.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
use rustc_hir::def::DefKind;
22
use rustc_infer::traits::ObligationCause;
3+
use rustc_middle::bug;
34
use rustc_middle::ty::{
45
self, DefiningScopeKind, DefinitionSiteHiddenType, OpaqueTypeKey, ProvisionalHiddenType,
5-
TypeVisitableExt, TypingMode,
6+
TypeVisitableExt,
67
};
78
use rustc_trait_selection::error_reporting::infer::need_type_info::TypeAnnotationNeeded;
89
use rustc_trait_selection::opaque_types::{
@@ -97,9 +98,16 @@ impl<'tcx> FnCtxt<'_, 'tcx> {
9798
debug!(?opaque_types);
9899

99100
let tcx = self.tcx;
100-
let TypingMode::Analysis { defining_opaque_types_and_generators } = self.typing_mode()
101-
else {
102-
unreachable!();
101+
let defining_opaque_types_and_generators = match self.typing_mode() {
102+
ty::TypingMode::Analysis { defining_opaque_types_and_generators } => {
103+
defining_opaque_types_and_generators
104+
}
105+
ty::TypingMode::Coherence
106+
| ty::TypingMode::Borrowck { .. }
107+
| ty::TypingMode::PostBorrowckAnalysis { .. }
108+
| ty::TypingMode::PostAnalysis => {
109+
bug!()
110+
}
103111
};
104112

105113
for def_id in defining_opaque_types_and_generators {

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
}

compiler/rustc_next_trait_solver/src/solve/assembly/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ where
442442
// normalizing the self type as well, since type variables are not uniquified.
443443
let goal = self.resolve_vars_if_possible(goal);
444444

445-
if let TypingMode::Coherence = self.typing_mode()
445+
if self.typing_mode().is_coherence()
446446
&& let Ok(candidate) = self.consider_coherence_unknowable_candidate(goal)
447447
{
448448
candidates.push(candidate);

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)