Skip to content

Commit

Permalink
Fixing a missed check for needs_parenthesis in division
Browse files Browse the repository at this point in the history
  • Loading branch information
hcbarker committed Nov 5, 2024
1 parent 6d738f6 commit 1b7239d
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 13 deletions.
9 changes: 2 additions & 7 deletions clippy_lints/src/operators/identity_op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,8 @@ pub(crate) fn check<'tcx>(
},
BinOpKind::Div => {
if is_redundant_op(cx, right, 1) {
span_ineffective_operation(
cx,
expr.span,
peeled_left_span,
Parens::Unneeded,
left_is_coerced_to_value,
);
let paren = needs_parenthesis(cx, expr, left);
span_ineffective_operation(cx, expr.span, peeled_left_span, paren, left_is_coerced_to_value);
}
},
BinOpKind::BitAnd => {
Expand Down
3 changes: 3 additions & 0 deletions tests/ui/identity_op.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,9 @@ fn issue_13470() {
// If we don't maintain the parens here, the behavior changes
let _ = -(x + y);
//~^ ERROR: this operation has no effect
// Similarly, we need to maintain parens here
let _ = -(x / y);
//~^ ERROR: this operation has no effect
// Maintain parenthesis if the parent expr is of higher precedence
let _ = 2i32 * (x + y);
//~^ ERROR: this operation has no effect
Expand Down
3 changes: 3 additions & 0 deletions tests/ui/identity_op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,9 @@ fn issue_13470() {
// If we don't maintain the parens here, the behavior changes
let _ = -(x + y + 0i32);
//~^ ERROR: this operation has no effect
// Similarly, we need to maintain parens here
let _ = -(x / y / 1i32);
//~^ ERROR: this operation has no effect
// Maintain parenthesis if the parent expr is of higher precedence
let _ = 2i32 * (x + y + 0i32);
//~^ ERROR: this operation has no effect
Expand Down
18 changes: 12 additions & 6 deletions tests/ui/identity_op.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -344,34 +344,40 @@ 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
--> tests/ui/identity_op.rs:236:14
|
LL | let _ = -(x / y / 1i32);
| ^^^^^^^^^^^^^^ 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:240:20
--> tests/ui/identity_op.rs:243: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:244:17
--> tests/ui/identity_op.rs:247: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:248:20
--> tests/ui/identity_op.rs:251: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:253:25
--> 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`

error: aborting due to 62 previous errors
error: aborting due to 63 previous errors

0 comments on commit 1b7239d

Please sign in to comment.