mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-28 01:34:21 +00:00
Check for arguments before accessing the first arg
This commit is contained in:
parent
25510cfb13
commit
6224e19b80
@ -36,7 +36,6 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
|
||||
ExprClosure(_, ref decl, closure_eid, _, _) => {
|
||||
let body = cx.tcx.hir.body(closure_eid);
|
||||
let closure_expr = remove_blocks(&body.value);
|
||||
let ty = cx.tables.pat_ty(&body.arguments[0].pat);
|
||||
if_chain! {
|
||||
// nothing special in the argument, besides reference bindings
|
||||
// (e.g. .map(|&x| x) )
|
||||
@ -45,6 +44,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
|
||||
// the method is being called on a known type (option or iterator)
|
||||
if let Some(type_name) = get_type_name(cx, expr, &args[0]);
|
||||
then {
|
||||
// We know that body.arguments is not empty at this point
|
||||
let ty = cx.tables.pat_ty(&body.arguments[0].pat);
|
||||
// look for derefs, for .map(|x| *x)
|
||||
if only_derefs(cx, &*closure_expr, arg_ident) &&
|
||||
// .cloned() only removes one level of indirection, don't lint on more
|
||||
|
14
tests/run-pass/issue-2862.rs
Normal file
14
tests/run-pass/issue-2862.rs
Normal file
@ -0,0 +1,14 @@
|
||||
pub trait FooMap {
|
||||
fn map<B, F: Fn() -> B>(&self, f: F) -> B;
|
||||
}
|
||||
|
||||
impl FooMap for bool {
|
||||
fn map<B, F: Fn() -> B>(&self, f: F) -> B {
|
||||
f()
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let a = true;
|
||||
a.map(|| false);
|
||||
}
|
Loading…
Reference in New Issue
Block a user