Remove check for duplicate parenthesis

It was an incorrect, and could lead to behavior changes in the
suggested code
This commit is contained in:
TheSlapstickDictator 2024-11-07 20:45:15 -08:00
parent 1b7239d954
commit ef0f1cad59
3 changed files with 13 additions and 19 deletions

View file

@ -220,13 +220,7 @@ fn span_ineffective_operation(
expr_snippet.into_owned()
};
let suggestion = match parens {
Parens::Needed => {
if !expr_snippet.starts_with('(') && !expr_snippet.ends_with(')') {
format!("({expr_snippet})")
} else {
expr_snippet
}
},
Parens::Needed => format!("({expr_snippet})"),
Parens::Unneeded => expr_snippet,
};

View file

@ -116,7 +116,7 @@ fn main() {
//~^ ERROR: this operation has no effect
(match a { 0 => 10, _ => 20 }) + if b { 3 } else { 4 };
//~^ ERROR: this operation has no effect
(if b { 1 } else { 2 });
((if b { 1 } else { 2 }));
//~^ ERROR: this operation has no effect
({ a }) + 3;
@ -149,7 +149,7 @@ fn main() {
2 * { a };
//~^ ERROR: this operation has no effect
({ a } + 4);
(({ a } + 4));
//~^ ERROR: this operation has no effect
1;
//~^ ERROR: this operation has no effect
@ -223,10 +223,10 @@ fn issue_13470() {
let _: u64 = 1u64 & (x + y) as u64;
//~^ ERROR: this operation has no effect
// Same as above, but with extra redundant parenthesis
let _: u64 = 1u64 & (x + y) as u64;
let _: u64 = 1u64 & ((x + y)) as u64;
//~^ ERROR: this operation has no effect
// Should maintain parenthesis even if the surrounding expr has the same precedence
let _: u64 = 5u64 + (x + y) as u64;
let _: u64 = 5u64 + ((x + y)) as u64;
//~^ ERROR: this operation has no effect
// If we don't maintain the parens here, the behavior changes
@ -244,7 +244,7 @@ fn issue_13470() {
//~^ ERROR: this operation has no effect
// But make sure that inner parens still exist
let z = 1i32;
let _ = 2 + x + (y * z);
let _ = 2 + (x + (y * z));
//~^ ERROR: this operation has no effect
// Maintain parenthesis if the parent expr is of lower precedence
// This is for clarity, and clippy will not warn on these being unnecessary
@ -253,6 +253,6 @@ fn issue_13470() {
let x = 1i16;
let y = 1i16;
let _: u64 = 1u64 + (x as i32 + y as i32) as u64;
let _: u64 = 1u64 + ((x as i32 + y as i32) as u64);
//~^ ERROR: this operation has no effect
}

View file

@ -149,7 +149,7 @@ error: this operation has no effect
--> tests/ui/identity_op.rs:119:5
|
LL | (if b { 1 } else { 2 }) + 0;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider reducing it to: `(if b { 1 } else { 2 })`
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider reducing it to: `((if b { 1 } else { 2 }))`
error: this operation has no effect
--> tests/ui/identity_op.rs:122:5
@ -221,7 +221,7 @@ error: this operation has no effect
--> tests/ui/identity_op.rs:152:5
|
LL | 1 * ({ a } + 4);
| ^^^^^^^^^^^^^^^ help: consider reducing it to: `({ a } + 4)`
| ^^^^^^^^^^^^^^^ help: consider reducing it to: `(({ a } + 4))`
error: this operation has no effect
--> tests/ui/identity_op.rs:154:5
@ -329,13 +329,13 @@ error: this operation has no effect
--> tests/ui/identity_op.rs:226:25
|
LL | let _: u64 = 1u64 & ((x + y) + 0i32) as u64;
| ^^^^^^^^^^^^^^^^ help: consider reducing it to: `(x + y)`
| ^^^^^^^^^^^^^^^^ help: consider reducing it to: `((x + y))`
error: this operation has no effect
--> tests/ui/identity_op.rs:229:25
|
LL | let _: u64 = 5u64 + ((x + y) + 0i32) as u64;
| ^^^^^^^^^^^^^^^^ help: consider reducing it to: `(x + y)`
| ^^^^^^^^^^^^^^^^ help: consider reducing it to: `((x + y))`
error: this operation has no effect
--> tests/ui/identity_op.rs:233:14
@ -365,7 +365,7 @@ error: this operation has no effect
--> tests/ui/identity_op.rs:247:17
|
LL | let _ = 2 + (x + (y * z) + 0);
| ^^^^^^^^^^^^^^^^^ help: consider reducing it to: `x + (y * z)`
| ^^^^^^^^^^^^^^^^^ help: consider reducing it to: `(x + (y * z))`
error: this operation has no effect
--> tests/ui/identity_op.rs:251:20
@ -377,7 +377,7 @@ error: this operation has no effect
--> tests/ui/identity_op.rs:256:25
|
LL | let _: u64 = 1u64 + ((x as i32 + y as i32) as u64 + 0u64);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider reducing it to: `(x as i32 + y as i32) as u64`
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider reducing it to: `((x as i32 + y as i32) as u64)`
error: aborting due to 63 previous errors