Skip to content

Commit c0939b1

Browse files
committed
Auto merge of #12409 - cookie-s:fix-identityop-duplicate-errors, r=Alexendoo
[`identity_op`]: Fix duplicate diagnostics Relates to #12379 In the `identity_op` lint, the following diagnostic was emitted two times ``` --> tests/ui/identity_op.rs:156:5 | LL | 1 * 1; | ^^^^^ help: consider reducing it to: `1` | ``` because both of the left operand and the right operand are the identity element of the multiplication. This PR fixes the issue so that if a diagnostic is created for an operand, the check of the other operand will be skipped. It's fine because the result is always the same in the affected operators. --- changelog: [`identity_op`]: Fix duplicate diagnostics
2 parents e970fa5 + 3b9939e commit c0939b1

File tree

4 files changed

+73
-76
lines changed

4 files changed

+73
-76
lines changed

Diff for: clippy_lints/src/operators/identity_op.rs

+21-20
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,15 @@ pub(crate) fn check<'tcx>(
4242

4343
match op {
4444
BinOpKind::Add | BinOpKind::BitOr | BinOpKind::BitXor => {
45-
check_op(
45+
let _ = check_op(
4646
cx,
4747
left,
4848
0,
4949
expr.span,
5050
peeled_right_span,
5151
needs_parenthesis(cx, expr, right),
5252
right_is_coerced_to_value,
53-
);
54-
check_op(
53+
) || check_op(
5554
cx,
5655
right,
5756
0,
@@ -62,7 +61,7 @@ pub(crate) fn check<'tcx>(
6261
);
6362
},
6463
BinOpKind::Shl | BinOpKind::Shr | BinOpKind::Sub => {
65-
check_op(
64+
let _ = check_op(
6665
cx,
6766
right,
6867
0,
@@ -73,16 +72,26 @@ pub(crate) fn check<'tcx>(
7372
);
7473
},
7574
BinOpKind::Mul => {
76-
check_op(
75+
let _ = check_op(
7776
cx,
7877
left,
7978
1,
8079
expr.span,
8180
peeled_right_span,
8281
needs_parenthesis(cx, expr, right),
8382
right_is_coerced_to_value,
83+
) || check_op(
84+
cx,
85+
right,
86+
1,
87+
expr.span,
88+
peeled_left_span,
89+
Parens::Unneeded,
90+
left_is_coerced_to_value,
8491
);
85-
check_op(
92+
},
93+
BinOpKind::Div => {
94+
let _ = check_op(
8695
cx,
8796
right,
8897
1,
@@ -92,26 +101,16 @@ pub(crate) fn check<'tcx>(
92101
left_is_coerced_to_value,
93102
);
94103
},
95-
BinOpKind::Div => check_op(
96-
cx,
97-
right,
98-
1,
99-
expr.span,
100-
peeled_left_span,
101-
Parens::Unneeded,
102-
left_is_coerced_to_value,
103-
),
104104
BinOpKind::BitAnd => {
105-
check_op(
105+
let _ = check_op(
106106
cx,
107107
left,
108108
-1,
109109
expr.span,
110110
peeled_right_span,
111111
needs_parenthesis(cx, expr, right),
112112
right_is_coerced_to_value,
113-
);
114-
check_op(
113+
) || check_op(
115114
cx,
116115
right,
117116
-1,
@@ -201,12 +200,12 @@ fn check_remainder(cx: &LateContext<'_>, left: &Expr<'_>, right: &Expr<'_>, span
201200
}
202201
}
203202

204-
fn check_op(cx: &LateContext<'_>, e: &Expr<'_>, m: i8, span: Span, arg: Span, parens: Parens, is_erased: bool) {
203+
fn check_op(cx: &LateContext<'_>, e: &Expr<'_>, m: i8, span: Span, arg: Span, parens: Parens, is_erased: bool) -> bool {
205204
if let Some(Constant::Int(v)) = constant_simple(cx, cx.typeck_results(), e).map(Constant::peel_refs) {
206205
let check = match *cx.typeck_results().expr_ty(e).peel_refs().kind() {
207206
ty::Int(ity) => unsext(cx.tcx, -1_i128, ity),
208207
ty::Uint(uty) => clip(cx.tcx, !0, uty),
209-
_ => return,
208+
_ => return false,
210209
};
211210
if match m {
212211
0 => v == 0,
@@ -215,8 +214,10 @@ fn check_op(cx: &LateContext<'_>, e: &Expr<'_>, m: i8, span: Span, arg: Span, pa
215214
_ => unreachable!(),
216215
} {
217216
span_ineffective_operation(cx, span, arg, parens, is_erased);
217+
return true;
218218
}
219219
}
220+
false
220221
}
221222

222223
fn span_ineffective_operation(

Diff for: tests/ui/identity_op.fixed

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
//@compile-flags: -Zdeduplicate-diagnostics=yes
2-
31
#![warn(clippy::identity_op)]
42
#![allow(unused)]
53
#![allow(

Diff for: tests/ui/identity_op.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
//@compile-flags: -Zdeduplicate-diagnostics=yes
2-
31
#![warn(clippy::identity_op)]
42
#![allow(unused)]
53
#![allow(

0 commit comments

Comments
 (0)