|
1 | 1 | use super::NEEDLESS_COLLECT;
|
2 | 2 | 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}; |
4 | 4 | use clippy_utils::sugg::Sugg;
|
5 | 5 | use clippy_utils::ty::is_type_diagnostic_item;
|
6 | 6 | 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
|
28 | 28 | if let Some(GenericArg::Type(ref ty)) = generic_args.args.get(0);
|
29 | 29 | if let Some(ty) = cx.typeck_results().node_type_opt(ty.hir_id);
|
30 | 30 | 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(); |
32 | 33 | let method_name = &*method.ident.name.as_str();
|
33 | 34 | let sugg = if is_type_diagnostic_item(cx, ty, sym::vec_type) ||
|
34 | 35 | is_type_diagnostic_item(cx, ty, sym::vecdeque_type) ||
|
35 | 36 | is_type_diagnostic_item(cx, ty, sym::LinkedList) ||
|
36 | 37 | is_type_diagnostic_item(cx, ty, sym::BinaryHeap) {
|
37 | 38 | match method_name {
|
38 |
| - "len" => Some("count()".to_string()), |
| 39 | + "len" => "count()".to_string(), |
39 | 40 | "is_empty" => is_empty_sugg,
|
40 | 41 | "contains" => {
|
41 |
| - let contains_arg = snippet(cx, args[1].span, "??"); |
| 42 | + let contains_arg = snippet_with_applicability(cx, args[1].span, "??", &mut applicability); |
42 | 43 | let (arg, pred) = contains_arg
|
43 | 44 | .strip_prefix('&')
|
44 | 45 | .map_or(("&x", &*contains_arg), |s| ("x", s));
|
45 |
| - Some(format!("any(|{}| x == {})", arg, pred)) |
| 46 | + format!("any(|{}| x == {})", arg, pred) |
46 | 47 | }
|
47 |
| - _ => None, |
| 48 | + _ => return, |
48 | 49 | }
|
49 | 50 | }
|
50 | 51 | else if is_type_diagnostic_item(cx, ty, sym::BTreeMap) ||
|
51 | 52 | is_type_diagnostic_item(cx, ty, sym::hashmap_type) {
|
52 | 53 | match method_name {
|
53 | 54 | "is_empty" => is_empty_sugg,
|
54 |
| - _ => None, |
| 55 | + _ => return, |
55 | 56 | }
|
56 | 57 | }
|
57 | 58 | else {
|
58 |
| - None |
| 59 | + return; |
59 | 60 | };
|
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 | + ); |
71 | 70 | }
|
72 | 71 | }
|
73 | 72 | }
|
|
0 commit comments