mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-26 22:50:56 +00:00
Rustup
This commit is contained in:
parent
061b2f3057
commit
48e6be42d7
3 changed files with 21 additions and 17 deletions
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue