Skip to content

Commit e054442

Browse files
committed
Suggest add let when no let in refutable bindings
Signed-off-by: xizheyin <[email protected]>
1 parent b7f3316 commit e054442

File tree

3 files changed

+14
-9
lines changed

3 files changed

+14
-9
lines changed

compiler/rustc_mir_build/src/errors.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1062,13 +1062,11 @@ pub(crate) enum SuggestLet {
10621062
semi_span: Span,
10631063
count: usize,
10641064
},
1065-
#[suggestion(
1066-
mir_build_suggest_let_else,
1067-
code = " else {{ todo!() }}",
1068-
applicability = "has-placeholders"
1069-
)]
1065+
#[multipart_suggestion(mir_build_suggest_let_else, applicability = "has-placeholders")]
10701066
Else {
1071-
#[primary_span]
1067+
#[suggestion_part(code = "let ")]
1068+
let_span: Option<Span>,
1069+
#[suggestion_part(code = " else {{ todo!() }}")]
10721070
end_span: Span,
10731071
count: usize,
10741072
},

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -727,7 +727,14 @@ impl<'p, 'tcx> MatchVisitor<'p, 'tcx> {
727727
let_suggestion = Some(if bindings.is_empty() {
728728
SuggestLet::If { start_span, semi_span, count }
729729
} else {
730-
SuggestLet::Else { end_span, count }
730+
let let_span = if let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(span)
731+
&& !snippet.trim_start().starts_with("let")
732+
{
733+
Some(start_span)
734+
} else {
735+
None
736+
};
737+
SuggestLet::Else { let_span, end_span, count }
731738
});
732739
};
733740

tests/ui/let-else/suggest-with-let-issue-145548.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ LL | Some(x) = Some(1);
99
= note: the matched value is of type `Option<i32>`
1010
help: you might want to use `let else` to handle the variant that isn't matched
1111
|
12-
LL | Some(x) = Some(1) else { todo!() };
13-
| ++++++++++++++++
12+
LL | let Some(x) = Some(1) else { todo!() };
13+
| +++ ++++++++++++++++
1414

1515
error[E0005]: refutable pattern in local binding
1616
--> $DIR/suggest-with-let-issue-145548.rs:11:9

0 commit comments

Comments
 (0)