Actually check for constants.

This commit is contained in:
daxpedda 2019-01-21 13:59:49 +01:00
parent 0555ca1c2d
commit 87d24e1fc9
No known key found for this signature in database
GPG Key ID: C722DCB6A191EEAB
3 changed files with 26 additions and 9 deletions

View File

@ -1,3 +1,4 @@
use crate::consts::constant_simple;
use crate::utils::span_lint;
use rustc::hir;
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
@ -92,7 +93,13 @@ fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr) {
}
},
hir::ExprKind::Unary(hir::UnOp::UnNeg, arg) => {
if cx.tables.expr_ty(arg).is_floating_point() {
let ty = cx.tables.expr_ty(arg);
if ty.is_integral() {
if constant_simple(cx, cx.tables, expr).is_none() {
span_lint(cx, INTEGER_ARITHMETIC, expr.span, "integer arithmetic detected");
self.expr_span = Some(expr.span);
}
} else if ty.is_floating_point() {
span_lint(cx, FLOAT_ARITHMETIC, expr.span, "floating-point arithmetic detected");
self.expr_span = Some(expr.span);
}

View File

@ -15,7 +15,11 @@ fn main() {
1 %
i / 2; // no error, this is part of the expression in the preceding line
i - 2 + 2 - i;
-i; // no error, overflows are checked by `overflowing_literals`
-i;
// no error, overflows are checked by `overflowing_literals`
-1;
-(-1);
i & 1; // no wrapping
i | 1;

View File

@ -25,8 +25,14 @@ error: integer arithmetic detected
LL | i - 2 + 2 - i;
| ^^^^^^^^^^^^^
error: integer arithmetic detected
--> $DIR/arithmetic.rs:18:5
|
LL | -i;
| ^^
error: floating-point arithmetic detected
--> $DIR/arithmetic.rs:28:5
--> $DIR/arithmetic.rs:32:5
|
LL | f * 2.0;
| ^^^^^^^
@ -34,34 +40,34 @@ LL | f * 2.0;
= note: `-D clippy::float-arithmetic` implied by `-D warnings`
error: floating-point arithmetic detected
--> $DIR/arithmetic.rs:30:5
--> $DIR/arithmetic.rs:34:5
|
LL | 1.0 + f;
| ^^^^^^^
error: floating-point arithmetic detected
--> $DIR/arithmetic.rs:31:5
--> $DIR/arithmetic.rs:35:5
|
LL | f * 2.0;
| ^^^^^^^
error: floating-point arithmetic detected
--> $DIR/arithmetic.rs:32:5
--> $DIR/arithmetic.rs:36:5
|
LL | f / 2.0;
| ^^^^^^^
error: floating-point arithmetic detected
--> $DIR/arithmetic.rs:33:5
--> $DIR/arithmetic.rs:37:5
|
LL | f - 2.0 * 4.2;
| ^^^^^^^^^^^^^
error: floating-point arithmetic detected
--> $DIR/arithmetic.rs:34:5
--> $DIR/arithmetic.rs:38:5
|
LL | -f;
| ^^
error: aborting due to 10 previous errors
error: aborting due to 11 previous errors