Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add two more tests for redundant_closure #4032

Merged
merged 1 commit into from Apr 29, 2019
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.
+57 −3
Diff settings

Always

Just for now

@@ -98,6 +98,30 @@ fn test_redundant_closures_containing_method_calls() {
t.iter().filter(|x| x.trait_foo_ref());
t.iter().map(|x| x.trait_foo_ref());
}

let mut some = Some(|x| x * x);
let arr = [Ok(1), Err(2)];
let _: Vec<_> = arr.iter().map(|x| x.map_err(some.take().unwrap())).collect();
}

struct Thunk<T>(Box<FnMut() -> T>);

impl<T> Thunk<T> {
fn new<F: 'static + FnOnce() -> T>(f: F) -> Thunk<T> {
let mut option = Some(f);
// This should not trigger redundant_closure (#1439)
Thunk(Box::new(move || option.take().unwrap()()))
}

fn unwrap(self) -> T {
let Thunk(mut f) = self;
f()
}
}

fn foobar() {
let thunk = Thunk::new(|| println!("Hello, world!"));
thunk.unwrap()
}

fn meta<F>(f: F)
@@ -98,6 +98,30 @@ fn test_redundant_closures_containing_method_calls() {
t.iter().filter(|x| x.trait_foo_ref());
t.iter().map(|x| x.trait_foo_ref());
}

let mut some = Some(|x| x * x);
let arr = [Ok(1), Err(2)];
let _: Vec<_> = arr.iter().map(|x| x.map_err(|e| some.take().unwrap()(e))).collect();
}

struct Thunk<T>(Box<FnMut() -> T>);

impl<T> Thunk<T> {
fn new<F: 'static + FnOnce() -> T>(f: F) -> Thunk<T> {
let mut option = Some(f);
// This should not trigger redundant_closure (#1439)
Thunk(Box::new(move || option.take().unwrap()()))
}

fn unwrap(self) -> T {
let Thunk(mut f) = self;
f()
}
}

fn foobar() {
let thunk = Thunk::new(|| println!("Hello, world!"));
thunk.unwrap()
}

fn meta<F>(f: F)
@@ -69,16 +69,22 @@ LL | let e: std::vec::Vec<char> = vec!['a', 'b', 'c'].iter().map(|c| c.to_as
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove closure as shown: `char::to_ascii_uppercase`

error: redundant closure found
--> $DIR/eta.rs:145:27
--> $DIR/eta.rs:104: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
--> $DIR/eta.rs:169: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
--> $DIR/eta.rs:174:27
|
LL | let a = Some(1u8).map(|a| closure(a));
| ^^^^^^^^^^^^^^ help: remove closure as shown: `closure`

error: aborting due to 13 previous errors
error: aborting due to 14 previous errors

ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.