Skip to content

Commit

Permalink
Add two more tests for redundant_closure
Browse files Browse the repository at this point in the history
These two cases were fixed by rust-lang#4008.

Closes rust-lang#1439

changelog: none
  • Loading branch information
phansch committed Apr 26, 2019
1 parent 910d538 commit 3f637cb
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 3 deletions.
24 changes: 24 additions & 0 deletions tests/ui/eta.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
24 changes: 24 additions & 0 deletions tests/ui/eta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 9 additions & 3 deletions tests/ui/eta.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 3f637cb

Please sign in to comment.