Skip to content

Commit 6d738f6

Browse files
committed
Fix parens getting removed for non-associative operators
1 parent a0b7681 commit 6d738f6

File tree

4 files changed

+18
-26
lines changed

4 files changed

+18
-26
lines changed

clippy_lints/src/operators/identity_op.rs

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,8 @@ pub(crate) fn check<'tcx>(
5252
},
5353
BinOpKind::Shl | BinOpKind::Shr | BinOpKind::Sub => {
5454
if is_redundant_op(cx, right, 0) {
55-
span_ineffective_operation(
56-
cx,
57-
expr.span,
58-
peeled_left_span,
59-
Parens::Unneeded,
60-
left_is_coerced_to_value,
61-
);
55+
let paren = needs_parenthesis(cx, expr, left);
56+
span_ineffective_operation(cx, expr.span, peeled_left_span, paren, left_is_coerced_to_value);
6257
}
6358
},
6459
BinOpKind::Mul => {
@@ -127,17 +122,12 @@ fn needs_parenthesis(cx: &LateContext<'_>, binary: &Expr<'_>, child: &Expr<'_>)
127122
match child.kind {
128123
ExprKind::Binary(_, lhs, _) | ExprKind::Cast(lhs, _) => {
129124
// For casts and binary expressions, we want to add parenthesis if
130-
// the parent HIR node is an expression with a different precedence,
131-
// or if the parent HIR node is a Block or Stmt, and the new left hand side
132-
// would be treated as a statement rather than an expression.
125+
// the parent HIR node is an expression, or if the parent HIR node
126+
// is a Block or Stmt, and the new left hand side would need
127+
// parenthesis be treated as a statement rather than an expression.
133128
if let Some((_, parent)) = cx.tcx.hir().parent_iter(binary.hir_id).next() {
134-
if let Node::Expr(expr) = parent {
135-
if child.precedence().order() != expr.precedence().order() {
136-
return Parens::Needed;
137-
}
138-
return Parens::Unneeded;
139-
}
140129
match parent {
130+
Node::Expr(_) => return Parens::Needed,
141131
Node::Block(_) | Node::Stmt(_) => {
142132
// ensure we're checking against the leftmost expression of `child`
143133
//

tests/ui/identity_op.fixed

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,9 @@ fn issue_13470() {
235235
// Maintain parenthesis if the parent expr is of higher precedence
236236
let _ = 2i32 * (x + y);
237237
//~^ ERROR: this operation has no effect
238-
// No need for parenthesis if the parent expr is of equal precedence
239-
let _ = 2i32 + x + y;
238+
// Maintain parenthesis if the parent expr is the same precedence
239+
// as not all operations are associative
240+
let _ = 2i32 - (x - y);
240241
//~^ ERROR: this operation has no effect
241242
// But make sure that inner parens still exist
242243
let z = 1i32;

tests/ui/identity_op.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,9 @@ fn issue_13470() {
235235
// Maintain parenthesis if the parent expr is of higher precedence
236236
let _ = 2i32 * (x + y + 0i32);
237237
//~^ ERROR: this operation has no effect
238-
// No need for parenthesis if the parent expr is of equal precedence
239-
let _ = 2i32 + (x + y + 0i32);
238+
// Maintain parenthesis if the parent expr is the same precedence
239+
// as not all operations are associative
240+
let _ = 2i32 - (x - y - 0i32);
240241
//~^ ERROR: this operation has no effect
241242
// But make sure that inner parens still exist
242243
let z = 1i32;

tests/ui/identity_op.stderr

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -350,25 +350,25 @@ LL | let _ = 2i32 * (x + y + 0i32);
350350
| ^^^^^^^^^^^^^^ help: consider reducing it to: `(x + y)`
351351

352352
error: this operation has no effect
353-
--> tests/ui/identity_op.rs:239:20
353+
--> tests/ui/identity_op.rs:240:20
354354
|
355-
LL | let _ = 2i32 + (x + y + 0i32);
356-
| ^^^^^^^^^^^^^^ help: consider reducing it to: `x + y`
355+
LL | let _ = 2i32 - (x - y - 0i32);
356+
| ^^^^^^^^^^^^^^ help: consider reducing it to: `(x - y)`
357357

358358
error: this operation has no effect
359-
--> tests/ui/identity_op.rs:243:17
359+
--> tests/ui/identity_op.rs:244:17
360360
|
361361
LL | let _ = 2 + (x + (y * z) + 0);
362362
| ^^^^^^^^^^^^^^^^^ help: consider reducing it to: `x + (y * z)`
363363

364364
error: this operation has no effect
365-
--> tests/ui/identity_op.rs:247:20
365+
--> tests/ui/identity_op.rs:248:20
366366
|
367367
LL | let _ = 2i32 + (x * y * 1i32);
368368
| ^^^^^^^^^^^^^^ help: consider reducing it to: `(x * y)`
369369

370370
error: this operation has no effect
371-
--> tests/ui/identity_op.rs:252:25
371+
--> tests/ui/identity_op.rs:253:25
372372
|
373373
LL | let _: u64 = 1u64 + ((x as i32 + y as i32) as u64 + 0u64);
374374
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider reducing it to: `(x as i32 + y as i32) as u64`

0 commit comments

Comments
 (0)