From 4f801a278dc4ed68ef209f07fece4d8b8e99fbeb Mon Sep 17 00:00:00 2001 From: Grzegorz Date: Sat, 20 Apr 2019 22:20:14 +0200 Subject: [PATCH] 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