Skip to content

Commit ef0f1ca

Browse files
committed
Remove check for duplicate parenthesis
It was an incorrect, and could lead to behavior changes in the suggested code
1 parent 1b7239d commit ef0f1ca

File tree

3 files changed

+13
-19
lines changed

3 files changed

+13
-19
lines changed

clippy_lints/src/operators/identity_op.rs

+1-7
Original file line numberDiff line numberDiff line change
@@ -220,13 +220,7 @@ fn span_ineffective_operation(
220220
expr_snippet.into_owned()
221221
};
222222
let suggestion = match parens {
223-
Parens::Needed => {
224-
if !expr_snippet.starts_with('(') && !expr_snippet.ends_with(')') {
225-
format!("({expr_snippet})")
226-
} else {
227-
expr_snippet
228-
}
229-
},
223+
Parens::Needed => format!("({expr_snippet})"),
230224
Parens::Unneeded => expr_snippet,
231225
};
232226

tests/ui/identity_op.fixed

+6-6
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ fn main() {
116116
//~^ ERROR: this operation has no effect
117117
(match a { 0 => 10, _ => 20 }) + if b { 3 } else { 4 };
118118
//~^ ERROR: this operation has no effect
119-
(if b { 1 } else { 2 });
119+
((if b { 1 } else { 2 }));
120120
//~^ ERROR: this operation has no effect
121121

122122
({ a }) + 3;
@@ -149,7 +149,7 @@ fn main() {
149149

150150
2 * { a };
151151
//~^ ERROR: this operation has no effect
152-
({ a } + 4);
152+
(({ a } + 4));
153153
//~^ ERROR: this operation has no effect
154154
1;
155155
//~^ ERROR: this operation has no effect
@@ -223,10 +223,10 @@ fn issue_13470() {
223223
let _: u64 = 1u64 & (x + y) as u64;
224224
//~^ ERROR: this operation has no effect
225225
// Same as above, but with extra redundant parenthesis
226-
let _: u64 = 1u64 & (x + y) as u64;
226+
let _: u64 = 1u64 & ((x + y)) as u64;
227227
//~^ ERROR: this operation has no effect
228228
// Should maintain parenthesis even if the surrounding expr has the same precedence
229-
let _: u64 = 5u64 + (x + y) as u64;
229+
let _: u64 = 5u64 + ((x + y)) as u64;
230230
//~^ ERROR: this operation has no effect
231231

232232
// If we don't maintain the parens here, the behavior changes
@@ -244,7 +244,7 @@ fn issue_13470() {
244244
//~^ ERROR: this operation has no effect
245245
// But make sure that inner parens still exist
246246
let z = 1i32;
247-
let _ = 2 + x + (y * z);
247+
let _ = 2 + (x + (y * z));
248248
//~^ ERROR: this operation has no effect
249249
// Maintain parenthesis if the parent expr is of lower precedence
250250
// This is for clarity, and clippy will not warn on these being unnecessary
@@ -253,6 +253,6 @@ fn issue_13470() {
253253

254254
let x = 1i16;
255255
let y = 1i16;
256-
let _: u64 = 1u64 + (x as i32 + y as i32) as u64;
256+
let _: u64 = 1u64 + ((x as i32 + y as i32) as u64);
257257
//~^ ERROR: this operation has no effect
258258
}

tests/ui/identity_op.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ error: this operation has no effect
149149
--> tests/ui/identity_op.rs:119:5
150150
|
151151
LL | (if b { 1 } else { 2 }) + 0;
152-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider reducing it to: `(if b { 1 } else { 2 })`
152+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider reducing it to: `((if b { 1 } else { 2 }))`
153153

154154
error: this operation has no effect
155155
--> tests/ui/identity_op.rs:122:5
@@ -221,7 +221,7 @@ error: this operation has no effect
221221
--> tests/ui/identity_op.rs:152:5
222222
|
223223
LL | 1 * ({ a } + 4);
224-
| ^^^^^^^^^^^^^^^ help: consider reducing it to: `({ a } + 4)`
224+
| ^^^^^^^^^^^^^^^ help: consider reducing it to: `(({ a } + 4))`
225225

226226
error: this operation has no effect
227227
--> tests/ui/identity_op.rs:154:5
@@ -329,13 +329,13 @@ error: this operation has no effect
329329
--> tests/ui/identity_op.rs:226:25
330330
|
331331
LL | let _: u64 = 1u64 & ((x + y) + 0i32) as u64;
332-
| ^^^^^^^^^^^^^^^^ help: consider reducing it to: `(x + y)`
332+
| ^^^^^^^^^^^^^^^^ help: consider reducing it to: `((x + y))`
333333

334334
error: this operation has no effect
335335
--> tests/ui/identity_op.rs:229:25
336336
|
337337
LL | let _: u64 = 5u64 + ((x + y) + 0i32) as u64;
338-
| ^^^^^^^^^^^^^^^^ help: consider reducing it to: `(x + y)`
338+
| ^^^^^^^^^^^^^^^^ help: consider reducing it to: `((x + y))`
339339

340340
error: this operation has no effect
341341
--> tests/ui/identity_op.rs:233:14
@@ -365,7 +365,7 @@ error: this operation has no effect
365365
--> tests/ui/identity_op.rs:247:17
366366
|
367367
LL | let _ = 2 + (x + (y * z) + 0);
368-
| ^^^^^^^^^^^^^^^^^ help: consider reducing it to: `x + (y * z)`
368+
| ^^^^^^^^^^^^^^^^^ help: consider reducing it to: `(x + (y * z))`
369369

370370
error: this operation has no effect
371371
--> tests/ui/identity_op.rs:251:20
@@ -377,7 +377,7 @@ error: this operation has no effect
377377
--> tests/ui/identity_op.rs:256:25
378378
|
379379
LL | let _: u64 = 1u64 + ((x as i32 + y as i32) as u64 + 0u64);
380-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider reducing it to: `(x as i32 + y as i32) as u64`
380+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider reducing it to: `((x as i32 + y as i32) as u64)`
381381

382382
error: aborting due to 63 previous errors
383383

0 commit comments

Comments
 (0)