mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-10 15:14:29 +00:00
redundant closure triggers for fnptrs and closures
This commit is contained in:
parent
aa9cf07d56
commit
4f801a278d
4 changed files with 37 additions and 2 deletions
|
@ -1,4 +1,5 @@
|
||||||
use if_chain::if_chain;
|
use if_chain::if_chain;
|
||||||
|
use matches::matches;
|
||||||
use rustc::hir::*;
|
use rustc::hir::*;
|
||||||
use rustc::lint::{in_external_macro, LateContext, LateLintPass, LintArray, LintContext, LintPass};
|
use rustc::lint::{in_external_macro, LateContext, LateLintPass, LintArray, LintContext, LintPass};
|
||||||
use rustc::ty::{self, Ty};
|
use rustc::ty::{self, Ty};
|
||||||
|
@ -66,7 +67,7 @@ fn check_closure(cx: &LateContext<'_, '_>, expr: &Expr) {
|
||||||
|
|
||||||
let fn_ty = cx.tables.expr_ty(caller);
|
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);
|
if !type_is_unsafe_function(cx, fn_ty);
|
||||||
|
|
||||||
|
|
|
@ -138,3 +138,14 @@ fn passes_fn_mut(mut x: Box<dyn FnMut()>) {
|
||||||
requires_fn_once(|| x());
|
requires_fn_once(|| x());
|
||||||
}
|
}
|
||||||
fn requires_fn_once<T: FnOnce()>(_: T) {}
|
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);
|
||||||
|
}
|
||||||
|
|
|
@ -138,3 +138,14 @@ fn passes_fn_mut(mut x: Box<dyn FnMut()>) {
|
||||||
requires_fn_once(|| x());
|
requires_fn_once(|| x());
|
||||||
}
|
}
|
||||||
fn requires_fn_once<T: FnOnce()>(_: T) {}
|
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));
|
||||||
|
}
|
||||||
|
|
|
@ -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();
|
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`
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ 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
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue