Skip to content

Commit 52b563b

Browse files
committed
Emit lint when rhs is negative
1 parent a944ccb commit 52b563b

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

clippy_lints/src/identity_op.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ impl<'tcx> LateLintPass<'tcx> for IdentityOp {
5454
check(cx, left, -1, e.span, right.span);
5555
check(cx, right, -1, e.span, left.span);
5656
},
57-
BinOpKind::Rem => check_modulo(cx, left, right, e.span, left.span),
57+
BinOpKind::Rem => check_remainder(cx, left, right, e.span, left.span),
5858
_ => (),
5959
}
6060
}
@@ -71,11 +71,11 @@ fn is_allowed(cx: &LateContext<'_>, cmp: BinOp, left: &Expr<'_>, right: &Expr<'_
7171
&& constant_simple(cx, cx.typeck_results(), left) == Some(Constant::Int(1)))
7272
}
7373

74-
fn check_modulo(cx: &LateContext<'_>, left: &Expr<'_>, right: &Expr<'_>, span: Span, arg: Span) {
74+
fn check_remainder(cx: &LateContext<'_>, left: &Expr<'_>, right: &Expr<'_>, span: Span, arg: Span) {
7575
let lhs_const = constant_full_int(cx, cx.typeck_results(), left);
7676
let rhs_const = constant_full_int(cx, cx.typeck_results(), right);
7777
if match (lhs_const, rhs_const) {
78-
(Some(FullInt::S(lv)), Some(FullInt::S(rv))) => lv.abs() < rv,
78+
(Some(FullInt::S(lv)), Some(FullInt::S(rv))) => lv.abs() < rv.abs(),
7979
(Some(FullInt::U(lv)), Some(FullInt::U(rv))) => lv < rv,
8080
_ => return,
8181
} {

tests/ui/identity_op.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,10 @@ fn main() {
6969

7070
2 % 3;
7171
-2 % 3;
72+
2 % -3 + x;
73+
-2 % -3 + x;
7274
x + 1 % 3;
7375
(x + 1) % 3; // no error
7476
4 % 3; // no error
77+
4 % -3; // no error
7578
}

tests/ui/identity_op.stderr

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,23 @@ error: the operation is ineffective. Consider reducing it to `-2`
9090
LL | -2 % 3;
9191
| ^^^^^^
9292

93+
error: the operation is ineffective. Consider reducing it to `2`
94+
--> $DIR/identity_op.rs:72:5
95+
|
96+
LL | 2 % -3 + x;
97+
| ^^^^^^
98+
99+
error: the operation is ineffective. Consider reducing it to `-2`
100+
--> $DIR/identity_op.rs:73:5
101+
|
102+
LL | -2 % -3 + x;
103+
| ^^^^^^^
104+
93105
error: the operation is ineffective. Consider reducing it to `1`
94-
--> $DIR/identity_op.rs:72:9
106+
--> $DIR/identity_op.rs:74:9
95107
|
96108
LL | x + 1 % 3;
97109
| ^^^^^
98110

99-
error: aborting due to 16 previous errors
111+
error: aborting due to 18 previous errors
100112

0 commit comments

Comments
 (0)