From dfde407f0d81b2155a6354fa8ddc5a5ad0d7c2b6 Mon Sep 17 00:00:00 2001 From: Philipp Hansch Date: Thu, 12 Apr 2018 22:16:43 +0200 Subject: [PATCH] Move unnecessary_fold UI tests to separate file --- tests/ui/methods.rs | 39 ------------------------------- tests/ui/methods.stderr | 38 +++--------------------------- tests/ui/unnecessary_fold.rs | 40 ++++++++++++++++++++++++++++++++ tests/ui/unnecessary_fold.stderr | 34 +++++++++++++++++++++++++++ 4 files changed, 77 insertions(+), 74 deletions(-) create mode 100644 tests/ui/unnecessary_fold.rs create mode 100644 tests/ui/unnecessary_fold.stderr diff --git a/tests/ui/methods.rs b/tests/ui/methods.rs index 65cac8ec4ff7..9e2536558333 100644 --- a/tests/ui/methods.rs +++ b/tests/ui/methods.rs @@ -385,45 +385,6 @@ fn iter_skip_next() { let _ = foo.filter().skip(42).next(); } -/// Calls which should trigger the `UNNECESSARY_FOLD` lint -fn unnecessary_fold() { - // Can be replaced by .any - let _ = (0..3).fold(false, |acc, x| acc || x > 2); - // 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); - // Can be replaced by .product - let _ = (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); -} - -/// 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()); -} - #[allow(similar_names)] fn main() { let opt = Some(0); diff --git a/tests/ui/methods.stderr b/tests/ui/methods.stderr index 42cf3d3cbc55..1dd1ddc3caad 100644 --- a/tests/ui/methods.stderr +++ b/tests/ui/methods.stderr @@ -493,45 +493,13 @@ error: called `skip(x).next()` on an iterator. This is more succinctly expressed 382 | let _ = &some_vec[..].iter().skip(3).next(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -error: this `.fold` can be written more succinctly using another method - --> $DIR/methods.rs:391:19 - | -391 | let _ = (0..3).fold(false, |acc, x| acc || x > 2); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `.any(|x| x > 2)` - | - = note: `-D unnecessary-fold` implied by `-D warnings` - -error: this `.fold` can be written more succinctly using another method - --> $DIR/methods.rs:393:19 - | -393 | 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/methods.rs:395:19 - | -395 | let _ = (0..3).fold(0, |acc, x| acc + x); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `.sum()` - -error: this `.fold` can be written more succinctly using another method - --> $DIR/methods.rs:397:19 - | -397 | let _ = (0..3).fold(1, |acc, x| acc * x); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `.product()` - -error: this `.fold` can be written more succinctly using another method - --> $DIR/methods.rs:402:34 - | -402 | let _ = (0..3).map(|x| 2 * x).fold(false, |acc, x| acc || x > 2); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `.any(|x| x > 2)` - error: used unwrap() on an Option value. If you don't want to handle the None case gracefully, consider using expect() to provide a better panic message - --> $DIR/methods.rs:430:13 + --> $DIR/methods.rs:391:13 | -430 | let _ = opt.unwrap(); +391 | let _ = opt.unwrap(); | ^^^^^^^^^^^^ | = note: `-D option-unwrap-used` implied by `-D warnings` -error: aborting due to 71 previous errors +error: aborting due to 66 previous errors diff --git a/tests/ui/unnecessary_fold.rs b/tests/ui/unnecessary_fold.rs new file mode 100644 index 000000000000..62198e21ef7c --- /dev/null +++ b/tests/ui/unnecessary_fold.rs @@ -0,0 +1,40 @@ +/// Calls which should trigger the `UNNECESSARY_FOLD` lint +fn unnecessary_fold() { + // Can be replaced by .any + let _ = (0..3).fold(false, |acc, x| acc || x > 2); + // 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); + // Can be replaced by .product + let _ = (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); +} + +/// 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.stderr b/tests/ui/unnecessary_fold.stderr new file mode 100644 index 000000000000..8bc4b8244bd5 --- /dev/null +++ b/tests/ui/unnecessary_fold.stderr @@ -0,0 +1,34 @@ +error: this `.fold` can be written more succinctly using another method + --> $DIR/unnecessary_fold.rs:4:19 + | +4 | let _ = (0..3).fold(false, |acc, x| acc || x > 2); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `.any(|x| x > 2)` + | + = note: `-D unnecessary-fold` implied by `-D warnings` + +error: this `.fold` can be written more succinctly using another method + --> $DIR/unnecessary_fold.rs:6:19 + | +6 | 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 + | +8 | let _ = (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 + | +10 | let _ = (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 + | +15 | let _ = (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 +