Skip to content

Commit 87d24e1

Browse files
committed
Actually check for constants.
1 parent 0555ca1 commit 87d24e1

File tree

3 files changed

+26
-9
lines changed

3 files changed

+26
-9
lines changed

clippy_lints/src/arithmetic.rs

+8-1
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};
@@ -92,7 +93,13 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Arithmetic {
9293
}
9394
},
9495
hir::ExprKind::Unary(hir::UnOp::UnNeg, arg) => {
95-
if cx.tables.expr_ty(arg).is_floating_point() {
96+
let ty = cx.tables.expr_ty(arg);
97+
if ty.is_integral() {
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+
}
102+
} else if ty.is_floating_point() {
96103
span_lint(cx, FLOAT_ARITHMETIC, expr.span, "floating-point arithmetic detected");
97104
self.expr_span = Some(expr.span);
98105
}

tests/ui/arithmetic.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@ fn main() {
1515
1 %
1616
i / 2; // no error, this is part of the expression in the preceding line
1717
i - 2 + 2 - i;
18-
-i; // no error, overflows are checked by `overflowing_literals`
18+
-i;
19+
20+
// no error, overflows are checked by `overflowing_literals`
21+
-1;
22+
-(-1);
1923

2024
i & 1; // no wrapping
2125
i | 1;

tests/ui/arithmetic.stderr

+13-7
Original file line numberDiff line numberDiff line change
@@ -25,43 +25,49 @@ error: integer arithmetic detected
2525
LL | i - 2 + 2 - i;
2626
| ^^^^^^^^^^^^^
2727

28+
error: integer arithmetic detected
29+
--> $DIR/arithmetic.rs:18:5
30+
|
31+
LL | -i;
32+
| ^^
33+
2834
error: floating-point arithmetic detected
29-
--> $DIR/arithmetic.rs:28:5
35+
--> $DIR/arithmetic.rs:32:5
3036
|
3137
LL | f * 2.0;
3238
| ^^^^^^^
3339
|
3440
= note: `-D clippy::float-arithmetic` implied by `-D warnings`
3541

3642
error: floating-point arithmetic detected
37-
--> $DIR/arithmetic.rs:30:5
43+
--> $DIR/arithmetic.rs:34:5
3844
|
3945
LL | 1.0 + f;
4046
| ^^^^^^^
4147

4248
error: floating-point arithmetic detected
43-
--> $DIR/arithmetic.rs:31:5
49+
--> $DIR/arithmetic.rs:35:5
4450
|
4551
LL | f * 2.0;
4652
| ^^^^^^^
4753

4854
error: floating-point arithmetic detected
49-
--> $DIR/arithmetic.rs:32:5
55+
--> $DIR/arithmetic.rs:36:5
5056
|
5157
LL | f / 2.0;
5258
| ^^^^^^^
5359

5460
error: floating-point arithmetic detected
55-
--> $DIR/arithmetic.rs:33:5
61+
--> $DIR/arithmetic.rs:37:5
5662
|
5763
LL | f - 2.0 * 4.2;
5864
| ^^^^^^^^^^^^^
5965

6066
error: floating-point arithmetic detected
61-
--> $DIR/arithmetic.rs:34:5
67+
--> $DIR/arithmetic.rs:38:5
6268
|
6369
LL | -f;
6470
| ^^
6571

66-
error: aborting due to 10 previous errors
72+
error: aborting due to 11 previous errors
6773

0 commit comments

Comments
 (0)