Skip to content

Commit 1b7239d

Browse files
committed
Fixing a missed check for needs_parenthesis in division
1 parent 6d738f6 commit 1b7239d

File tree

4 files changed

+20
-13
lines changed

4 files changed

+20
-13
lines changed

clippy_lints/src/operators/identity_op.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,8 @@ pub(crate) fn check<'tcx>(
6767
},
6868
BinOpKind::Div => {
6969
if is_redundant_op(cx, right, 1) {
70-
span_ineffective_operation(
71-
cx,
72-
expr.span,
73-
peeled_left_span,
74-
Parens::Unneeded,
75-
left_is_coerced_to_value,
76-
);
70+
let paren = needs_parenthesis(cx, expr, left);
71+
span_ineffective_operation(cx, expr.span, peeled_left_span, paren, left_is_coerced_to_value);
7772
}
7873
},
7974
BinOpKind::BitAnd => {

tests/ui/identity_op.fixed

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,9 @@ fn issue_13470() {
232232
// If we don't maintain the parens here, the behavior changes
233233
let _ = -(x + y);
234234
//~^ ERROR: this operation has no effect
235+
// Similarly, we need to maintain parens here
236+
let _ = -(x / y);
237+
//~^ ERROR: this operation has no effect
235238
// Maintain parenthesis if the parent expr is of higher precedence
236239
let _ = 2i32 * (x + y);
237240
//~^ ERROR: this operation has no effect

tests/ui/identity_op.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,9 @@ fn issue_13470() {
232232
// If we don't maintain the parens here, the behavior changes
233233
let _ = -(x + y + 0i32);
234234
//~^ ERROR: this operation has no effect
235+
// Similarly, we need to maintain parens here
236+
let _ = -(x / y / 1i32);
237+
//~^ ERROR: this operation has no effect
235238
// Maintain parenthesis if the parent expr is of higher precedence
236239
let _ = 2i32 * (x + y + 0i32);
237240
//~^ ERROR: this operation has no effect

tests/ui/identity_op.stderr

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -344,34 +344,40 @@ LL | let _ = -(x + y + 0i32);
344344
| ^^^^^^^^^^^^^^ help: consider reducing it to: `(x + y)`
345345

346346
error: this operation has no effect
347-
--> tests/ui/identity_op.rs:236:20
347+
--> tests/ui/identity_op.rs:236:14
348+
|
349+
LL | let _ = -(x / y / 1i32);
350+
| ^^^^^^^^^^^^^^ help: consider reducing it to: `(x / y)`
351+
352+
error: this operation has no effect
353+
--> tests/ui/identity_op.rs:239:20
348354
|
349355
LL | let _ = 2i32 * (x + y + 0i32);
350356
| ^^^^^^^^^^^^^^ help: consider reducing it to: `(x + y)`
351357

352358
error: this operation has no effect
353-
--> tests/ui/identity_op.rs:240:20
359+
--> tests/ui/identity_op.rs:243:20
354360
|
355361
LL | let _ = 2i32 - (x - y - 0i32);
356362
| ^^^^^^^^^^^^^^ help: consider reducing it to: `(x - y)`
357363

358364
error: this operation has no effect
359-
--> tests/ui/identity_op.rs:244:17
365+
--> tests/ui/identity_op.rs:247:17
360366
|
361367
LL | let _ = 2 + (x + (y * z) + 0);
362368
| ^^^^^^^^^^^^^^^^^ help: consider reducing it to: `x + (y * z)`
363369

364370
error: this operation has no effect
365-
--> tests/ui/identity_op.rs:248:20
371+
--> tests/ui/identity_op.rs:251:20
366372
|
367373
LL | let _ = 2i32 + (x * y * 1i32);
368374
| ^^^^^^^^^^^^^^ help: consider reducing it to: `(x * y)`
369375

370376
error: this operation has no effect
371-
--> tests/ui/identity_op.rs:253:25
377+
--> tests/ui/identity_op.rs:256:25
372378
|
373379
LL | let _: u64 = 1u64 + ((x as i32 + y as i32) as u64 + 0u64);
374380
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider reducing it to: `(x as i32 + y as i32) as u64`
375381

376-
error: aborting due to 62 previous errors
382+
error: aborting due to 63 previous errors
377383

0 commit comments

Comments
 (0)