Skip to content

Commit

Permalink
Reduce span of let else irrefutable_let_patterns warning
Browse files Browse the repository at this point in the history
Huge spans aren't good for IDE users as they underline constructs that
are possibly multiline.
  • Loading branch information
est31 committed Oct 30, 2022
1 parent 5e97720 commit 7b55d17
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 15 deletions.
18 changes: 8 additions & 10 deletions compiler/rustc_mir_build/src/thir/pattern/check_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,10 @@ impl<'tcx> Visitor<'tcx> for MatchVisitor<'_, '_, 'tcx> {
intravisit::walk_local(self, loc);
let els = loc.els;
if let Some(init) = loc.init && els.is_some() {
self.check_let(&loc.pat, init, loc.span);
// Build a span without the else { ... } as we don't want to underline
// the entire else block in the IDE setting.
let span = loc.span.with_hi(init.span.hi());
self.check_let(&loc.pat, init, span);
}

let (msg, sp) = match loc.source {
Expand Down Expand Up @@ -630,11 +633,6 @@ fn irrefutable_let_patterns(
count: usize,
span: Span,
) {
let span = match source {
LetSource::LetElse(span) => span,
_ => span,
};

macro_rules! emit_diag {
(
$lint:expr,
Expand Down Expand Up @@ -680,7 +678,7 @@ fn irrefutable_let_patterns(
"removing the guard and adding a `let` inside the match arm"
);
}
LetSource::LetElse(..) => {
LetSource::LetElse => {
emit_diag!(
lint,
"`let...else`",
Expand Down Expand Up @@ -1127,7 +1125,7 @@ pub enum LetSource {
GenericLet,
IfLet,
IfLetGuard,
LetElse(Span),
LetElse,
WhileLet,
}

Expand Down Expand Up @@ -1156,8 +1154,8 @@ fn let_source_parent(tcx: TyCtxt<'_>, parent: HirId, pat_id: Option<HirId>) -> L
let parent_parent = hir.get_parent_node(parent);
let parent_parent_node = hir.get(parent_parent);
match parent_parent_node {
hir::Node::Stmt(hir::Stmt { kind: hir::StmtKind::Local(_), span, .. }) => {
return LetSource::LetElse(*span);
hir::Node::Stmt(hir::Stmt { kind: hir::StmtKind::Local(_), .. }) => {
return LetSource::LetElse;
}
hir::Node::Arm(hir::Arm { guard: Some(hir::Guard::If(_)), .. }) => {
return LetSource::IfLetGuard;
Expand Down
8 changes: 6 additions & 2 deletions src/test/ui/let-else/let-else-irrefutable.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
// check-pass



fn main() {
let x = 1 else { return }; //~ WARN irrefutable `let...else` pattern

// Multiline else blocks should not get printed
let x = 1 else { //~ WARN irrefutable `let...else` pattern
eprintln!("problem case encountered");
return
};
}
15 changes: 12 additions & 3 deletions src/test/ui/let-else/let-else-irrefutable.stderr
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
warning: irrefutable `let...else` pattern
--> $DIR/let-else-irrefutable.rs:6:5
--> $DIR/let-else-irrefutable.rs:4:5
|
LL | let x = 1 else { return };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^
|
= note: this pattern will always match, so the `else` clause is useless
= help: consider removing the `else` clause
= note: `#[warn(irrefutable_let_patterns)]` on by default

warning: 1 warning emitted
warning: irrefutable `let...else` pattern
--> $DIR/let-else-irrefutable.rs:7:5
|
LL | let x = 1 else {
| ^^^^^^^^^
|
= note: this pattern will always match, so the `else` clause is useless
= help: consider removing the `else` clause

warning: 2 warnings emitted

0 comments on commit 7b55d17

Please sign in to comment.