diff options
| -rw-r--r-- | clippy_lints/src/operators/identity_op.rs | 8 | ||||
| -rw-r--r-- | tests/ui/identity_op.fixed | 12 | ||||
| -rw-r--r-- | tests/ui/identity_op.stderr | 12 |
3 files changed, 13 insertions, 19 deletions
diff --git a/clippy_lints/src/operators/identity_op.rs b/clippy_lints/src/operators/identity_op.rs index 76993785727..1c2d6e90fc9 100644 --- a/clippy_lints/src/operators/identity_op.rs +++ b/clippy_lints/src/operators/identity_op.rs @@ -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, }; diff --git a/tests/ui/identity_op.fixed b/tests/ui/identity_op.fixed index b80ba701c3a..2e8e2366de6 100644 --- a/tests/ui/identity_op.fixed +++ b/tests/ui/identity_op.fixed @@ -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 } diff --git a/tests/ui/identity_op.stderr b/tests/ui/identity_op.stderr index 3f5cab5404d..99a58ef2c1b 100644 --- a/tests/ui/identity_op.stderr +++ b/tests/ui/identity_op.stderr @@ -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 |
