redundant closure triggers for fnptrs and closures

This commit is contained in:
Grzegorz 2019-04-20 22:20:14 +02:00
parent aa9cf07d56
commit 4f801a278d
4 changed files with 37 additions and 2 deletions

View file

@ -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);

View file

@ -138,3 +138,14 @@ fn passes_fn_mut(mut x: Box<dyn FnMut()>) {
requires_fn_once(|| x());
}
fn requires_fn_once<T: FnOnce()>(_: 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);
}

View file

@ -138,3 +138,14 @@ fn passes_fn_mut(mut x: Box<dyn FnMut()>) {
requires_fn_once(|| x());
}
fn requires_fn_once<T: FnOnce()>(_: 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));
}

View file

@ -68,5 +68,17 @@ error: redundant closure found
LL | let e: std::vec::Vec<char> = 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