mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-26 22:50:56 +00:00
Apply applicability
This commit is contained in:
parent
dfed9751bd
commit
061b2f3057
2 changed files with 41 additions and 35 deletions
|
@ -14,6 +14,7 @@ use rustc::middle::mem_categorization::Categorization;
|
||||||
use rustc::middle::mem_categorization::cmt_;
|
use rustc::middle::mem_categorization::cmt_;
|
||||||
use rustc::ty::{self, Ty};
|
use rustc::ty::{self, Ty};
|
||||||
use rustc::ty::subst::Subst;
|
use rustc::ty::subst::Subst;
|
||||||
|
use rustc_errors::Applicability;
|
||||||
use std::collections::{HashMap, HashSet};
|
use std::collections::{HashMap, HashSet};
|
||||||
use std::iter::{once, Iterator};
|
use std::iter::{once, Iterator};
|
||||||
use syntax::ast;
|
use syntax::ast;
|
||||||
|
@ -2267,6 +2268,8 @@ impl<'a, 'tcx> Visitor<'tcx> for VarCollectorVisitor<'a, 'tcx> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const NEEDLESS_COLLECT_MSG: &str = "avoid using `collect()` when not needed";
|
||||||
|
|
||||||
fn check_needless_collect<'a, 'tcx>(expr: &'tcx Expr, cx: &LateContext<'a, 'tcx>) {
|
fn check_needless_collect<'a, 'tcx>(expr: &'tcx Expr, cx: &LateContext<'a, 'tcx>) {
|
||||||
if let ExprKind::MethodCall(ref method, _, ref args) = expr.node {
|
if let ExprKind::MethodCall(ref method, _, ref args) = expr.node {
|
||||||
if let ExprKind::MethodCall(ref chain_method, _, _) = args[0].node {
|
if let ExprKind::MethodCall(ref chain_method, _, _) = args[0].node {
|
||||||
|
@ -2281,38 +2284,41 @@ fn check_needless_collect<'a, 'tcx>(expr: &'tcx Expr, cx: &LateContext<'a, 'tcx>
|
||||||
match_type(cx, ty, &paths::BTREEMAP) ||
|
match_type(cx, ty, &paths::BTREEMAP) ||
|
||||||
match_type(cx, ty, &paths::HASHMAP) {
|
match_type(cx, ty, &paths::HASHMAP) {
|
||||||
if method.ident.name == "len" {
|
if method.ident.name == "len" {
|
||||||
span_lint_and_sugg(
|
let span = shorten_needless_collect_span(expr);
|
||||||
cx,
|
span_lint_and_then(cx, NEEDLESS_COLLECT, span, NEEDLESS_COLLECT_MSG, |db| {
|
||||||
NEEDLESS_COLLECT,
|
db.span_suggestion_with_applicability(
|
||||||
shorten_needless_collect_span(expr),
|
span,
|
||||||
"you are collecting an iterator to check its length",
|
"replace with",
|
||||||
"consider replacing with",
|
".count()".to_string(),
|
||||||
".count()".to_string(),
|
Applicability::MachineApplicable,
|
||||||
);
|
);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
if method.ident.name == "is_empty" {
|
if method.ident.name == "is_empty" {
|
||||||
span_lint_and_sugg(
|
let span = shorten_needless_collect_span(expr);
|
||||||
cx,
|
span_lint_and_then(cx, NEEDLESS_COLLECT, span, NEEDLESS_COLLECT_MSG, |db| {
|
||||||
NEEDLESS_COLLECT,
|
db.span_suggestion_with_applicability(
|
||||||
shorten_needless_collect_span(expr),
|
span,
|
||||||
"you are collecting an iterator to check if it is empty",
|
"replace with",
|
||||||
"consider replacing with",
|
".next().is_none()".to_string(),
|
||||||
".next().is_none()".to_string(),
|
Applicability::MachineApplicable,
|
||||||
);
|
);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
if method.ident.name == "contains" {
|
if method.ident.name == "contains" {
|
||||||
let contains_arg = snippet(cx, args[1].span, "??");
|
let contains_arg = snippet(cx, args[1].span, "??");
|
||||||
span_lint_and_sugg(
|
let span = shorten_needless_collect_span(expr);
|
||||||
cx,
|
span_lint_and_then(cx, NEEDLESS_COLLECT, span, NEEDLESS_COLLECT_MSG, |db| {
|
||||||
NEEDLESS_COLLECT,
|
db.span_suggestion_with_applicability(
|
||||||
shorten_needless_collect_span(expr),
|
span,
|
||||||
"you are collecting an iterator to check if contains an element",
|
"replace with",
|
||||||
"consider replacing with",
|
format!(
|
||||||
format!(
|
".any(|&x| x == {})",
|
||||||
".any(|&x| x == {})",
|
if contains_arg.starts_with('&') { &contains_arg[1..] } else { &contains_arg }
|
||||||
if contains_arg.starts_with('&') { &contains_arg[1..] } else { &contains_arg }
|
),
|
||||||
),
|
Applicability::MachineApplicable,
|
||||||
);
|
);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,28 +1,28 @@
|
||||||
error: you are collecting an iterator to check its length
|
error: avoid using `collect()` when not needed
|
||||||
--> $DIR/needless_collect.rs:7:28
|
--> $DIR/needless_collect.rs:7:28
|
||||||
|
|
|
|
||||||
7 | let len = sample.iter().collect::<Vec<_>>().len();
|
7 | let len = sample.iter().collect::<Vec<_>>().len();
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider replacing with: `.count()`
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `.count()`
|
||||||
|
|
|
|
||||||
= note: `-D needless-collect` implied by `-D warnings`
|
= note: `-D needless-collect` implied by `-D warnings`
|
||||||
|
|
||||||
error: you are collecting an iterator to check if it is empty
|
error: avoid using `collect()` when not needed
|
||||||
--> $DIR/needless_collect.rs:8:21
|
--> $DIR/needless_collect.rs:8:21
|
||||||
|
|
|
|
||||||
8 | if sample.iter().collect::<Vec<_>>().is_empty() {
|
8 | if sample.iter().collect::<Vec<_>>().is_empty() {
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider replacing with: `.next().is_none()`
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `.next().is_none()`
|
||||||
|
|
||||||
error: you are collecting an iterator to check if contains an element
|
error: avoid using `collect()` when not needed
|
||||||
--> $DIR/needless_collect.rs:11:27
|
--> $DIR/needless_collect.rs:11:27
|
||||||
|
|
|
|
||||||
11 | sample.iter().cloned().collect::<Vec<_>>().contains(&1);
|
11 | sample.iter().cloned().collect::<Vec<_>>().contains(&1);
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider replacing with: `.any(|&x| x == 1)`
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `.any(|&x| x == 1)`
|
||||||
|
|
||||||
error: you are collecting an iterator to check its length
|
error: avoid using `collect()` when not needed
|
||||||
--> $DIR/needless_collect.rs:12:34
|
--> $DIR/needless_collect.rs:12:34
|
||||||
|
|
|
|
||||||
12 | sample.iter().map(|x| (x, x)).collect::<HashMap<_, _>>().len();
|
12 | sample.iter().map(|x| (x, x)).collect::<HashMap<_, _>>().len();
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider replacing with: `.count()`
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `.count()`
|
||||||
|
|
||||||
error: aborting due to 4 previous errors
|
error: aborting due to 4 previous errors
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue