Skip to content

Commit

Permalink
Remove check for duplicate parenthesis
Browse files Browse the repository at this point in the history
It was an incorrect, and could lead to behavior changes in the
suggested code
  • Loading branch information
hcbarker committed Nov 8, 2024
1 parent 1b7239d commit ef0f1ca
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 19 deletions.
8 changes: 1 addition & 7 deletions clippy_lints/src/operators/identity_op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand Down
12 changes: 6 additions & 6 deletions tests/ui/identity_op.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
}
12 changes: 6 additions & 6 deletions tests/ui/identity_op.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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

0 comments on commit ef0f1ca

Please sign in to comment.