rust-clippy/tests/ui/identity_op.stderr
TheSlapstickDictator a0b7681a6f fix: identity_op suggestions use correct parenthesis
The `identity_op` lint was suggesting code fixes that resulted
in incorrect or broken code, due to missing parenthesis in the fix
that changed the semantics of the code.

For a binary expression, `left op right`, if the `left` was redundant,
it would check if the right side needed parenthesis, but if the `right`
was redundant, it would just assume that the left side did not need
parenthesis.

This can result in either rustfix generating broken code and failing,
or code that has different behavior than before the fix.
e.g. `-(x + y + 0)` would turn into `-x + y`, changing the behavior,
and `1u64 + (x + y + 0i32) as u64` where `x: i32` and `y: i32` would
turn into `1u64 + x + y as u64`, creating broken code where `x` cannot
be added to the other values, as it was never cast to `u64`.

This commit fixes both of these cases by always checking the
non-redundant child of a binary expression for needed parenthesis, and
makes it so if we need parenthesis, but they already exist, we don't add
any redundant ones.

Fixes #13470
2024-11-02 19:47:11 -07:00

377 lines
11 KiB
Text

error: this operation has no effect
--> tests/ui/identity_op.rs:44:5
|
LL | x + 0;
| ^^^^^ help: consider reducing it to: `x`
|
= note: `-D clippy::identity-op` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::identity_op)]`
error: this operation has no effect
--> tests/ui/identity_op.rs:46:5
|
LL | x + (1 - 1);
| ^^^^^^^^^^^ help: consider reducing it to: `x`
error: this operation has no effect
--> tests/ui/identity_op.rs:49:5
|
LL | 0 + x;
| ^^^^^ help: consider reducing it to: `x`
error: this operation has no effect
--> tests/ui/identity_op.rs:53:5
|
LL | x | (0);
| ^^^^^^^ help: consider reducing it to: `x`
error: this operation has no effect
--> tests/ui/identity_op.rs:57:5
|
LL | x * 1;
| ^^^^^ help: consider reducing it to: `x`
error: this operation has no effect
--> tests/ui/identity_op.rs:59:5
|
LL | 1 * x;
| ^^^^^ help: consider reducing it to: `x`
error: this operation has no effect
--> tests/ui/identity_op.rs:66:5
|
LL | -1 & x;
| ^^^^^^ help: consider reducing it to: `x`
error: this operation has no effect
--> tests/ui/identity_op.rs:70:5
|
LL | u & 255;
| ^^^^^^^ help: consider reducing it to: `u`
error: this operation has no effect
--> tests/ui/identity_op.rs:74:5
|
LL | 42 << 0;
| ^^^^^^^ help: consider reducing it to: `42`
error: this operation has no effect
--> tests/ui/identity_op.rs:76:5
|
LL | 1 >> 0;
| ^^^^^^ help: consider reducing it to: `1`
error: this operation has no effect
--> tests/ui/identity_op.rs:78:5
|
LL | 42 >> 0;
| ^^^^^^^ help: consider reducing it to: `42`
error: this operation has no effect
--> tests/ui/identity_op.rs:80:5
|
LL | &x >> 0;
| ^^^^^^^ help: consider reducing it to: `x`
error: this operation has no effect
--> tests/ui/identity_op.rs:82:5
|
LL | x >> &0;
| ^^^^^^^ help: consider reducing it to: `x`
error: this operation has no effect
--> tests/ui/identity_op.rs:90:5
|
LL | 2 % 3;
| ^^^^^ help: consider reducing it to: `2`
error: this operation has no effect
--> tests/ui/identity_op.rs:92:5
|
LL | -2 % 3;
| ^^^^^^ help: consider reducing it to: `-2`
error: this operation has no effect
--> tests/ui/identity_op.rs:94:5
|
LL | 2 % -3 + x;
| ^^^^^^ help: consider reducing it to: `2`
error: this operation has no effect
--> tests/ui/identity_op.rs:96:5
|
LL | -2 % -3 + x;
| ^^^^^^^ help: consider reducing it to: `-2`
error: this operation has no effect
--> tests/ui/identity_op.rs:98:9
|
LL | x + 1 % 3;
| ^^^^^ help: consider reducing it to: `1`
error: this operation has no effect
--> tests/ui/identity_op.rs:107:5
|
LL | 0 + 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:109:5
|
LL | 0 + if b { 1 } else { 2 } + if b { 3 } else { 4 };
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider reducing it to: `(if b { 1 } else { 2 })`
error: this operation has no effect
--> tests/ui/identity_op.rs:111:5
|
LL | 0 + match a { 0 => 10, _ => 20 };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider reducing it to: `(match a { 0 => 10, _ => 20 })`
error: this operation has no effect
--> tests/ui/identity_op.rs:113:5
|
LL | 0 + match a { 0 => 10, _ => 20 } + match a { 0 => 30, _ => 40 };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider reducing it to: `(match a { 0 => 10, _ => 20 })`
error: this operation has no effect
--> tests/ui/identity_op.rs:115:5
|
LL | 0 + if b { 1 } else { 2 } + match a { 0 => 30, _ => 40 };
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider reducing it to: `(if b { 1 } else { 2 })`
error: this operation has no effect
--> tests/ui/identity_op.rs:117:5
|
LL | 0 + match a { 0 => 10, _ => 20 } + if b { 3 } else { 4 };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider reducing it to: `(match a { 0 => 10, _ => 20 })`
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 })`
error: this operation has no effect
--> tests/ui/identity_op.rs:122:5
|
LL | 0 + { a } + 3;
| ^^^^^^^^^ help: consider reducing it to: `({ a })`
error: this operation has no effect
--> tests/ui/identity_op.rs:124:5
|
LL | 0 + { a } * 2;
| ^^^^^^^^^^^^^ help: consider reducing it to: `({ a } * 2)`
error: this operation has no effect
--> tests/ui/identity_op.rs:126:5
|
LL | 0 + loop { let mut c = 0; if c == 10 { break c; } c += 1; } + { a * 2 };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider reducing it to: `(loop { let mut c = 0; if c == 10 { break c; } c += 1; })`
error: this operation has no effect
--> tests/ui/identity_op.rs:133:7
|
LL | f(1 * a + { 8 * 5 });
| ^^^^^ help: consider reducing it to: `a`
error: this operation has no effect
--> tests/ui/identity_op.rs:135:7
|
LL | f(0 + if b { 1 } else { 2 } + 3);
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider reducing it to: `if b { 1 } else { 2 }`
error: this operation has no effect
--> tests/ui/identity_op.rs:138:20
|
LL | const _: i32 = { 2 * 4 } + 0 + 3;
| ^^^^^^^^^^^^^ help: consider reducing it to: `{ 2 * 4 }`
error: this operation has no effect
--> tests/ui/identity_op.rs:140:20
|
LL | const _: i32 = 0 + { 1 + 2 * 3 } + 3;
| ^^^^^^^^^^^^^^^^^ help: consider reducing it to: `{ 1 + 2 * 3 }`
error: this operation has no effect
--> tests/ui/identity_op.rs:143:5
|
LL | 0 + a as usize;
| ^^^^^^^^^^^^^^ help: consider reducing it to: `a as usize`
error: this operation has no effect
--> tests/ui/identity_op.rs:145:13
|
LL | let _ = 0 + a as usize;
| ^^^^^^^^^^^^^^ help: consider reducing it to: `a as usize`
error: this operation has no effect
--> tests/ui/identity_op.rs:147:5
|
LL | 0 + { a } as usize;
| ^^^^^^^^^^^^^^^^^^ help: consider reducing it to: `({ a } as usize)`
error: this operation has no effect
--> tests/ui/identity_op.rs:150:9
|
LL | 2 * (0 + { a });
| ^^^^^^^^^^^ help: consider reducing it to: `{ a }`
error: this operation has no effect
--> tests/ui/identity_op.rs:152:5
|
LL | 1 * ({ a } + 4);
| ^^^^^^^^^^^^^^^ help: consider reducing it to: `({ a } + 4)`
error: this operation has no effect
--> tests/ui/identity_op.rs:154:5
|
LL | 1 * 1;
| ^^^^^ help: consider reducing it to: `1`
error: this operation has no effect
--> tests/ui/identity_op.rs:159:18
|
LL | let _: i32 = &x + 0;
| ^^^^^^ help: consider reducing it to: `x`
error: this operation has no effect
--> tests/ui/identity_op.rs:164:5
|
LL | 0 + if a { 1 } else { 2 } + if b { 3 } else { 5 }
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider reducing it to: `(if a { 1 } else { 2 })`
error: this operation has no effect
--> tests/ui/identity_op.rs:175:22
|
LL | let _: i32 = *x + 0;
| ^^^^^^ help: consider reducing it to: `*x`
error: this operation has no effect
--> tests/ui/identity_op.rs:177:22
|
LL | let _: i32 = x + 0;
| ^^^^^ help: consider reducing it to: `*x`
error: this operation has no effect
--> tests/ui/identity_op.rs:182:22
|
LL | let _: i32 = **x + 0;
| ^^^^^^^ help: consider reducing it to: `**x`
error: this operation has no effect
--> tests/ui/identity_op.rs:185:22
|
LL | let _: i32 = *x + 0;
| ^^^^^^ help: consider reducing it to: `**x`
error: this operation has no effect
--> tests/ui/identity_op.rs:191:22
|
LL | let _: i32 = ***x + 0;
| ^^^^^^^^ help: consider reducing it to: `***x`
error: this operation has no effect
--> tests/ui/identity_op.rs:193:22
|
LL | let _: i32 = **x + 0;
| ^^^^^^^ help: consider reducing it to: `***x`
error: this operation has no effect
--> tests/ui/identity_op.rs:196:22
|
LL | let _: i32 = *&x + 0;
| ^^^^^^^ help: consider reducing it to: `*&x`
error: this operation has no effect
--> tests/ui/identity_op.rs:198:22
|
LL | let _: i32 = **&&x + 0;
| ^^^^^^^^^ help: consider reducing it to: `**&&x`
error: this operation has no effect
--> tests/ui/identity_op.rs:200:22
|
LL | let _: i32 = *&*&x + 0;
| ^^^^^^^^^ help: consider reducing it to: `*&*&x`
error: this operation has no effect
--> tests/ui/identity_op.rs:202:22
|
LL | let _: i32 = **&&*&x + 0;
| ^^^^^^^^^^^ help: consider reducing it to: `**&&*&x`
error: this operation has no effect
--> tests/ui/identity_op.rs:209:22
|
LL | let _: i32 = **&&*&x + 0;
| ^^^^^^^^^^^ help: consider reducing it to: `***&&*&x`
error: this operation has no effect
--> tests/ui/identity_op.rs:211:22
|
LL | let _: i32 = **&&*&x + 0;
| ^^^^^^^^^^^ help: consider reducing it to: `***&&*&x`
error: this operation has no effect
--> tests/ui/identity_op.rs:220:18
|
LL | let _: u64 = (x + y + 0i32) as u64;
| ^^^^^^^^^^^^^^ help: consider reducing it to: `(x + y)`
error: this operation has no effect
--> tests/ui/identity_op.rs:223:25
|
LL | let _: u64 = 1u64 & (x + y + 0i32) as u64;
| ^^^^^^^^^^^^^^ help: consider reducing it to: `(x + y)`
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)`
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)`
error: this operation has no effect
--> tests/ui/identity_op.rs:233:14
|
LL | let _ = -(x + y + 0i32);
| ^^^^^^^^^^^^^^ help: consider reducing it to: `(x + y)`
error: this operation has no effect
--> tests/ui/identity_op.rs:236:20
|
LL | let _ = 2i32 * (x + y + 0i32);
| ^^^^^^^^^^^^^^ help: consider reducing it to: `(x + y)`
error: this operation has no effect
--> tests/ui/identity_op.rs:239:20
|
LL | let _ = 2i32 + (x + y + 0i32);
| ^^^^^^^^^^^^^^ help: consider reducing it to: `x + y`
error: this operation has no effect
--> tests/ui/identity_op.rs:243:17
|
LL | let _ = 2 + (x + (y * z) + 0);
| ^^^^^^^^^^^^^^^^^ help: consider reducing it to: `x + (y * z)`
error: this operation has no effect
--> tests/ui/identity_op.rs:247:20
|
LL | let _ = 2i32 + (x * y * 1i32);
| ^^^^^^^^^^^^^^ help: consider reducing it to: `(x * y)`
error: this operation has no effect
--> tests/ui/identity_op.rs:252: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`
error: aborting due to 62 previous errors