Skip to content

Commit 5a3a722

Browse files
committed
Auto merge of rust-lang#10004 - Jarcho:issue_9904, r=llogiq
Don't suggest keeping borrows in `identity_op` fixes rust-lang#9904 changelog: `identity_op`: Don't suggest keeping borrows
2 parents d4cd91c + 03ba0ea commit 5a3a722

File tree

4 files changed

+83
-13
lines changed

4 files changed

+83
-13
lines changed

clippy_lints/src/operators/identity_op.rs

Lines changed: 65 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use clippy_utils::consts::{constant_full_int, constant_simple, Constant, FullInt};
22
use clippy_utils::diagnostics::span_lint_and_sugg;
33
use clippy_utils::source::snippet_with_applicability;
4-
use clippy_utils::{clip, unsext};
4+
use clippy_utils::{clip, peel_hir_expr_refs, unsext};
55
use rustc_errors::Applicability;
66
use rustc_hir::{BinOpKind, Expr, ExprKind, Node};
77
use rustc_lint::LateContext;
@@ -20,20 +20,76 @@ pub(crate) fn check<'tcx>(
2020
if !is_allowed(cx, op, left, right) {
2121
match op {
2222
BinOpKind::Add | BinOpKind::BitOr | BinOpKind::BitXor => {
23-
check_op(cx, left, 0, expr.span, right.span, needs_parenthesis(cx, expr, right));
24-
check_op(cx, right, 0, expr.span, left.span, Parens::Unneeded);
23+
check_op(
24+
cx,
25+
left,
26+
0,
27+
expr.span,
28+
peel_hir_expr_refs(right).0.span,
29+
needs_parenthesis(cx, expr, right),
30+
);
31+
check_op(
32+
cx,
33+
right,
34+
0,
35+
expr.span,
36+
peel_hir_expr_refs(left).0.span,
37+
Parens::Unneeded,
38+
);
2539
},
2640
BinOpKind::Shl | BinOpKind::Shr | BinOpKind::Sub => {
27-
check_op(cx, right, 0, expr.span, left.span, Parens::Unneeded);
41+
check_op(
42+
cx,
43+
right,
44+
0,
45+
expr.span,
46+
peel_hir_expr_refs(left).0.span,
47+
Parens::Unneeded,
48+
);
2849
},
2950
BinOpKind::Mul => {
30-
check_op(cx, left, 1, expr.span, right.span, needs_parenthesis(cx, expr, right));
31-
check_op(cx, right, 1, expr.span, left.span, Parens::Unneeded);
51+
check_op(
52+
cx,
53+
left,
54+
1,
55+
expr.span,
56+
peel_hir_expr_refs(right).0.span,
57+
needs_parenthesis(cx, expr, right),
58+
);
59+
check_op(
60+
cx,
61+
right,
62+
1,
63+
expr.span,
64+
peel_hir_expr_refs(left).0.span,
65+
Parens::Unneeded,
66+
);
3267
},
33-
BinOpKind::Div => check_op(cx, right, 1, expr.span, left.span, Parens::Unneeded),
68+
BinOpKind::Div => check_op(
69+
cx,
70+
right,
71+
1,
72+
expr.span,
73+
peel_hir_expr_refs(left).0.span,
74+
Parens::Unneeded,
75+
),
3476
BinOpKind::BitAnd => {
35-
check_op(cx, left, -1, expr.span, right.span, needs_parenthesis(cx, expr, right));
36-
check_op(cx, right, -1, expr.span, left.span, Parens::Unneeded);
77+
check_op(
78+
cx,
79+
left,
80+
-1,
81+
expr.span,
82+
peel_hir_expr_refs(right).0.span,
83+
needs_parenthesis(cx, expr, right),
84+
);
85+
check_op(
86+
cx,
87+
right,
88+
-1,
89+
expr.span,
90+
peel_hir_expr_refs(left).0.span,
91+
Parens::Unneeded,
92+
);
3793
},
3894
BinOpKind::Rem => check_remainder(cx, left, right, expr.span, left.span),
3995
_ => (),

tests/ui/identity_op.fixed

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ fn main() {
6565
42;
6666
1;
6767
42;
68-
&x;
68+
x;
6969
x;
7070

7171
let mut a = A(String::new());
@@ -112,6 +112,10 @@ fn main() {
112112
2 * { a };
113113
(({ a } + 4));
114114
1;
115+
116+
// Issue #9904
117+
let x = 0i32;
118+
let _: i32 = x;
115119
}
116120

117121
pub fn decide(a: bool, b: bool) -> u32 {

tests/ui/identity_op.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,10 @@ fn main() {
112112
2 * (0 + { a });
113113
1 * ({ a } + 4);
114114
1 * 1;
115+
116+
// Issue #9904
117+
let x = 0i32;
118+
let _: i32 = &x + 0;
115119
}
116120

117121
pub fn decide(a: bool, b: bool) -> u32 {

tests/ui/identity_op.stderr

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ error: this operation has no effect
7070
--> $DIR/identity_op.rs:68:5
7171
|
7272
LL | &x >> 0;
73-
| ^^^^^^^ help: consider reducing it to: `&x`
73+
| ^^^^^^^ help: consider reducing it to: `x`
7474

7575
error: this operation has no effect
7676
--> $DIR/identity_op.rs:69:5
@@ -229,10 +229,16 @@ LL | 1 * 1;
229229
| ^^^^^ help: consider reducing it to: `1`
230230

231231
error: this operation has no effect
232-
--> $DIR/identity_op.rs:118:5
232+
--> $DIR/identity_op.rs:118:18
233+
|
234+
LL | let _: i32 = &x + 0;
235+
| ^^^^^^ help: consider reducing it to: `x`
236+
237+
error: this operation has no effect
238+
--> $DIR/identity_op.rs:122:5
233239
|
234240
LL | 0 + if a { 1 } else { 2 } + if b { 3 } else { 5 }
235241
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider reducing it to: `(if a { 1 } else { 2 })`
236242

237-
error: aborting due to 39 previous errors
243+
error: aborting due to 40 previous errors
238244

0 commit comments

Comments
 (0)