Skip to content

Commit 34c57df

Browse files
committed
Suppress let else suggestion for uninitialized refutable lets
1 parent ab71ee7 commit 34c57df

File tree

3 files changed

+28
-5
lines changed

3 files changed

+28
-5
lines changed

compiler/rustc_mir_build/src/thir/pattern/check_match.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -683,11 +683,11 @@ impl<'p, 'tcx> MatchVisitor<'p, 'tcx> {
683683
let end_span = semi_span.shrink_to_lo();
684684
let count = witnesses.len();
685685

686-
let_suggestion = Some(if bindings.is_empty() {
687-
SuggestLet::If { start_span, semi_span, count }
688-
} else {
689-
SuggestLet::Else { end_span, count }
690-
});
686+
if bindings.is_empty() {
687+
let_suggestion = Some(SuggestLet::If { start_span, semi_span, count });
688+
} else if scrut.is_some() {
689+
let_suggestion = Some(SuggestLet::Else { end_span, count });
690+
}
691691
};
692692

693693
let adt_defined_here = report_adt_defined_here(self.tcx, pattern_ty, &witnesses, false);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// https://github.com/rust-lang/rust/issues/123844
2+
// An uninitialized refutable let should not suggest `let else`, as it can't be used with deferred
3+
// initialization.
4+
//
5+
// @check-fail
6+
7+
fn main() {
8+
let Some(x); //~ ERROR refutable pattern in local binding
9+
x = 1;
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
error[E0005]: refutable pattern in local binding
2+
--> $DIR/uninitialized-refutable-let-issue-123844.rs:8:9
3+
|
4+
LL | let Some(x);
5+
| ^^^^^^^ pattern `None` not covered
6+
|
7+
= note: `let` bindings require an "irrefutable pattern", like a `struct` or an `enum` with only one variant
8+
= note: for more information, visit https://doc.rust-lang.org/book/ch18-02-refutability.html
9+
= note: the matched value is of type `Option<i32>`
10+
11+
error: aborting due to 1 previous error
12+
13+
For more information about this error, try `rustc --explain E0005`.

0 commit comments

Comments
 (0)