Skip to content

Commit 2a6a8f4

Browse files
author
Yuki Okushi
authored
Rollup merge of #103749 - est31:reduce_irrefutable_let_else_span, r=cjgillot
Reduce span of let else irrefutable_let_patterns warning Huge spans aren't good for IDE users as they underline constructs that are possibly multiline. Similar PR to #90761 which did the same for the `unused_macros` lint.
2 parents 2125181 + 7b55d17 commit 2a6a8f4

File tree

3 files changed

+26
-15
lines changed

3 files changed

+26
-15
lines changed

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

+8-10
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,10 @@ impl<'tcx> Visitor<'tcx> for MatchVisitor<'_, '_, 'tcx> {
7979
intravisit::walk_local(self, loc);
8080
let els = loc.els;
8181
if let Some(init) = loc.init && els.is_some() {
82-
self.check_let(&loc.pat, init, loc.span);
82+
// Build a span without the else { ... } as we don't want to underline
83+
// the entire else block in the IDE setting.
84+
let span = loc.span.with_hi(init.span.hi());
85+
self.check_let(&loc.pat, init, span);
8386
}
8487

8588
let (msg, sp) = match loc.source {
@@ -630,11 +633,6 @@ fn irrefutable_let_patterns(
630633
count: usize,
631634
span: Span,
632635
) {
633-
let span = match source {
634-
LetSource::LetElse(span) => span,
635-
_ => span,
636-
};
637-
638636
macro_rules! emit_diag {
639637
(
640638
$lint:expr,
@@ -680,7 +678,7 @@ fn irrefutable_let_patterns(
680678
"removing the guard and adding a `let` inside the match arm"
681679
);
682680
}
683-
LetSource::LetElse(..) => {
681+
LetSource::LetElse => {
684682
emit_diag!(
685683
lint,
686684
"`let...else`",
@@ -1127,7 +1125,7 @@ pub enum LetSource {
11271125
GenericLet,
11281126
IfLet,
11291127
IfLetGuard,
1130-
LetElse(Span),
1128+
LetElse,
11311129
WhileLet,
11321130
}
11331131

@@ -1156,8 +1154,8 @@ fn let_source_parent(tcx: TyCtxt<'_>, parent: HirId, pat_id: Option<HirId>) -> L
11561154
let parent_parent = hir.get_parent_node(parent);
11571155
let parent_parent_node = hir.get(parent_parent);
11581156
match parent_parent_node {
1159-
hir::Node::Stmt(hir::Stmt { kind: hir::StmtKind::Local(_), span, .. }) => {
1160-
return LetSource::LetElse(*span);
1157+
hir::Node::Stmt(hir::Stmt { kind: hir::StmtKind::Local(_), .. }) => {
1158+
return LetSource::LetElse;
11611159
}
11621160
hir::Node::Arm(hir::Arm { guard: Some(hir::Guard::If(_)), .. }) => {
11631161
return LetSource::IfLetGuard;
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
// check-pass
22

3-
4-
53
fn main() {
64
let x = 1 else { return }; //~ WARN irrefutable `let...else` pattern
5+
6+
// Multiline else blocks should not get printed
7+
let x = 1 else { //~ WARN irrefutable `let...else` pattern
8+
eprintln!("problem case encountered");
9+
return
10+
};
711
}
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
11
warning: irrefutable `let...else` pattern
2-
--> $DIR/let-else-irrefutable.rs:6:5
2+
--> $DIR/let-else-irrefutable.rs:4:5
33
|
44
LL | let x = 1 else { return };
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
5+
| ^^^^^^^^^
66
|
77
= note: this pattern will always match, so the `else` clause is useless
88
= help: consider removing the `else` clause
99
= note: `#[warn(irrefutable_let_patterns)]` on by default
1010

11-
warning: 1 warning emitted
11+
warning: irrefutable `let...else` pattern
12+
--> $DIR/let-else-irrefutable.rs:7:5
13+
|
14+
LL | let x = 1 else {
15+
| ^^^^^^^^^
16+
|
17+
= note: this pattern will always match, so the `else` clause is useless
18+
= help: consider removing the `else` clause
19+
20+
warning: 2 warnings emitted
1221

0 commit comments

Comments
 (0)