From 980650eec27851276d6cc59ed1c863256102529d Mon Sep 17 00:00:00 2001 From: Manish Goregaokar Date: Wed, 25 Sep 2019 05:25:31 -0700 Subject: [PATCH] needless_collect: fix suggestion, make test rustfixable --- clippy_lints/src/loops.rs | 9 +++++++-- tests/ui/needless_collect.fixed | 21 +++++++++++++++++++++ tests/ui/needless_collect.rs | 4 ++++ tests/ui/needless_collect.stderr | 10 +++++----- 4 files changed, 37 insertions(+), 7 deletions(-) create mode 100644 tests/ui/needless_collect.fixed diff --git a/clippy_lints/src/loops.rs b/clippy_lints/src/loops.rs index 94328a2b5e3..0a315fe48b5 100644 --- a/clippy_lints/src/loops.rs +++ b/clippy_lints/src/loops.rs @@ -2427,12 +2427,17 @@ fn check_needless_collect<'a, 'tcx>(expr: &'tcx Expr, cx: &LateContext<'a, 'tcx> let contains_arg = snippet(cx, args[1].span, "??"); let span = shorten_needless_collect_span(expr); span_lint_and_then(cx, NEEDLESS_COLLECT, span, NEEDLESS_COLLECT_MSG, |db| { + let (arg, pred) = if contains_arg.starts_with('&') { + ("x", &contains_arg[1..]) + } else { + ("&x", &*contains_arg) + }; db.span_suggestion( span, "replace with", format!( - ".any(|&x| x == {})", - if contains_arg.starts_with('&') { &contains_arg[1..] } else { &contains_arg } + ".any(|{}| x == {})", + arg, pred ), Applicability::MachineApplicable, ); diff --git a/tests/ui/needless_collect.fixed b/tests/ui/needless_collect.fixed new file mode 100644 index 00000000000..b4227eaf2f8 --- /dev/null +++ b/tests/ui/needless_collect.fixed @@ -0,0 +1,21 @@ +// run-rustfix + +#![allow(unused, clippy::suspicious_map)] + +use std::collections::{BTreeSet, HashMap, HashSet}; + +#[warn(clippy::needless_collect)] +#[allow(unused_variables, clippy::iter_cloned_collect)] +fn main() { + let sample = [1; 5]; + let len = sample.iter().count(); + if sample.iter().next().is_none() { + // Empty + } + sample.iter().cloned().any(|x| x == 1); + sample.iter().map(|x| (x, x)).count(); + // Notice the `HashSet`--this should not be linted + sample.iter().collect::>().len(); + // Neither should this + sample.iter().collect::>().len(); +} diff --git a/tests/ui/needless_collect.rs b/tests/ui/needless_collect.rs index d4815f60f51..7ee603afeb0 100644 --- a/tests/ui/needless_collect.rs +++ b/tests/ui/needless_collect.rs @@ -1,3 +1,7 @@ +// run-rustfix + +#![allow(unused, clippy::suspicious_map)] + use std::collections::{BTreeSet, HashMap, HashSet}; #[warn(clippy::needless_collect)] diff --git a/tests/ui/needless_collect.stderr b/tests/ui/needless_collect.stderr index 684c501c5b5..8884c8e1612 100644 --- a/tests/ui/needless_collect.stderr +++ b/tests/ui/needless_collect.stderr @@ -1,5 +1,5 @@ error: avoid using `collect()` when not needed - --> $DIR/needless_collect.rs:7:28 + --> $DIR/needless_collect.rs:11:28 | LL | let len = sample.iter().collect::>().len(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `.count()` @@ -7,19 +7,19 @@ LL | let len = sample.iter().collect::>().len(); = note: `-D clippy::needless-collect` implied by `-D warnings` error: avoid using `collect()` when not needed - --> $DIR/needless_collect.rs:8:21 + --> $DIR/needless_collect.rs:12:21 | LL | if sample.iter().collect::>().is_empty() { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `.next().is_none()` error: avoid using `collect()` when not needed - --> $DIR/needless_collect.rs:11:27 + --> $DIR/needless_collect.rs:15:27 | LL | sample.iter().cloned().collect::>().contains(&1); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `.any(|&x| x == 1)` + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `.any(|x| x == 1)` error: avoid using `collect()` when not needed - --> $DIR/needless_collect.rs:12:34 + --> $DIR/needless_collect.rs:16:34 | LL | sample.iter().map(|x| (x, x)).collect::>().len(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `.count()`