fix false positives for unused_parens
around unary and binary operations
This commit is contained in:
parent
e14b81f10d
commit
8df1f41b9c
4 changed files with 64 additions and 36 deletions
|
@ -569,36 +569,48 @@ trait UnusedDelimLint {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prevent false-positives in cases like `fn x() -> u8 { ({ 0 } + 1) }`
|
// Check if LHS needs parens to prevent false-positives in cases like `fn x() -> u8 { ({ 0 } + 1) }`.
|
||||||
let lhs_needs_parens = {
|
{
|
||||||
let mut innermost = inner;
|
let mut innermost = inner;
|
||||||
loop {
|
loop {
|
||||||
innermost = match &innermost.kind {
|
innermost = match &innermost.kind {
|
||||||
ExprKind::Binary(_, lhs, _rhs) => lhs,
|
ExprKind::Binary(_op, lhs, _rhs) => lhs,
|
||||||
ExprKind::Call(fn_, _params) => fn_,
|
ExprKind::Call(fn_, _params) => fn_,
|
||||||
ExprKind::Cast(expr, _ty) => expr,
|
ExprKind::Cast(expr, _ty) => expr,
|
||||||
ExprKind::Type(expr, _ty) => expr,
|
ExprKind::Type(expr, _ty) => expr,
|
||||||
ExprKind::Index(base, _subscript) => base,
|
ExprKind::Index(base, _subscript) => base,
|
||||||
_ => break false,
|
_ => break,
|
||||||
};
|
};
|
||||||
if !classify::expr_requires_semi_to_be_stmt(innermost) {
|
if !classify::expr_requires_semi_to_be_stmt(innermost) {
|
||||||
break true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
lhs_needs_parens
|
// Check if RHS needs parens to prevent false-positives in cases like `if (() == return) {}`.
|
||||||
|| (followed_by_block
|
if !followed_by_block {
|
||||||
&& match &inner.kind {
|
return false;
|
||||||
ExprKind::Ret(_)
|
}
|
||||||
| ExprKind::Break(..)
|
let mut innermost = inner;
|
||||||
| ExprKind::Yield(..)
|
loop {
|
||||||
| ExprKind::Yeet(..) => true,
|
innermost = match &innermost.kind {
|
||||||
ExprKind::Range(_lhs, Some(rhs), _limits) => {
|
ExprKind::Unary(_op, expr) => expr,
|
||||||
matches!(rhs.kind, ExprKind::Block(..))
|
ExprKind::Binary(_op, _lhs, rhs) => rhs,
|
||||||
}
|
ExprKind::AssignOp(_op, _lhs, rhs) => rhs,
|
||||||
_ => parser::contains_exterior_struct_lit(&inner),
|
ExprKind::Assign(_lhs, rhs, _span) => rhs,
|
||||||
})
|
|
||||||
|
ExprKind::Ret(_)
|
||||||
|
| ExprKind::Break(..)
|
||||||
|
| ExprKind::Yield(..)
|
||||||
|
| ExprKind::Yeet(..) => return true,
|
||||||
|
|
||||||
|
ExprKind::Range(_lhs, Some(rhs), _limits) => {
|
||||||
|
return matches!(rhs.kind, ExprKind::Block(..));
|
||||||
|
}
|
||||||
|
|
||||||
|
_ => return parser::contains_exterior_struct_lit(&inner),
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn emit_unused_delims_expr(
|
fn emit_unused_delims_expr(
|
||||||
|
|
|
@ -32,6 +32,14 @@ fn _no_lint_yeet() -> Result<(), ()> {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn _no_lint_ops() {
|
||||||
|
#![allow(unreachable_code, irrefutable_let_patterns)]
|
||||||
|
if ((..{}) == ..{}) {}
|
||||||
|
if (!return) {}
|
||||||
|
loop { match (() = () = () = break {}) {} }
|
||||||
|
while let () = (*&mut false |= true && return) {}
|
||||||
|
}
|
||||||
|
|
||||||
// Don't lint in these cases (#64106).
|
// Don't lint in these cases (#64106).
|
||||||
fn or_patterns_no_lint() {
|
fn or_patterns_no_lint() {
|
||||||
match Box::new(0) {
|
match Box::new(0) {
|
||||||
|
|
|
@ -32,6 +32,14 @@ fn _no_lint_yeet() -> Result<(), ()> {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn _no_lint_ops() {
|
||||||
|
#![allow(unreachable_code, irrefutable_let_patterns)]
|
||||||
|
if ((..{}) == ..{}) {}
|
||||||
|
if (!return) {}
|
||||||
|
loop { match (() = () = () = break {}) {} }
|
||||||
|
while let () = (*&mut false |= true && return) {}
|
||||||
|
}
|
||||||
|
|
||||||
// Don't lint in these cases (#64106).
|
// Don't lint in these cases (#64106).
|
||||||
fn or_patterns_no_lint() {
|
fn or_patterns_no_lint() {
|
||||||
match Box::new(0) {
|
match Box::new(0) {
|
||||||
|
|
|
@ -76,7 +76,7 @@ LL + let _ = |a: u8| 0;
|
||||||
|
|
|
|
||||||
|
|
||||||
error: unnecessary parentheses around pattern
|
error: unnecessary parentheses around pattern
|
||||||
--> $DIR/issue-54538-unused-parens-lint.rs:56:12
|
--> $DIR/issue-54538-unused-parens-lint.rs:64:12
|
||||||
|
|
|
|
||||||
LL | if let (0 | 1) = 0 {}
|
LL | if let (0 | 1) = 0 {}
|
||||||
| ^ ^
|
| ^ ^
|
||||||
|
@ -88,7 +88,7 @@ LL + if let 0 | 1 = 0 {}
|
||||||
|
|
|
|
||||||
|
|
||||||
error: unnecessary parentheses around pattern
|
error: unnecessary parentheses around pattern
|
||||||
--> $DIR/issue-54538-unused-parens-lint.rs:57:13
|
--> $DIR/issue-54538-unused-parens-lint.rs:65:13
|
||||||
|
|
|
|
||||||
LL | if let ((0 | 1),) = (0,) {}
|
LL | if let ((0 | 1),) = (0,) {}
|
||||||
| ^ ^
|
| ^ ^
|
||||||
|
@ -100,7 +100,7 @@ LL + if let (0 | 1,) = (0,) {}
|
||||||
|
|
|
|
||||||
|
|
||||||
error: unnecessary parentheses around pattern
|
error: unnecessary parentheses around pattern
|
||||||
--> $DIR/issue-54538-unused-parens-lint.rs:58:13
|
--> $DIR/issue-54538-unused-parens-lint.rs:66:13
|
||||||
|
|
|
|
||||||
LL | if let [(0 | 1)] = [0] {}
|
LL | if let [(0 | 1)] = [0] {}
|
||||||
| ^ ^
|
| ^ ^
|
||||||
|
@ -112,7 +112,7 @@ LL + if let [0 | 1] = [0] {}
|
||||||
|
|
|
|
||||||
|
|
||||||
error: unnecessary parentheses around pattern
|
error: unnecessary parentheses around pattern
|
||||||
--> $DIR/issue-54538-unused-parens-lint.rs:59:16
|
--> $DIR/issue-54538-unused-parens-lint.rs:67:16
|
||||||
|
|
|
|
||||||
LL | if let 0 | (1 | 2) = 0 {}
|
LL | if let 0 | (1 | 2) = 0 {}
|
||||||
| ^ ^
|
| ^ ^
|
||||||
|
@ -124,7 +124,7 @@ LL + if let 0 | 1 | 2 = 0 {}
|
||||||
|
|
|
|
||||||
|
|
||||||
error: unnecessary parentheses around pattern
|
error: unnecessary parentheses around pattern
|
||||||
--> $DIR/issue-54538-unused-parens-lint.rs:61:15
|
--> $DIR/issue-54538-unused-parens-lint.rs:69:15
|
||||||
|
|
|
|
||||||
LL | if let TS((0 | 1)) = TS(0) {}
|
LL | if let TS((0 | 1)) = TS(0) {}
|
||||||
| ^ ^
|
| ^ ^
|
||||||
|
@ -136,7 +136,7 @@ LL + if let TS(0 | 1) = TS(0) {}
|
||||||
|
|
|
|
||||||
|
|
||||||
error: unnecessary parentheses around pattern
|
error: unnecessary parentheses around pattern
|
||||||
--> $DIR/issue-54538-unused-parens-lint.rs:63:20
|
--> $DIR/issue-54538-unused-parens-lint.rs:71:20
|
||||||
|
|
|
|
||||||
LL | if let NS { f: (0 | 1) } = (NS { f: 0 }) {}
|
LL | if let NS { f: (0 | 1) } = (NS { f: 0 }) {}
|
||||||
| ^ ^
|
| ^ ^
|
||||||
|
@ -148,7 +148,7 @@ LL + if let NS { f: 0 | 1 } = (NS { f: 0 }) {}
|
||||||
|
|
|
|
||||||
|
|
||||||
error: unnecessary parentheses around pattern
|
error: unnecessary parentheses around pattern
|
||||||
--> $DIR/issue-54538-unused-parens-lint.rs:73:9
|
--> $DIR/issue-54538-unused-parens-lint.rs:81:9
|
||||||
|
|
|
|
||||||
LL | (_) => {}
|
LL | (_) => {}
|
||||||
| ^ ^
|
| ^ ^
|
||||||
|
@ -160,7 +160,7 @@ LL + _ => {}
|
||||||
|
|
|
|
||||||
|
|
||||||
error: unnecessary parentheses around pattern
|
error: unnecessary parentheses around pattern
|
||||||
--> $DIR/issue-54538-unused-parens-lint.rs:74:9
|
--> $DIR/issue-54538-unused-parens-lint.rs:82:9
|
||||||
|
|
|
|
||||||
LL | (y) => {}
|
LL | (y) => {}
|
||||||
| ^ ^
|
| ^ ^
|
||||||
|
@ -172,7 +172,7 @@ LL + y => {}
|
||||||
|
|
|
|
||||||
|
|
||||||
error: unnecessary parentheses around pattern
|
error: unnecessary parentheses around pattern
|
||||||
--> $DIR/issue-54538-unused-parens-lint.rs:75:9
|
--> $DIR/issue-54538-unused-parens-lint.rs:83:9
|
||||||
|
|
|
|
||||||
LL | (ref r) => {}
|
LL | (ref r) => {}
|
||||||
| ^ ^
|
| ^ ^
|
||||||
|
@ -184,7 +184,7 @@ LL + ref r => {}
|
||||||
|
|
|
|
||||||
|
|
||||||
error: unnecessary parentheses around pattern
|
error: unnecessary parentheses around pattern
|
||||||
--> $DIR/issue-54538-unused-parens-lint.rs:76:9
|
--> $DIR/issue-54538-unused-parens-lint.rs:84:9
|
||||||
|
|
|
|
||||||
LL | (e @ 1...2) => {}
|
LL | (e @ 1...2) => {}
|
||||||
| ^ ^
|
| ^ ^
|
||||||
|
@ -196,7 +196,7 @@ LL + e @ 1...2 => {}
|
||||||
|
|
|
|
||||||
|
|
||||||
error: unnecessary parentheses around pattern
|
error: unnecessary parentheses around pattern
|
||||||
--> $DIR/issue-54538-unused-parens-lint.rs:82:9
|
--> $DIR/issue-54538-unused-parens-lint.rs:90:9
|
||||||
|
|
|
|
||||||
LL | (e @ &(1...2)) => {}
|
LL | (e @ &(1...2)) => {}
|
||||||
| ^ ^
|
| ^ ^
|
||||||
|
@ -208,7 +208,7 @@ LL + e @ &(1...2) => {}
|
||||||
|
|
|
|
||||||
|
|
||||||
error: unnecessary parentheses around pattern
|
error: unnecessary parentheses around pattern
|
||||||
--> $DIR/issue-54538-unused-parens-lint.rs:83:10
|
--> $DIR/issue-54538-unused-parens-lint.rs:91:10
|
||||||
|
|
|
|
||||||
LL | &(_) => {}
|
LL | &(_) => {}
|
||||||
| ^ ^
|
| ^ ^
|
||||||
|
@ -220,7 +220,7 @@ LL + &_ => {}
|
||||||
|
|
|
|
||||||
|
|
||||||
error: unnecessary parentheses around pattern
|
error: unnecessary parentheses around pattern
|
||||||
--> $DIR/issue-54538-unused-parens-lint.rs:94:9
|
--> $DIR/issue-54538-unused-parens-lint.rs:102:9
|
||||||
|
|
|
|
||||||
LL | (_) => {}
|
LL | (_) => {}
|
||||||
| ^ ^
|
| ^ ^
|
||||||
|
@ -232,7 +232,7 @@ LL + _ => {}
|
||||||
|
|
|
|
||||||
|
|
||||||
error: unnecessary parentheses around pattern
|
error: unnecessary parentheses around pattern
|
||||||
--> $DIR/issue-54538-unused-parens-lint.rs:95:9
|
--> $DIR/issue-54538-unused-parens-lint.rs:103:9
|
||||||
|
|
|
|
||||||
LL | (y) => {}
|
LL | (y) => {}
|
||||||
| ^ ^
|
| ^ ^
|
||||||
|
@ -244,7 +244,7 @@ LL + y => {}
|
||||||
|
|
|
|
||||||
|
|
||||||
error: unnecessary parentheses around pattern
|
error: unnecessary parentheses around pattern
|
||||||
--> $DIR/issue-54538-unused-parens-lint.rs:96:9
|
--> $DIR/issue-54538-unused-parens-lint.rs:104:9
|
||||||
|
|
|
|
||||||
LL | (ref r) => {}
|
LL | (ref r) => {}
|
||||||
| ^ ^
|
| ^ ^
|
||||||
|
@ -256,7 +256,7 @@ LL + ref r => {}
|
||||||
|
|
|
|
||||||
|
|
||||||
error: unnecessary parentheses around pattern
|
error: unnecessary parentheses around pattern
|
||||||
--> $DIR/issue-54538-unused-parens-lint.rs:97:9
|
--> $DIR/issue-54538-unused-parens-lint.rs:105:9
|
||||||
|
|
|
|
||||||
LL | (e @ 1..=2) => {}
|
LL | (e @ 1..=2) => {}
|
||||||
| ^ ^
|
| ^ ^
|
||||||
|
@ -268,7 +268,7 @@ LL + e @ 1..=2 => {}
|
||||||
|
|
|
|
||||||
|
|
||||||
error: unnecessary parentheses around pattern
|
error: unnecessary parentheses around pattern
|
||||||
--> $DIR/issue-54538-unused-parens-lint.rs:103:9
|
--> $DIR/issue-54538-unused-parens-lint.rs:111:9
|
||||||
|
|
|
|
||||||
LL | (e @ &(1..=2)) => {}
|
LL | (e @ &(1..=2)) => {}
|
||||||
| ^ ^
|
| ^ ^
|
||||||
|
@ -280,7 +280,7 @@ LL + e @ &(1..=2) => {}
|
||||||
|
|
|
|
||||||
|
|
||||||
error: unnecessary parentheses around pattern
|
error: unnecessary parentheses around pattern
|
||||||
--> $DIR/issue-54538-unused-parens-lint.rs:104:10
|
--> $DIR/issue-54538-unused-parens-lint.rs:112:10
|
||||||
|
|
|
|
||||||
LL | &(_) => {}
|
LL | &(_) => {}
|
||||||
| ^ ^
|
| ^ ^
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue