Skip to content

Commit 5f5fa08

Browse files
authored
Merge pull request rust-lang#2864 from flip1995/issue-2862
Fix panic on map_clone lint
2 parents 25510cf + 6224e19 commit 5f5fa08

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

clippy_lints/src/map_clone.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
3636
ExprClosure(_, ref decl, closure_eid, _, _) => {
3737
let body = cx.tcx.hir.body(closure_eid);
3838
let closure_expr = remove_blocks(&body.value);
39-
let ty = cx.tables.pat_ty(&body.arguments[0].pat);
4039
if_chain! {
4140
// nothing special in the argument, besides reference bindings
4241
// (e.g. .map(|&x| x) )
@@ -45,6 +44,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
4544
// the method is being called on a known type (option or iterator)
4645
if let Some(type_name) = get_type_name(cx, expr, &args[0]);
4746
then {
47+
// We know that body.arguments is not empty at this point
48+
let ty = cx.tables.pat_ty(&body.arguments[0].pat);
4849
// look for derefs, for .map(|x| *x)
4950
if only_derefs(cx, &*closure_expr, arg_ident) &&
5051
// .cloned() only removes one level of indirection, don't lint on more

tests/run-pass/issue-2862.rs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
pub trait FooMap {
2+
fn map<B, F: Fn() -> B>(&self, f: F) -> B;
3+
}
4+
5+
impl FooMap for bool {
6+
fn map<B, F: Fn() -> B>(&self, f: F) -> B {
7+
f()
8+
}
9+
}
10+
11+
fn main() {
12+
let a = true;
13+
a.map(|| false);
14+
}

0 commit comments

Comments
 (0)