diff --git a/clippy_lints/src/eta_reduction.rs b/clippy_lints/src/eta_reduction.rs index 63abe47b4428..60e2ab8b9b9f 100644 --- a/clippy_lints/src/eta_reduction.rs +++ b/clippy_lints/src/eta_reduction.rs @@ -1,4 +1,5 @@ use if_chain::if_chain; +use matches::matches; use rustc::hir::*; use rustc::lint::{in_external_macro, LateContext, LateLintPass, LintArray, LintContext, LintPass}; use rustc::ty::{self, Ty}; @@ -65,6 +66,9 @@ fn check_closure(cx: &LateContext<'_, '_>, expr: &Expr) { if !(is_adjusted(cx, ex) || args.iter().any(|arg| is_adjusted(cx, arg))); let fn_ty = cx.tables.expr_ty(caller); + + if matches!(fn_ty.sty, ty::FnDef(_, _) | ty::FnPtr(_) | ty::Closure(_, _)); + if !type_is_unsafe_function(cx, fn_ty); if compare_inputs(&mut iter_input_pats(decl, body), &mut args.into_iter()); diff --git a/tests/ui/eta.fixed b/tests/ui/eta.fixed index 7e487fe21679..bae9b09c6977 100644 --- a/tests/ui/eta.fixed +++ b/tests/ui/eta.fixed @@ -133,3 +133,19 @@ fn divergent(_: u8) -> ! { fn generic(_: T) -> u8 { 0 } + +fn passes_fn_mut(mut x: Box) { + requires_fn_once(|| x()); +} +fn requires_fn_once(_: T) {} + +fn test_redundant_closure_with_function_pointer() { + type FnPtrType = fn(u8); + let foo_ptr: FnPtrType = foo; + let a = Some(1u8).map(foo_ptr); +} + +fn test_redundant_closure_with_another_closure() { + let closure = |a| println!("{}", a); + let a = Some(1u8).map(closure); +} diff --git a/tests/ui/eta.rs b/tests/ui/eta.rs index a0d83c438251..a23da73bde75 100644 --- a/tests/ui/eta.rs +++ b/tests/ui/eta.rs @@ -133,3 +133,19 @@ fn divergent(_: u8) -> ! { fn generic(_: T) -> u8 { 0 } + +fn passes_fn_mut(mut x: Box) { + requires_fn_once(|| x()); +} +fn requires_fn_once(_: T) {} + +fn test_redundant_closure_with_function_pointer() { + type FnPtrType = fn(u8); + let foo_ptr: FnPtrType = foo; + let a = Some(1u8).map(|a| foo_ptr(a)); +} + +fn test_redundant_closure_with_another_closure() { + let closure = |a| println!("{}", a); + let a = Some(1u8).map(|a| closure(a)); +} diff --git a/tests/ui/eta.stderr b/tests/ui/eta.stderr index 77423694f815..eb55a251bcc9 100644 --- a/tests/ui/eta.stderr +++ b/tests/ui/eta.stderr @@ -68,5 +68,17 @@ error: redundant closure found LL | let e: std::vec::Vec = vec!['a', 'b', 'c'].iter().map(|c| c.to_ascii_uppercase()).collect(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove closure as shown: `char::to_ascii_uppercase` -error: aborting due to 11 previous errors +error: redundant closure found + --> $DIR/eta.rs:145:27 + | +LL | let a = Some(1u8).map(|a| foo_ptr(a)); + | ^^^^^^^^^^^^^^ help: remove closure as shown: `foo_ptr` + +error: redundant closure found + --> $DIR/eta.rs:150:27 + | +LL | let a = Some(1u8).map(|a| closure(a)); + | ^^^^^^^^^^^^^^ help: remove closure as shown: `closure` + +error: aborting due to 13 previous errors