Skip to content

Commit 2fe73ce

Browse files
committed
Fix false positive with if let and ranges
1 parent b381d3a commit 2fe73ce

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

compiler/rustc_lint/src/unused.rs

+12-4
Original file line numberDiff line numberDiff line change
@@ -651,9 +651,11 @@ trait UnusedDelimLint {
651651

652652
fn is_expr_delims_necessary(
653653
inner: &ast::Expr,
654+
ctx: UnusedDelimsCtx,
654655
followed_by_block: bool,
655-
followed_by_else: bool,
656656
) -> bool {
657+
let followed_by_else = ctx == UnusedDelimsCtx::AssignedValueLetElse;
658+
657659
if followed_by_else {
658660
match inner.kind {
659661
ast::ExprKind::Binary(op, ..) if op.node.is_lazy() => return true,
@@ -662,6 +664,13 @@ trait UnusedDelimLint {
662664
}
663665
}
664666

667+
// Check it's range in LetScrutineeExpr
668+
if let ast::ExprKind::Range(..) = inner.kind
669+
&& matches!(ctx, UnusedDelimsCtx::LetScrutineeExpr)
670+
{
671+
return true;
672+
}
673+
665674
// Check if LHS needs parens to prevent false-positives in cases like `fn x() -> u8 { ({ 0 } + 1) }`.
666675
{
667676
let mut innermost = inner;
@@ -1007,8 +1016,7 @@ impl UnusedDelimLint for UnusedParens {
10071016
) {
10081017
match value.kind {
10091018
ast::ExprKind::Paren(ref inner) => {
1010-
let followed_by_else = ctx == UnusedDelimsCtx::AssignedValueLetElse;
1011-
if !Self::is_expr_delims_necessary(inner, followed_by_block, followed_by_else)
1019+
if !Self::is_expr_delims_necessary(inner, ctx, followed_by_block)
10121020
&& value.attrs.is_empty()
10131021
&& !value.span.from_expansion()
10141022
&& (ctx != UnusedDelimsCtx::LetScrutineeExpr
@@ -1334,7 +1342,7 @@ impl UnusedDelimLint for UnusedBraces {
13341342
// FIXME(const_generics): handle paths when #67075 is fixed.
13351343
if let [stmt] = inner.stmts.as_slice() {
13361344
if let ast::StmtKind::Expr(ref expr) = stmt.kind {
1337-
if !Self::is_expr_delims_necessary(expr, followed_by_block, false)
1345+
if !Self::is_expr_delims_necessary(expr, ctx, followed_by_block)
13381346
&& (ctx != UnusedDelimsCtx::AnonConst
13391347
|| (matches!(expr.kind, ast::ExprKind::Lit(_))
13401348
&& !expr.span.from_expansion()))
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// check-pass
2+
3+
#![feature(let_chains)]
4+
#![allow(irrefutable_let_patterns)]
5+
fn main() {
6+
let _a = 0..1;
7+
8+
if let x = (0..1) {
9+
eprintln!("x: {:?}", x);
10+
}
11+
if let x = (0..1) &&
12+
let _y = (0..2)
13+
{
14+
eprintln!("x: {:?}", x);
15+
}
16+
}

0 commit comments

Comments
 (0)