From 51c0dd427b63faf6d3a3810ebd7942aae9a62065 Mon Sep 17 00:00:00 2001 From: Wilco Kusee Date: Sun, 13 Jan 2019 20:03:22 +0100 Subject: [PATCH] Add run-rustfix to unnecessary_fold --- tests/ui/unnecessary_fold.fixed | 44 ++++++++++++++++++++++++++++++++ tests/ui/unnecessary_fold.rs | 10 +++++--- tests/ui/unnecessary_fold.stderr | 22 ++++++++-------- 3 files changed, 62 insertions(+), 14 deletions(-) create mode 100644 tests/ui/unnecessary_fold.fixed diff --git a/tests/ui/unnecessary_fold.fixed b/tests/ui/unnecessary_fold.fixed new file mode 100644 index 00000000000..5f12d72a76a --- /dev/null +++ b/tests/ui/unnecessary_fold.fixed @@ -0,0 +1,44 @@ +// run-rustfix + +#![allow(dead_code)] + +/// Calls which should trigger the `UNNECESSARY_FOLD` lint +fn unnecessary_fold() { + // Can be replaced by .any + let _ = (0..3).any(|x| x > 2); + // Can be replaced by .all + let _ = (0..3).all(|x| x > 2); + // Can be replaced by .sum + let _: i32 = (0..3).sum(); + // Can be replaced by .product + let _: i32 = (0..3).product(); +} + +/// Should trigger the `UNNECESSARY_FOLD` lint, with an error span including exactly `.fold(...)` +fn unnecessary_fold_span_for_multi_element_chain() { + let _: bool = (0..3).map(|x| 2 * x).any(|x| x > 2); +} + +/// Calls which should not trigger the `UNNECESSARY_FOLD` lint +fn unnecessary_fold_should_ignore() { + let _ = (0..3).fold(true, |acc, x| acc || x > 2); + let _ = (0..3).fold(false, |acc, x| acc && x > 2); + let _ = (0..3).fold(1, |acc, x| acc + x); + let _ = (0..3).fold(0, |acc, x| acc * x); + let _ = (0..3).fold(0, |acc, x| 1 + acc + x); + + // We only match against an accumulator on the left + // hand side. We could lint for .sum and .product when + // it's on the right, but don't for now (and this wouldn't + // be valid if we extended the lint to cover arbitrary numeric + // types). + let _ = (0..3).fold(false, |acc, x| x > 2 || acc); + let _ = (0..3).fold(true, |acc, x| x > 2 && acc); + let _ = (0..3).fold(0, |acc, x| x + acc); + let _ = (0..3).fold(1, |acc, x| x * acc); + + let _ = [(0..2), (0..3)].iter().fold(0, |a, b| a + b.len()); + let _ = [(0..2), (0..3)].iter().fold(1, |a, b| a * b.len()); +} + +fn main() {} diff --git a/tests/ui/unnecessary_fold.rs b/tests/ui/unnecessary_fold.rs index 62198e21ef7..ae667d1ac06 100644 --- a/tests/ui/unnecessary_fold.rs +++ b/tests/ui/unnecessary_fold.rs @@ -1,3 +1,7 @@ +// run-rustfix + +#![allow(dead_code)] + /// Calls which should trigger the `UNNECESSARY_FOLD` lint fn unnecessary_fold() { // Can be replaced by .any @@ -5,14 +9,14 @@ fn unnecessary_fold() { // Can be replaced by .all let _ = (0..3).fold(true, |acc, x| acc && x > 2); // Can be replaced by .sum - let _ = (0..3).fold(0, |acc, x| acc + x); + let _: i32 = (0..3).fold(0, |acc, x| acc + x); // Can be replaced by .product - let _ = (0..3).fold(1, |acc, x| acc * x); + let _: i32 = (0..3).fold(1, |acc, x| acc * x); } /// Should trigger the `UNNECESSARY_FOLD` lint, with an error span including exactly `.fold(...)` fn unnecessary_fold_span_for_multi_element_chain() { - let _ = (0..3).map(|x| 2 * x).fold(false, |acc, x| acc || x > 2); + let _: bool = (0..3).map(|x| 2 * x).fold(false, |acc, x| acc || x > 2); } /// Calls which should not trigger the `UNNECESSARY_FOLD` lint diff --git a/tests/ui/unnecessary_fold.stderr b/tests/ui/unnecessary_fold.stderr index 07414b400c1..f9911d4a3dc 100644 --- a/tests/ui/unnecessary_fold.stderr +++ b/tests/ui/unnecessary_fold.stderr @@ -1,5 +1,5 @@ error: this `.fold` can be written more succinctly using another method - --> $DIR/unnecessary_fold.rs:4:19 + --> $DIR/unnecessary_fold.rs:8:19 | LL | let _ = (0..3).fold(false, |acc, x| acc || x > 2); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `.any(|x| x > 2)` @@ -7,28 +7,28 @@ LL | let _ = (0..3).fold(false, |acc, x| acc || x > 2); = note: `-D clippy::unnecessary-fold` implied by `-D warnings` error: this `.fold` can be written more succinctly using another method - --> $DIR/unnecessary_fold.rs:6:19 + --> $DIR/unnecessary_fold.rs:10:19 | LL | let _ = (0..3).fold(true, |acc, x| acc && x > 2); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `.all(|x| x > 2)` error: this `.fold` can be written more succinctly using another method - --> $DIR/unnecessary_fold.rs:8:19 + --> $DIR/unnecessary_fold.rs:12:24 | -LL | let _ = (0..3).fold(0, |acc, x| acc + x); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `.sum()` +LL | let _: i32 = (0..3).fold(0, |acc, x| acc + x); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `.sum()` error: this `.fold` can be written more succinctly using another method - --> $DIR/unnecessary_fold.rs:10:19 + --> $DIR/unnecessary_fold.rs:14:24 | -LL | let _ = (0..3).fold(1, |acc, x| acc * x); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `.product()` +LL | let _: i32 = (0..3).fold(1, |acc, x| acc * x); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `.product()` error: this `.fold` can be written more succinctly using another method - --> $DIR/unnecessary_fold.rs:15:34 + --> $DIR/unnecessary_fold.rs:19:40 | -LL | let _ = (0..3).map(|x| 2 * x).fold(false, |acc, x| acc || x > 2); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `.any(|x| x > 2)` +LL | let _: bool = (0..3).map(|x| 2 * x).fold(false, |acc, x| acc || x > 2); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `.any(|x| x > 2)` error: aborting due to 5 previous errors