Skip to content

Commit b249290

Browse files
committed
needless_collect: use snippet_with_applicability
+ small code refactor - using early returns.
1 parent 171789e commit b249290

File tree

1 file changed

+18
-19
lines changed

1 file changed

+18
-19
lines changed

clippy_lints/src/loops/needless_collect.rs

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::NEEDLESS_COLLECT;
22
use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_and_then};
3-
use clippy_utils::source::snippet;
3+
use clippy_utils::source::{snippet, snippet_with_applicability};
44
use clippy_utils::sugg::Sugg;
55
use clippy_utils::ty::is_type_diagnostic_item;
66
use clippy_utils::{is_trait_method, path_to_local_id};
@@ -28,46 +28,45 @@ fn check_needless_collect_direct_usage<'tcx>(expr: &'tcx Expr<'_>, cx: &LateCont
2828
if let Some(GenericArg::Type(ref ty)) = generic_args.args.get(0);
2929
if let Some(ty) = cx.typeck_results().node_type_opt(ty.hir_id);
3030
then {
31-
let is_empty_sugg = Some("next().is_none()".to_string());
31+
let mut applicability = Applicability::MachineApplicable;
32+
let is_empty_sugg = "next().is_none()".to_string();
3233
let method_name = &*method.ident.name.as_str();
3334
let sugg = if is_type_diagnostic_item(cx, ty, sym::vec_type) ||
3435
is_type_diagnostic_item(cx, ty, sym::vecdeque_type) ||
3536
is_type_diagnostic_item(cx, ty, sym::LinkedList) ||
3637
is_type_diagnostic_item(cx, ty, sym::BinaryHeap) {
3738
match method_name {
38-
"len" => Some("count()".to_string()),
39+
"len" => "count()".to_string(),
3940
"is_empty" => is_empty_sugg,
4041
"contains" => {
41-
let contains_arg = snippet(cx, args[1].span, "??");
42+
let contains_arg = snippet_with_applicability(cx, args[1].span, "??", &mut applicability);
4243
let (arg, pred) = contains_arg
4344
.strip_prefix('&')
4445
.map_or(("&x", &*contains_arg), |s| ("x", s));
45-
Some(format!("any(|{}| x == {})", arg, pred))
46+
format!("any(|{}| x == {})", arg, pred)
4647
}
47-
_ => None,
48+
_ => return,
4849
}
4950
}
5051
else if is_type_diagnostic_item(cx, ty, sym::BTreeMap) ||
5152
is_type_diagnostic_item(cx, ty, sym::hashmap_type) {
5253
match method_name {
5354
"is_empty" => is_empty_sugg,
54-
_ => None,
55+
_ => return,
5556
}
5657
}
5758
else {
58-
None
59+
return;
5960
};
60-
if let Some(sugg) = sugg {
61-
span_lint_and_sugg(
62-
cx,
63-
NEEDLESS_COLLECT,
64-
method0_span.with_hi(expr.span.hi()),
65-
NEEDLESS_COLLECT_MSG,
66-
"replace with",
67-
sugg,
68-
Applicability::MachineApplicable,
69-
);
70-
}
61+
span_lint_and_sugg(
62+
cx,
63+
NEEDLESS_COLLECT,
64+
method0_span.with_hi(expr.span.hi()),
65+
NEEDLESS_COLLECT_MSG,
66+
"replace with",
67+
sugg,
68+
applicability,
69+
);
7170
}
7271
}
7372
}

0 commit comments

Comments
 (0)