Skip to content

Commit c1f2370

Browse files
flip1995phansch
authored andcommitted
Fix bugs which produced ICEs
1 parent c88f724 commit c1f2370

File tree

1 file changed

+31
-24
lines changed

1 file changed

+31
-24
lines changed

clippy_lints/src/collect.rs

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use itertools::{repeat_n, Itertools};
22
use rustc::hir::*;
33
use rustc::lint::*;
4-
use rustc::ty::TypeVariants;
4+
use rustc::ty::{AssociatedKind, TypeVariants};
55
use syntax::ast::NodeId;
66

77
use std::collections::HashSet;
@@ -108,30 +108,37 @@ fn check_expr_for_collect<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr
108108
// Get the type of the Item associated to the Iterator on which collect() is
109109
// called.
110110
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+
);
120125

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+
}
135142
};
136143
}
137144
}

0 commit comments

Comments
 (0)