Auto merge of #5329 - matthiaskrgr:int_arith_on_ref_5328, r=flip1995

integer_arithmetic: detect integer arithmetic on references.

changelog: integer_arithmetic fix false negatives with references on integers

Fixes #5328
This commit is contained in:
bors 2020-03-18 14:56:05 +00:00
commit e004b9431d
11 changed files with 355 additions and 184 deletions

View File

@ -4,6 +4,8 @@ use std::path::{Path, PathBuf};
use walkdir::WalkDir;
use clippy_dev::clippy_project_root;
// The maximum length allowed for stderr files.
//
// We limit this because small files are easier to deal with than bigger files.
@ -14,22 +16,24 @@ pub fn check() {
if !exceeding_files.is_empty() {
eprintln!("Error: stderr files exceeding limit of {} lines:", LENGTH_LIMIT);
for path in exceeding_files {
println!("{}", path.display());
for (path, count) in exceeding_files {
println!("{}: {}", path.display(), count);
}
std::process::exit(1);
}
}
fn exceeding_stderr_files() -> Vec<PathBuf> {
fn exceeding_stderr_files() -> Vec<(PathBuf, usize)> {
// We use `WalkDir` instead of `fs::read_dir` here in order to recurse into subdirectories.
WalkDir::new("../tests/ui")
WalkDir::new(clippy_project_root().join("tests/ui"))
.into_iter()
.filter_map(Result::ok)
.filter(|f| !f.file_type().is_dir())
.filter_map(|e| {
let p = e.into_path();
if p.extension() == Some(OsStr::new("stderr")) && count_linenumbers(&p) > LENGTH_LIMIT {
Some(p)
let count = count_linenumbers(&p);
if p.extension() == Some(OsStr::new("stderr")) && count > LENGTH_LIMIT {
Some((p, count))
} else {
None
}

View File

@ -81,11 +81,12 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Arithmetic {
| hir::BinOpKind::Gt => return,
_ => (),
}
let (l_ty, r_ty) = (cx.tables.expr_ty(l), cx.tables.expr_ty(r));
if l_ty.is_integral() && r_ty.is_integral() {
if l_ty.peel_refs().is_integral() && r_ty.peel_refs().is_integral() {
span_lint(cx, INTEGER_ARITHMETIC, expr.span, "integer arithmetic detected");
self.expr_span = Some(expr.span);
} else if l_ty.is_floating_point() && r_ty.is_floating_point() {
} else if l_ty.peel_refs().is_floating_point() && r_ty.peel_refs().is_floating_point() {
span_lint(cx, FLOAT_ARITHMETIC, expr.span, "floating-point arithmetic detected");
self.expr_span = Some(expr.span);
}

View File

@ -1,127 +0,0 @@
error: integer arithmetic detected
--> $DIR/arithmetic.rs:13:5
|
LL | 1 + i;
| ^^^^^
|
= note: `-D clippy::integer-arithmetic` implied by `-D warnings`
error: integer arithmetic detected
--> $DIR/arithmetic.rs:14:5
|
LL | i * 2;
| ^^^^^
error: integer arithmetic detected
--> $DIR/arithmetic.rs:15:5
|
LL | / 1 %
LL | | i / 2; // no error, this is part of the expression in the preceding line
| |_________^
error: integer arithmetic detected
--> $DIR/arithmetic.rs:17:5
|
LL | i - 2 + 2 - i;
| ^^^^^^^^^^^^^
error: integer arithmetic detected
--> $DIR/arithmetic.rs:18:5
|
LL | -i;
| ^^
error: integer arithmetic detected
--> $DIR/arithmetic.rs:30:5
|
LL | i += 1;
| ^^^^^^
error: integer arithmetic detected
--> $DIR/arithmetic.rs:31:5
|
LL | i -= 1;
| ^^^^^^
error: integer arithmetic detected
--> $DIR/arithmetic.rs:32:5
|
LL | i *= 2;
| ^^^^^^
error: integer arithmetic detected
--> $DIR/arithmetic.rs:33:5
|
LL | i /= 2;
| ^^^^^^
error: integer arithmetic detected
--> $DIR/arithmetic.rs:34:5
|
LL | i %= 2;
| ^^^^^^
error: floating-point arithmetic detected
--> $DIR/arithmetic.rs:45:5
|
LL | f * 2.0;
| ^^^^^^^
|
= note: `-D clippy::float-arithmetic` implied by `-D warnings`
error: floating-point arithmetic detected
--> $DIR/arithmetic.rs:47:5
|
LL | 1.0 + f;
| ^^^^^^^
error: floating-point arithmetic detected
--> $DIR/arithmetic.rs:48:5
|
LL | f * 2.0;
| ^^^^^^^
error: floating-point arithmetic detected
--> $DIR/arithmetic.rs:49:5
|
LL | f / 2.0;
| ^^^^^^^
error: floating-point arithmetic detected
--> $DIR/arithmetic.rs:50:5
|
LL | f - 2.0 * 4.2;
| ^^^^^^^^^^^^^
error: floating-point arithmetic detected
--> $DIR/arithmetic.rs:51:5
|
LL | -f;
| ^^
error: floating-point arithmetic detected
--> $DIR/arithmetic.rs:53:5
|
LL | f += 1.0;
| ^^^^^^^^
error: floating-point arithmetic detected
--> $DIR/arithmetic.rs:54:5
|
LL | f -= 1.0;
| ^^^^^^^^
error: floating-point arithmetic detected
--> $DIR/arithmetic.rs:55:5
|
LL | f *= 2.0;
| ^^^^^^^^
error: floating-point arithmetic detected
--> $DIR/arithmetic.rs:56:5
|
LL | f /= 2.0;
| ^^^^^^^^
error: aborting due to 20 previous errors

View File

@ -51,15 +51,4 @@ fn test_complex_conditions() {
}
}
fn test_nested() {
fn nested() {
let x = Some(());
if x.is_some() {
x.unwrap(); // unnecessary
} else {
x.unwrap(); // will panic
}
}
}
fn main() {}

View File

@ -188,22 +188,5 @@ LL | if x.is_ok() || !(y.is_ok() && z.is_err()) {
LL | z.unwrap_err(); // unnecessary
| ^^^^^^^^^^^^^^
error: You checked before that `unwrap()` cannot fail. Instead of checking and unwrapping, it's better to use `if let` or `match`.
--> $DIR/complex_conditionals.rs:58:13
|
LL | if x.is_some() {
| ----------- the check is happening here
LL | x.unwrap(); // unnecessary
| ^^^^^^^^^^
error: This call to `unwrap()` will always panic.
--> $DIR/complex_conditionals.rs:60:13
|
LL | if x.is_some() {
| ----------- because of this check
...
LL | x.unwrap(); // will panic
| ^^^^^^^^^^
error: aborting due to 22 previous errors
error: aborting due to 20 previous errors

View File

@ -0,0 +1,15 @@
#![deny(clippy::panicking_unwrap, clippy::unnecessary_unwrap)]
#![allow(clippy::if_same_then_else)]
fn test_nested() {
fn nested() {
let x = Some(());
if x.is_some() {
x.unwrap(); // unnecessary
} else {
x.unwrap(); // will panic
}
}
}
fn main() {}

View File

@ -0,0 +1,31 @@
error: You checked before that `unwrap()` cannot fail. Instead of checking and unwrapping, it's better to use `if let` or `match`.
--> $DIR/complex_conditionals_nested.rs:8:13
|
LL | if x.is_some() {
| ----------- the check is happening here
LL | x.unwrap(); // unnecessary
| ^^^^^^^^^^
|
note: the lint level is defined here
--> $DIR/complex_conditionals_nested.rs:1:35
|
LL | #![deny(clippy::panicking_unwrap, clippy::unnecessary_unwrap)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
error: This call to `unwrap()` will always panic.
--> $DIR/complex_conditionals_nested.rs:10:13
|
LL | if x.is_some() {
| ----------- because of this check
...
LL | x.unwrap(); // will panic
| ^^^^^^^^^^
|
note: the lint level is defined here
--> $DIR/complex_conditionals_nested.rs:1:9
|
LL | #![deny(clippy::panicking_unwrap, clippy::unnecessary_unwrap)]
| ^^^^^^^^^^^^^^^^^^^^^^^^
error: aborting due to 2 previous errors

View File

@ -0,0 +1,53 @@
#![warn(clippy::integer_arithmetic, clippy::float_arithmetic)]
#![allow(
unused,
clippy::shadow_reuse,
clippy::shadow_unrelated,
clippy::no_effect,
clippy::unnecessary_operation,
clippy::op_ref,
clippy::trivially_copy_pass_by_ref
)]
#[rustfmt::skip]
fn main() {
let mut f = 1.0f32;
f * 2.0;
1.0 + f;
f * 2.0;
f / 2.0;
f - 2.0 * 4.2;
-f;
f += 1.0;
f -= 1.0;
f *= 2.0;
f /= 2.0;
}
// also warn about floating point arith with references involved
pub fn float_arith_ref() {
3.1_f32 + &1.2_f32;
&3.4_f32 + 1.5_f32;
&3.5_f32 + &1.3_f32;
}
pub fn float_foo(f: &f32) -> f32 {
let a = 5.1;
a + f
}
pub fn float_bar(f1: &f32, f2: &f32) -> f32 {
f1 + f2
}
pub fn float_baz(f1: f32, f2: &f32) -> f32 {
f1 + f2
}
pub fn float_qux(f1: f32, f2: f32) -> f32 {
(&f1 + &f2)
}

View File

@ -0,0 +1,106 @@
error: floating-point arithmetic detected
--> $DIR/float_arithmetic.rs:16:5
|
LL | f * 2.0;
| ^^^^^^^
|
= note: `-D clippy::float-arithmetic` implied by `-D warnings`
error: floating-point arithmetic detected
--> $DIR/float_arithmetic.rs:18:5
|
LL | 1.0 + f;
| ^^^^^^^
error: floating-point arithmetic detected
--> $DIR/float_arithmetic.rs:19:5
|
LL | f * 2.0;
| ^^^^^^^
error: floating-point arithmetic detected
--> $DIR/float_arithmetic.rs:20:5
|
LL | f / 2.0;
| ^^^^^^^
error: floating-point arithmetic detected
--> $DIR/float_arithmetic.rs:21:5
|
LL | f - 2.0 * 4.2;
| ^^^^^^^^^^^^^
error: floating-point arithmetic detected
--> $DIR/float_arithmetic.rs:22:5
|
LL | -f;
| ^^
error: floating-point arithmetic detected
--> $DIR/float_arithmetic.rs:24:5
|
LL | f += 1.0;
| ^^^^^^^^
error: floating-point arithmetic detected
--> $DIR/float_arithmetic.rs:25:5
|
LL | f -= 1.0;
| ^^^^^^^^
error: floating-point arithmetic detected
--> $DIR/float_arithmetic.rs:26:5
|
LL | f *= 2.0;
| ^^^^^^^^
error: floating-point arithmetic detected
--> $DIR/float_arithmetic.rs:27:5
|
LL | f /= 2.0;
| ^^^^^^^^
error: floating-point arithmetic detected
--> $DIR/float_arithmetic.rs:33:5
|
LL | 3.1_f32 + &1.2_f32;
| ^^^^^^^^^^^^^^^^^^
error: floating-point arithmetic detected
--> $DIR/float_arithmetic.rs:34:5
|
LL | &3.4_f32 + 1.5_f32;
| ^^^^^^^^^^^^^^^^^^
error: floating-point arithmetic detected
--> $DIR/float_arithmetic.rs:35:5
|
LL | &3.5_f32 + &1.3_f32;
| ^^^^^^^^^^^^^^^^^^^
error: floating-point arithmetic detected
--> $DIR/float_arithmetic.rs:40:5
|
LL | a + f
| ^^^^^
error: floating-point arithmetic detected
--> $DIR/float_arithmetic.rs:44:5
|
LL | f1 + f2
| ^^^^^^^
error: floating-point arithmetic detected
--> $DIR/float_arithmetic.rs:48:5
|
LL | f1 + f2
| ^^^^^^^
error: floating-point arithmetic detected
--> $DIR/float_arithmetic.rs:52:5
|
LL | (&f1 + &f2)
| ^^^^^^^^^^^
error: aborting due to 17 previous errors

View File

@ -4,7 +4,9 @@
clippy::shadow_reuse,
clippy::shadow_unrelated,
clippy::no_effect,
clippy::unnecessary_operation
clippy::unnecessary_operation,
clippy::op_ref,
clippy::trivially_copy_pass_by_ref
)]
#[rustfmt::skip]
@ -40,25 +42,6 @@ fn main() {
i &= 1;
i ^= i;
let mut f = 1.0f32;
f * 2.0;
1.0 + f;
f * 2.0;
f / 2.0;
f - 2.0 * 4.2;
-f;
f += 1.0;
f -= 1.0;
f *= 2.0;
f /= 2.0;
// no error, overflows are checked by `overflowing_literals`
-1.;
-(-1.);
// No errors for the following items because they are constant expressions
enum Foo {
Bar = -2,
@ -90,4 +73,30 @@ fn main() {
1 + 1
};
}
}
// warn on references as well! (#5328)
pub fn int_arith_ref() {
3 + &1;
&3 + 1;
&3 + &1;
}
pub fn foo(x: &i32) -> i32 {
let a = 5;
a + x
}
pub fn bar(x: &i32, y: &i32) -> i32 {
x + y
}
pub fn baz(x: i32, y: &i32) -> i32 {
x + y
}
pub fn qux(x: i32, y: i32) -> i32 {
(&x + &y)
}

View File

@ -0,0 +1,107 @@
error: integer arithmetic detected
--> $DIR/integer_arithmetic.rs:15:5
|
LL | 1 + i;
| ^^^^^
|
= note: `-D clippy::integer-arithmetic` implied by `-D warnings`
error: integer arithmetic detected
--> $DIR/integer_arithmetic.rs:16:5
|
LL | i * 2;
| ^^^^^
error: integer arithmetic detected
--> $DIR/integer_arithmetic.rs:17:5
|
LL | / 1 %
LL | | i / 2; // no error, this is part of the expression in the preceding line
| |_________^
error: integer arithmetic detected
--> $DIR/integer_arithmetic.rs:19:5
|
LL | i - 2 + 2 - i;
| ^^^^^^^^^^^^^
error: integer arithmetic detected
--> $DIR/integer_arithmetic.rs:20:5
|
LL | -i;
| ^^
error: integer arithmetic detected
--> $DIR/integer_arithmetic.rs:32:5
|
LL | i += 1;
| ^^^^^^
error: integer arithmetic detected
--> $DIR/integer_arithmetic.rs:33:5
|
LL | i -= 1;
| ^^^^^^
error: integer arithmetic detected
--> $DIR/integer_arithmetic.rs:34:5
|
LL | i *= 2;
| ^^^^^^
error: integer arithmetic detected
--> $DIR/integer_arithmetic.rs:35:5
|
LL | i /= 2;
| ^^^^^^
error: integer arithmetic detected
--> $DIR/integer_arithmetic.rs:36:5
|
LL | i %= 2;
| ^^^^^^
error: integer arithmetic detected
--> $DIR/integer_arithmetic.rs:82:5
|
LL | 3 + &1;
| ^^^^^^
error: integer arithmetic detected
--> $DIR/integer_arithmetic.rs:83:5
|
LL | &3 + 1;
| ^^^^^^
error: integer arithmetic detected
--> $DIR/integer_arithmetic.rs:84:5
|
LL | &3 + &1;
| ^^^^^^^
error: integer arithmetic detected
--> $DIR/integer_arithmetic.rs:89:5
|
LL | a + x
| ^^^^^
error: integer arithmetic detected
--> $DIR/integer_arithmetic.rs:93:5
|
LL | x + y
| ^^^^^
error: integer arithmetic detected
--> $DIR/integer_arithmetic.rs:97:5
|
LL | x + y
| ^^^^^
error: integer arithmetic detected
--> $DIR/integer_arithmetic.rs:101:5
|
LL | (&x + &y)
| ^^^^^^^^^
error: aborting due to 17 previous errors