mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-14 00:47:16 +00:00
Fixing eta with respect to lazy evaluation.
This commit is contained in:
parent
01ab9fe653
commit
86bab94631
4 changed files with 19 additions and 15 deletions
|
@ -82,6 +82,8 @@ fn check_closure(cx: &LateContext<'_, '_>, expr: &Expr) {
|
||||||
if_chain!(
|
if_chain!(
|
||||||
if let ExprKind::Call(ref caller, ref args) = ex.node;
|
if let ExprKind::Call(ref caller, ref args) = ex.node;
|
||||||
|
|
||||||
|
if let ExprKind::Path(_) = caller.node;
|
||||||
|
|
||||||
// Not the same number of arguments, there is no way the closure is the same as the function return;
|
// Not the same number of arguments, there is no way the closure is the same as the function return;
|
||||||
if args.len() == decl.inputs.len();
|
if args.len() == decl.inputs.len();
|
||||||
|
|
||||||
|
|
|
@ -20,7 +20,7 @@ use std::path::PathBuf;
|
||||||
fn main() {
|
fn main() {
|
||||||
let a = Some(1u8).map(foo);
|
let a = Some(1u8).map(foo);
|
||||||
meta(foo);
|
meta(foo);
|
||||||
let c = Some(1u8).map({1+2; foo});
|
let c = Some(1u8).map(|a| {1+2; foo}(a));
|
||||||
let d = Some(1u8).map(|a| foo((|b| foo2(b))(a))); //is adjusted?
|
let d = Some(1u8).map(|a| foo((|b| foo2(b))(a))); //is adjusted?
|
||||||
all(&[1, 2, 3], &2, |x, y| below(x, y)); //is adjusted
|
all(&[1, 2, 3], &2, |x, y| below(x, y)); //is adjusted
|
||||||
unsafe {
|
unsafe {
|
||||||
|
@ -105,7 +105,7 @@ fn test_redundant_closures_containing_method_calls() {
|
||||||
|
|
||||||
let mut some = Some(|x| x * x);
|
let mut some = Some(|x| x * x);
|
||||||
let arr = [Ok(1), Err(2)];
|
let arr = [Ok(1), Err(2)];
|
||||||
let _: Vec<_> = arr.iter().map(|x| x.map_err(some.take().unwrap())).collect();
|
let _: Vec<_> = arr.iter().map(|x| x.map_err(|e| some.take().unwrap()(e))).collect();
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Thunk<T>(Box<dyn FnMut() -> T>);
|
struct Thunk<T>(Box<dyn FnMut() -> T>);
|
||||||
|
@ -177,3 +177,10 @@ fn test_redundant_closure_with_another_closure() {
|
||||||
let closure = |a| println!("{}", a);
|
let closure = |a| println!("{}", a);
|
||||||
let a = Some(1u8).map(closure);
|
let a = Some(1u8).map(closure);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn make_lazy(f: fn() -> fn(u8) -> u8) -> impl Fn(u8) -> u8 {
|
||||||
|
// Currently f is called when result of make_lazy is called.
|
||||||
|
// If the closure is removed, f will be called when make_lazy itself is
|
||||||
|
// called. This changes semantics, so the closure must stay.
|
||||||
|
Box::new(move |x| f()(x))
|
||||||
|
}
|
||||||
|
|
|
@ -177,3 +177,10 @@ fn test_redundant_closure_with_another_closure() {
|
||||||
let closure = |a| println!("{}", a);
|
let closure = |a| println!("{}", a);
|
||||||
let a = Some(1u8).map(|a| closure(a));
|
let a = Some(1u8).map(|a| closure(a));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn make_lazy(f: fn() -> fn(u8) -> u8) -> impl Fn(u8) -> u8 {
|
||||||
|
// Currently f is called when result of make_lazy is called.
|
||||||
|
// If the closure is removed, f will be called when make_lazy itself is
|
||||||
|
// called. This changes semantics, so the closure must stay.
|
||||||
|
Box::new(move |x| f()(x))
|
||||||
|
}
|
||||||
|
|
|
@ -12,12 +12,6 @@ error: redundant closure found
|
||||||
LL | meta(|a| foo(a));
|
LL | meta(|a| foo(a));
|
||||||
| ^^^^^^^^^^ help: remove closure as shown: `foo`
|
| ^^^^^^^^^^ help: remove closure as shown: `foo`
|
||||||
|
|
||||||
error: redundant closure found
|
|
||||||
--> $DIR/eta.rs:23:27
|
|
||||||
|
|
|
||||||
LL | let c = Some(1u8).map(|a| {1+2; foo}(a));
|
|
||||||
| ^^^^^^^^^^^^^^^^^ help: remove closure as shown: `{1+2; foo}`
|
|
||||||
|
|
||||||
error: this expression borrows a reference that is immediately dereferenced by the compiler
|
error: this expression borrows a reference that is immediately dereferenced by the compiler
|
||||||
--> $DIR/eta.rs:25:21
|
--> $DIR/eta.rs:25:21
|
||||||
|
|
|
|
||||||
|
@ -70,12 +64,6 @@ 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: redundant closure found
|
|
||||||
--> $DIR/eta.rs:108:50
|
|
||||||
|
|
|
||||||
LL | let _: Vec<_> = arr.iter().map(|x| x.map_err(|e| some.take().unwrap()(e))).collect();
|
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove closure as shown: `some.take().unwrap()`
|
|
||||||
|
|
||||||
error: redundant closure found
|
error: redundant closure found
|
||||||
--> $DIR/eta.rs:173:27
|
--> $DIR/eta.rs:173:27
|
||||||
|
|
|
|
||||||
|
@ -88,5 +76,5 @@ error: redundant closure found
|
||||||
LL | let a = Some(1u8).map(|a| closure(a));
|
LL | let a = Some(1u8).map(|a| closure(a));
|
||||||
| ^^^^^^^^^^^^^^ help: remove closure as shown: `closure`
|
| ^^^^^^^^^^^^^^ help: remove closure as shown: `closure`
|
||||||
|
|
||||||
error: aborting due to 14 previous errors
|
error: aborting due to 12 previous errors
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue