diff --git a/clippy_dev/src/stderr_length_check.rs b/clippy_dev/src/stderr_length_check.rs index 041ee691137..c511733f7bf 100644 --- a/clippy_dev/src/stderr_length_check.rs +++ b/clippy_dev/src/stderr_length_check.rs @@ -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 { +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 } diff --git a/clippy_lints/src/arithmetic.rs b/clippy_lints/src/arithmetic.rs index fd252b9ee10..a138c9d3545 100644 --- a/clippy_lints/src/arithmetic.rs +++ b/clippy_lints/src/arithmetic.rs @@ -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); } diff --git a/tests/ui/arithmetic.stderr b/tests/ui/arithmetic.stderr deleted file mode 100644 index d999b69d7cb..00000000000 --- a/tests/ui/arithmetic.stderr +++ /dev/null @@ -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 - diff --git a/tests/ui/checked_unwrap/complex_conditionals.rs b/tests/ui/checked_unwrap/complex_conditionals.rs index fbeee1b572a..c986c992a07 100644 --- a/tests/ui/checked_unwrap/complex_conditionals.rs +++ b/tests/ui/checked_unwrap/complex_conditionals.rs @@ -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() {} diff --git a/tests/ui/checked_unwrap/complex_conditionals.stderr b/tests/ui/checked_unwrap/complex_conditionals.stderr index 4e53bd769e2..dc666bab460 100644 --- a/tests/ui/checked_unwrap/complex_conditionals.stderr +++ b/tests/ui/checked_unwrap/complex_conditionals.stderr @@ -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 diff --git a/tests/ui/checked_unwrap/complex_conditionals_nested.rs b/tests/ui/checked_unwrap/complex_conditionals_nested.rs new file mode 100644 index 00000000000..2307996a48f --- /dev/null +++ b/tests/ui/checked_unwrap/complex_conditionals_nested.rs @@ -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() {} diff --git a/tests/ui/checked_unwrap/complex_conditionals_nested.stderr b/tests/ui/checked_unwrap/complex_conditionals_nested.stderr new file mode 100644 index 00000000000..e4d085470c3 --- /dev/null +++ b/tests/ui/checked_unwrap/complex_conditionals_nested.stderr @@ -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 + diff --git a/tests/ui/float_arithmetic.rs b/tests/ui/float_arithmetic.rs new file mode 100644 index 00000000000..5ad320c6209 --- /dev/null +++ b/tests/ui/float_arithmetic.rs @@ -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) +} diff --git a/tests/ui/float_arithmetic.stderr b/tests/ui/float_arithmetic.stderr new file mode 100644 index 00000000000..809392529fd --- /dev/null +++ b/tests/ui/float_arithmetic.stderr @@ -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 + diff --git a/tests/ui/arithmetic.rs b/tests/ui/integer_arithmetic.rs similarity index 75% rename from tests/ui/arithmetic.rs rename to tests/ui/integer_arithmetic.rs index dc11cbd98de..31a07e7c35b 100644 --- a/tests/ui/arithmetic.rs +++ b/tests/ui/integer_arithmetic.rs @@ -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) } diff --git a/tests/ui/integer_arithmetic.stderr b/tests/ui/integer_arithmetic.stderr new file mode 100644 index 00000000000..0b8d0b767bf --- /dev/null +++ b/tests/ui/integer_arithmetic.stderr @@ -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 +