Skip to content

Commit ed9cd15

Browse files
committed
More if_chain
1 parent 48e6be4 commit ed9cd15

File tree

1 file changed

+48
-50
lines changed

1 file changed

+48
-50
lines changed

clippy_lints/src/loops.rs

Lines changed: 48 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -2271,56 +2271,54 @@ impl<'a, 'tcx> Visitor<'tcx> for VarCollectorVisitor<'a, 'tcx> {
22712271
const NEEDLESS_COLLECT_MSG: &str = "avoid using `collect()` when not needed";
22722272

22732273
fn check_needless_collect<'a, 'tcx>(expr: &'tcx Expr, cx: &LateContext<'a, 'tcx>) {
2274-
if let ExprKind::MethodCall(ref method, _, ref args) = expr.node {
2275-
if let ExprKind::MethodCall(ref chain_method, _, _) = args[0].node {
2276-
if chain_method.ident.name == "collect" &&
2277-
match_trait_method(cx, &args[0], &paths::ITERATOR) &&
2278-
chain_method.args.is_some() {
2279-
let generic_args = chain_method.args.as_ref().unwrap();
2280-
if let Some(GenericArg::Type(ref ty)) = generic_args.args.get(0) {
2281-
let ty = cx.tables.node_id_to_type(ty.hir_id);
2282-
if match_type(cx, ty, &paths::VEC) ||
2283-
match_type(cx, ty, &paths::VEC_DEQUE) ||
2284-
match_type(cx, ty, &paths::BTREEMAP) ||
2285-
match_type(cx, ty, &paths::HASHMAP) {
2286-
if method.ident.name == "len" {
2287-
let span = shorten_needless_collect_span(expr);
2288-
span_lint_and_then(cx, NEEDLESS_COLLECT, span, NEEDLESS_COLLECT_MSG, |db| {
2289-
db.span_suggestion_with_applicability(
2290-
span,
2291-
"replace with",
2292-
".count()".to_string(),
2293-
Applicability::MachineApplicable,
2294-
);
2295-
});
2296-
}
2297-
if method.ident.name == "is_empty" {
2298-
let span = shorten_needless_collect_span(expr);
2299-
span_lint_and_then(cx, NEEDLESS_COLLECT, span, NEEDLESS_COLLECT_MSG, |db| {
2300-
db.span_suggestion_with_applicability(
2301-
span,
2302-
"replace with",
2303-
".next().is_none()".to_string(),
2304-
Applicability::MachineApplicable,
2305-
);
2306-
});
2307-
}
2308-
if method.ident.name == "contains" {
2309-
let contains_arg = snippet(cx, args[1].span, "??");
2310-
let span = shorten_needless_collect_span(expr);
2311-
span_lint_and_then(cx, NEEDLESS_COLLECT, span, NEEDLESS_COLLECT_MSG, |db| {
2312-
db.span_suggestion_with_applicability(
2313-
span,
2314-
"replace with",
2315-
format!(
2316-
".any(|&x| x == {})",
2317-
if contains_arg.starts_with('&') { &contains_arg[1..] } else { &contains_arg }
2318-
),
2319-
Applicability::MachineApplicable,
2320-
);
2321-
});
2322-
}
2323-
}
2274+
if_chain! {
2275+
if let ExprKind::MethodCall(ref method, _, ref args) = expr.node;
2276+
if let ExprKind::MethodCall(ref chain_method, _, _) = args[0].node;
2277+
if chain_method.ident.name == "collect" && match_trait_method(cx, &args[0], &paths::ITERATOR);
2278+
if let Some(ref generic_args) = chain_method.args;
2279+
if let Some(GenericArg::Type(ref ty)) = generic_args.args.get(0);
2280+
then {
2281+
let ty = cx.tables.node_id_to_type(ty.hir_id);
2282+
if match_type(cx, ty, &paths::VEC) ||
2283+
match_type(cx, ty, &paths::VEC_DEQUE) ||
2284+
match_type(cx, ty, &paths::BTREEMAP) ||
2285+
match_type(cx, ty, &paths::HASHMAP) {
2286+
if method.ident.name == "len" {
2287+
let span = shorten_needless_collect_span(expr);
2288+
span_lint_and_then(cx, NEEDLESS_COLLECT, span, NEEDLESS_COLLECT_MSG, |db| {
2289+
db.span_suggestion_with_applicability(
2290+
span,
2291+
"replace with",
2292+
".count()".to_string(),
2293+
Applicability::MachineApplicable,
2294+
);
2295+
});
2296+
}
2297+
if method.ident.name == "is_empty" {
2298+
let span = shorten_needless_collect_span(expr);
2299+
span_lint_and_then(cx, NEEDLESS_COLLECT, span, NEEDLESS_COLLECT_MSG, |db| {
2300+
db.span_suggestion_with_applicability(
2301+
span,
2302+
"replace with",
2303+
".next().is_none()".to_string(),
2304+
Applicability::MachineApplicable,
2305+
);
2306+
});
2307+
}
2308+
if method.ident.name == "contains" {
2309+
let contains_arg = snippet(cx, args[1].span, "??");
2310+
let span = shorten_needless_collect_span(expr);
2311+
span_lint_and_then(cx, NEEDLESS_COLLECT, span, NEEDLESS_COLLECT_MSG, |db| {
2312+
db.span_suggestion_with_applicability(
2313+
span,
2314+
"replace with",
2315+
format!(
2316+
".any(|&x| x == {})",
2317+
if contains_arg.starts_with('&') { &contains_arg[1..] } else { &contains_arg }
2318+
),
2319+
Applicability::MachineApplicable,
2320+
);
2321+
});
23242322
}
23252323
}
23262324
}

0 commit comments

Comments
 (0)