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.
This commit is contained in:
est31 2022-10-30 04:02:10 +01:00
parent 5e97720429
commit 7b55d17a2f
3 changed files with 26 additions and 15 deletions

View File

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

View File

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

View File

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