This commit is contained in:
Josh Holmer 2018-09-03 23:50:24 -04:00
parent 061b2f3057
commit 48e6be42d7
3 changed files with 21 additions and 17 deletions

View file

@ -2328,8 +2328,10 @@ fn check_needless_collect<'a, 'tcx>(expr: &'tcx Expr, cx: &LateContext<'a, 'tcx>
}
fn shorten_needless_collect_span(expr: &Expr) -> Span {
if let ExprKind::MethodCall(_, _, ref args) = expr.node {
if let ExprKind::MethodCall(_, ref span, _) = args[0].node {
if_chain! {
if let ExprKind::MethodCall(_, _, ref args) = expr.node;
if let ExprKind::MethodCall(_, ref span, _) = args[0].node;
then {
return expr.span.with_lo(span.lo() - BytePos(1));
}
}

View file

@ -1,7 +1,9 @@
#![feature(tool_lints)]
use std::collections::{HashMap, HashSet, BTreeSet};
#[warn(clippy, needless_collect)]
#[allow(unused_variables, iter_cloned_collect)]
#[warn(clippy::needless_collect)]
#[allow(unused_variables, clippy::iter_cloned_collect)]
fn main() {
let sample = [1; 5];
let len = sample.iter().collect::<Vec<_>>().len();

View file

@ -1,27 +1,27 @@
error: avoid using `collect()` when not needed
--> $DIR/needless_collect.rs:7:28
--> $DIR/needless_collect.rs:9:28
|
7 | let len = sample.iter().collect::<Vec<_>>().len();
9 | let len = sample.iter().collect::<Vec<_>>().len();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `.count()`
|
= note: `-D needless-collect` implied by `-D warnings`
= note: `-D clippy::needless-collect` implied by `-D warnings`
error: avoid using `collect()` when not needed
--> $DIR/needless_collect.rs:8:21
|
8 | if sample.iter().collect::<Vec<_>>().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:10:21
|
11 | sample.iter().cloned().collect::<Vec<_>>().contains(&1);
10 | if sample.iter().collect::<Vec<_>>().is_empty() {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `.next().is_none()`
error: avoid using `collect()` when not needed
--> $DIR/needless_collect.rs:13:27
|
13 | sample.iter().cloned().collect::<Vec<_>>().contains(&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:14:34
|
12 | sample.iter().map(|x| (x, x)).collect::<HashMap<_, _>>().len();
14 | sample.iter().map(|x| (x, x)).collect::<HashMap<_, _>>().len();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `.count()`
error: aborting due to 4 previous errors