|
1 | 1 | use itertools::{repeat_n, Itertools};
|
2 | 2 | use rustc::hir::*;
|
3 | 3 | use rustc::lint::*;
|
4 |
| -use rustc::ty::TypeVariants; |
| 4 | +use rustc::ty::{AssociatedKind, TypeVariants}; |
5 | 5 | use syntax::ast::NodeId;
|
6 | 6 |
|
7 | 7 | use std::collections::HashSet;
|
@@ -108,30 +108,37 @@ fn check_expr_for_collect<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr
|
108 | 108 | // Get the type of the Item associated to the Iterator on which collect() is
|
109 | 109 | // called.
|
110 | 110 | let arg_ty = cx.tables.expr_ty(&args[0]);
|
111 |
| - let method_call = cx.tables.type_dependent_defs()[args[0].hir_id]; |
112 |
| - let trt_id = cx.tcx.trait_of_item(method_call.def_id()).unwrap(); |
113 |
| - let assoc_item_id = cx.tcx.associated_items(trt_id).next().unwrap().def_id; |
114 |
| - let substitutions = cx.tcx.mk_substs_trait(arg_ty, &[]); |
115 |
| - let projection = cx.tcx.mk_projection(assoc_item_id, substitutions); |
116 |
| - let normal_ty = cx.tcx.normalize_erasing_regions( |
117 |
| - cx.param_env, |
118 |
| - projection, |
119 |
| - ); |
| 111 | + let ty_defs = cx.tables.type_dependent_defs(); |
| 112 | + if_chain! { |
| 113 | + if let Some(method_call) = ty_defs.get(args[0].hir_id); |
| 114 | + if let Some(trt_id) = cx.tcx.trait_of_item(method_call.def_id()); |
| 115 | + if let Some(assoc_item) = cx.tcx.associated_items(trt_id).next(); |
| 116 | + if assoc_item.kind == AssociatedKind::Type; |
| 117 | + then { |
| 118 | + let assoc_item_id = assoc_item.def_id; |
| 119 | + let substitutions = cx.tcx.mk_substs_trait(arg_ty, &[]); |
| 120 | + let projection = cx.tcx.mk_projection(assoc_item_id, substitutions); |
| 121 | + let normal_ty = cx.tcx.normalize_erasing_regions( |
| 122 | + cx.param_env, |
| 123 | + projection, |
| 124 | + ); |
120 | 125 |
|
121 |
| - return if match_type(cx, normal_ty, &paths::OPTION) { |
122 |
| - Some(Suggestion { |
123 |
| - pattern: format_suggestion_pattern(cx, collect_ty.sty.clone(), true), |
124 |
| - type_colloquial: "Option", |
125 |
| - success_variant: "Some", |
126 |
| - }) |
127 |
| - } else if match_type(cx, normal_ty, &paths::RESULT) { |
128 |
| - Some(Suggestion { |
129 |
| - pattern: format_suggestion_pattern(cx, collect_ty.sty.clone(), false), |
130 |
| - type_colloquial: "Result", |
131 |
| - success_variant: "Ok", |
132 |
| - }) |
133 |
| - } else { |
134 |
| - None |
| 126 | + return if match_type(cx, normal_ty, &paths::OPTION) { |
| 127 | + Some(Suggestion { |
| 128 | + pattern: format_suggestion_pattern(cx, collect_ty.sty.clone(), true), |
| 129 | + type_colloquial: "Option", |
| 130 | + success_variant: "Some", |
| 131 | + }) |
| 132 | + } else if match_type(cx, normal_ty, &paths::RESULT) { |
| 133 | + Some(Suggestion { |
| 134 | + pattern: format_suggestion_pattern(cx, collect_ty.sty.clone(), false), |
| 135 | + type_colloquial: "Result", |
| 136 | + success_variant: "Ok", |
| 137 | + }) |
| 138 | + } else { |
| 139 | + None |
| 140 | + }; |
| 141 | + } |
135 | 142 | };
|
136 | 143 | }
|
137 | 144 | }
|
|
0 commit comments