Skip to content

Reduce span of let else irrefutable_let_patterns warning #103749

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 1, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
@@ -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 {
@@ -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,
@@ -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`",
@@ -1127,7 +1125,7 @@ pub enum LetSource {
GenericLet,
IfLet,
IfLetGuard,
LetElse(Span),
LetElse,
WhileLet,
}

@@ -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;
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