Skip to content

Commit 4f801a2

Browse files
author
Grzegorz
committed
redundant closure triggers for fnptrs and closures
1 parent aa9cf07 commit 4f801a2

File tree

4 files changed

+37
-2
lines changed

4 files changed

+37
-2
lines changed

clippy_lints/src/eta_reduction.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use if_chain::if_chain;
2+
use matches::matches;
23
use rustc::hir::*;
34
use rustc::lint::{in_external_macro, LateContext, LateLintPass, LintArray, LintContext, LintPass};
45
use rustc::ty::{self, Ty};
@@ -66,7 +67,7 @@ fn check_closure(cx: &LateContext<'_, '_>, expr: &Expr) {
6667

6768
let fn_ty = cx.tables.expr_ty(caller);
6869

69-
if let ty::FnDef(_, _) = fn_ty.sty;
70+
if matches!(fn_ty.sty, ty::FnDef(_, _) | ty::FnPtr(_) | ty::Closure(_, _));
7071

7172
if !type_is_unsafe_function(cx, fn_ty);
7273

tests/ui/eta.fixed

+11
Original file line numberDiff line numberDiff line change
@@ -138,3 +138,14 @@ fn passes_fn_mut(mut x: Box<dyn FnMut()>) {
138138
requires_fn_once(|| x());
139139
}
140140
fn requires_fn_once<T: FnOnce()>(_: T) {}
141+
142+
fn test_redundant_closure_with_function_pointer() {
143+
type FnPtrType = fn(u8);
144+
let foo_ptr: FnPtrType = foo;
145+
let a = Some(1u8).map(foo_ptr);
146+
}
147+
148+
fn test_redundant_closure_with_another_closure() {
149+
let closure = |a| println!("{}", a);
150+
let a = Some(1u8).map(closure);
151+
}

tests/ui/eta.rs

+11
Original file line numberDiff line numberDiff line change
@@ -138,3 +138,14 @@ fn passes_fn_mut(mut x: Box<dyn FnMut()>) {
138138
requires_fn_once(|| x());
139139
}
140140
fn requires_fn_once<T: FnOnce()>(_: T) {}
141+
142+
fn test_redundant_closure_with_function_pointer() {
143+
type FnPtrType = fn(u8);
144+
let foo_ptr: FnPtrType = foo;
145+
let a = Some(1u8).map(|a| foo_ptr(a));
146+
}
147+
148+
fn test_redundant_closure_with_another_closure() {
149+
let closure = |a| println!("{}", a);
150+
let a = Some(1u8).map(|a| closure(a));
151+
}

tests/ui/eta.stderr

+13-1
Original file line numberDiff line numberDiff line change
@@ -68,5 +68,17 @@ error: redundant closure found
6868
LL | let e: std::vec::Vec<char> = vec!['a', 'b', 'c'].iter().map(|c| c.to_ascii_uppercase()).collect();
6969
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove closure as shown: `char::to_ascii_uppercase`
7070

71-
error: aborting due to 11 previous errors
71+
error: redundant closure found
72+
--> $DIR/eta.rs:145:27
73+
|
74+
LL | let a = Some(1u8).map(|a| foo_ptr(a));
75+
| ^^^^^^^^^^^^^^ help: remove closure as shown: `foo_ptr`
76+
77+
error: redundant closure found
78+
--> $DIR/eta.rs:150:27
79+
|
80+
LL | let a = Some(1u8).map(|a| closure(a));
81+
| ^^^^^^^^^^^^^^ help: remove closure as shown: `closure`
82+
83+
error: aborting due to 13 previous errors
7284

0 commit comments

Comments
 (0)