Skip to content

Commit 9d5b148

Browse files
committed
Auto merge of #3677 - daxpedda:integer_arithmetic, r=oli-obk
Remove negative integer literal checks. Fixes #3678.
2 parents e0bcec7 + 87d24e1 commit 9d5b148

File tree

3 files changed

+15
-8
lines changed

3 files changed

+15
-8
lines changed

clippy_lints/src/arithmetic.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::consts::constant_simple;
12
use crate::utils::span_lint;
23
use rustc::hir;
34
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
@@ -94,8 +95,10 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Arithmetic {
9495
hir::ExprKind::Unary(hir::UnOp::UnNeg, arg) => {
9596
let ty = cx.tables.expr_ty(arg);
9697
if ty.is_integral() {
97-
span_lint(cx, INTEGER_ARITHMETIC, expr.span, "integer arithmetic detected");
98-
self.expr_span = Some(expr.span);
98+
if constant_simple(cx, cx.tables, expr).is_none() {
99+
span_lint(cx, INTEGER_ARITHMETIC, expr.span, "integer arithmetic detected");
100+
self.expr_span = Some(expr.span);
101+
}
99102
} else if ty.is_floating_point() {
100103
span_lint(cx, FLOAT_ARITHMETIC, expr.span, "floating-point arithmetic detected");
101104
self.expr_span = Some(expr.span);

tests/ui/arithmetic.rs

+4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ fn main() {
1717
i - 2 + 2 - i;
1818
-i;
1919

20+
// no error, overflows are checked by `overflowing_literals`
21+
-1;
22+
-(-1);
23+
2024
i & 1; // no wrapping
2125
i | 1;
2226
i ^ 1;

tests/ui/arithmetic.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -32,39 +32,39 @@ LL | -i;
3232
| ^^
3333

3434
error: floating-point arithmetic detected
35-
--> $DIR/arithmetic.rs:28:5
35+
--> $DIR/arithmetic.rs:32:5
3636
|
3737
LL | f * 2.0;
3838
| ^^^^^^^
3939
|
4040
= note: `-D clippy::float-arithmetic` implied by `-D warnings`
4141

4242
error: floating-point arithmetic detected
43-
--> $DIR/arithmetic.rs:30:5
43+
--> $DIR/arithmetic.rs:34:5
4444
|
4545
LL | 1.0 + f;
4646
| ^^^^^^^
4747

4848
error: floating-point arithmetic detected
49-
--> $DIR/arithmetic.rs:31:5
49+
--> $DIR/arithmetic.rs:35:5
5050
|
5151
LL | f * 2.0;
5252
| ^^^^^^^
5353

5454
error: floating-point arithmetic detected
55-
--> $DIR/arithmetic.rs:32:5
55+
--> $DIR/arithmetic.rs:36:5
5656
|
5757
LL | f / 2.0;
5858
| ^^^^^^^
5959

6060
error: floating-point arithmetic detected
61-
--> $DIR/arithmetic.rs:33:5
61+
--> $DIR/arithmetic.rs:37:5
6262
|
6363
LL | f - 2.0 * 4.2;
6464
| ^^^^^^^^^^^^^
6565

6666
error: floating-point arithmetic detected
67-
--> $DIR/arithmetic.rs:34:5
67+
--> $DIR/arithmetic.rs:38:5
6868
|
6969
LL | -f;
7070
| ^^

0 commit comments

Comments
 (0)