mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-24 05:33:27 +00:00
Emit lint when rhs is negative
This commit is contained in:
parent
a944ccb677
commit
52b563b283
3 changed files with 20 additions and 5 deletions
|
@ -54,7 +54,7 @@ impl<'tcx> LateLintPass<'tcx> for IdentityOp {
|
||||||
check(cx, left, -1, e.span, right.span);
|
check(cx, left, -1, e.span, right.span);
|
||||||
check(cx, right, -1, e.span, left.span);
|
check(cx, right, -1, e.span, left.span);
|
||||||
},
|
},
|
||||||
BinOpKind::Rem => check_modulo(cx, left, right, e.span, left.span),
|
BinOpKind::Rem => check_remainder(cx, left, right, e.span, left.span),
|
||||||
_ => (),
|
_ => (),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -71,11 +71,11 @@ fn is_allowed(cx: &LateContext<'_>, cmp: BinOp, left: &Expr<'_>, right: &Expr<'_
|
||||||
&& constant_simple(cx, cx.typeck_results(), left) == Some(Constant::Int(1)))
|
&& constant_simple(cx, cx.typeck_results(), left) == Some(Constant::Int(1)))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn check_modulo(cx: &LateContext<'_>, left: &Expr<'_>, right: &Expr<'_>, span: Span, arg: Span) {
|
fn check_remainder(cx: &LateContext<'_>, left: &Expr<'_>, right: &Expr<'_>, span: Span, arg: Span) {
|
||||||
let lhs_const = constant_full_int(cx, cx.typeck_results(), left);
|
let lhs_const = constant_full_int(cx, cx.typeck_results(), left);
|
||||||
let rhs_const = constant_full_int(cx, cx.typeck_results(), right);
|
let rhs_const = constant_full_int(cx, cx.typeck_results(), right);
|
||||||
if match (lhs_const, rhs_const) {
|
if match (lhs_const, rhs_const) {
|
||||||
(Some(FullInt::S(lv)), Some(FullInt::S(rv))) => lv.abs() < rv,
|
(Some(FullInt::S(lv)), Some(FullInt::S(rv))) => lv.abs() < rv.abs(),
|
||||||
(Some(FullInt::U(lv)), Some(FullInt::U(rv))) => lv < rv,
|
(Some(FullInt::U(lv)), Some(FullInt::U(rv))) => lv < rv,
|
||||||
_ => return,
|
_ => return,
|
||||||
} {
|
} {
|
||||||
|
|
|
@ -69,7 +69,10 @@ fn main() {
|
||||||
|
|
||||||
2 % 3;
|
2 % 3;
|
||||||
-2 % 3;
|
-2 % 3;
|
||||||
|
2 % -3 + x;
|
||||||
|
-2 % -3 + x;
|
||||||
x + 1 % 3;
|
x + 1 % 3;
|
||||||
(x + 1) % 3; // no error
|
(x + 1) % 3; // no error
|
||||||
4 % 3; // no error
|
4 % 3; // no error
|
||||||
|
4 % -3; // no error
|
||||||
}
|
}
|
||||||
|
|
|
@ -90,11 +90,23 @@ error: the operation is ineffective. Consider reducing it to `-2`
|
||||||
LL | -2 % 3;
|
LL | -2 % 3;
|
||||||
| ^^^^^^
|
| ^^^^^^
|
||||||
|
|
||||||
|
error: the operation is ineffective. Consider reducing it to `2`
|
||||||
|
--> $DIR/identity_op.rs:72:5
|
||||||
|
|
|
||||||
|
LL | 2 % -3 + x;
|
||||||
|
| ^^^^^^
|
||||||
|
|
||||||
|
error: the operation is ineffective. Consider reducing it to `-2`
|
||||||
|
--> $DIR/identity_op.rs:73:5
|
||||||
|
|
|
||||||
|
LL | -2 % -3 + x;
|
||||||
|
| ^^^^^^^
|
||||||
|
|
||||||
error: the operation is ineffective. Consider reducing it to `1`
|
error: the operation is ineffective. Consider reducing it to `1`
|
||||||
--> $DIR/identity_op.rs:72:9
|
--> $DIR/identity_op.rs:74:9
|
||||||
|
|
|
|
||||||
LL | x + 1 % 3;
|
LL | x + 1 % 3;
|
||||||
| ^^^^^
|
| ^^^^^
|
||||||
|
|
||||||
error: aborting due to 16 previous errors
|
error: aborting due to 18 previous errors
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue