From aa9cf07d56fac7044e6890d66759ab9903d352ef Mon Sep 17 00:00:00 2001 From: Grzegorz Date: Sun, 31 Mar 2019 14:10:17 +0200 Subject: [PATCH 1/2] redundant closure for functions restricted to FnDefs --- clippy_lints/src/eta_reduction.rs | 3 +++ tests/ui/eta.fixed | 5 +++++ tests/ui/eta.rs | 5 +++++ 3 files changed, 13 insertions(+) diff --git a/clippy_lints/src/eta_reduction.rs b/clippy_lints/src/eta_reduction.rs index 63abe47b4428..cb17ef18429a 100644 --- a/clippy_lints/src/eta_reduction.rs +++ b/clippy_lints/src/eta_reduction.rs @@ -65,6 +65,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 let ty::FnDef(_, _) = fn_ty.sty; + 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..6f9457a4a29d 100644 --- a/tests/ui/eta.fixed +++ b/tests/ui/eta.fixed @@ -133,3 +133,8 @@ fn divergent(_: u8) -> ! { fn generic(_: T) -> u8 { 0 } + +fn passes_fn_mut(mut x: Box) { + requires_fn_once(|| x()); +} +fn requires_fn_once(_: T) {} diff --git a/tests/ui/eta.rs b/tests/ui/eta.rs index a0d83c438251..11f142e17dfb 100644 --- a/tests/ui/eta.rs +++ b/tests/ui/eta.rs @@ -133,3 +133,8 @@ fn divergent(_: u8) -> ! { fn generic(_: T) -> u8 { 0 } + +fn passes_fn_mut(mut x: Box) { + requires_fn_once(|| x()); +} +fn requires_fn_once(_: T) {} From 4f801a278dc4ed68ef209f07fece4d8b8e99fbeb Mon Sep 17 00:00:00 2001 From: Grzegorz Date: Sat, 20 Apr 2019 22:20:14 +0200 Subject: [PATCH 2/2] redundant closure triggers for fnptrs and closures --- clippy_lints/src/eta_reduction.rs | 3 ++- tests/ui/eta.fixed | 11 +++++++++++ tests/ui/eta.rs | 11 +++++++++++ tests/ui/eta.stderr | 14 +++++++++++++- 4 files changed, 37 insertions(+), 2 deletions(-) diff --git a/clippy_lints/src/eta_reduction.rs b/clippy_lints/src/eta_reduction.rs index cb17ef18429a..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}; @@ -66,7 +67,7 @@ fn check_closure(cx: &LateContext<'_, '_>, expr: &Expr) { let fn_ty = cx.tables.expr_ty(caller); - if let ty::FnDef(_, _) = fn_ty.sty; + if matches!(fn_ty.sty, ty::FnDef(_, _) | ty::FnPtr(_) | ty::Closure(_, _)); if !type_is_unsafe_function(cx, fn_ty); diff --git a/tests/ui/eta.fixed b/tests/ui/eta.fixed index 6f9457a4a29d..bae9b09c6977 100644 --- a/tests/ui/eta.fixed +++ b/tests/ui/eta.fixed @@ -138,3 +138,14 @@ 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 11f142e17dfb..a23da73bde75 100644 --- a/tests/ui/eta.rs +++ b/tests/ui/eta.rs @@ -138,3 +138,14 @@ 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