Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 650e1c9

Browse files
committedApr 29, 2019
Auto merge of #4032 - phansch:add_tests, r=phansch
Add two more tests for redundant_closure These two cases were fixed by #4008. Closes #1439 changelog: none
2 parents 9e07b35 + 3f637cb commit 650e1c9

File tree

3 files changed

+57
-3
lines changed

3 files changed

+57
-3
lines changed
 

‎tests/ui/eta.fixed

+24
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,30 @@ fn test_redundant_closures_containing_method_calls() {
9898
t.iter().filter(|x| x.trait_foo_ref());
9999
t.iter().map(|x| x.trait_foo_ref());
100100
}
101+
102+
let mut some = Some(|x| x * x);
103+
let arr = [Ok(1), Err(2)];
104+
let _: Vec<_> = arr.iter().map(|x| x.map_err(some.take().unwrap())).collect();
105+
}
106+
107+
struct Thunk<T>(Box<FnMut() -> T>);
108+
109+
impl<T> Thunk<T> {
110+
fn new<F: 'static + FnOnce() -> T>(f: F) -> Thunk<T> {
111+
let mut option = Some(f);
112+
// This should not trigger redundant_closure (#1439)
113+
Thunk(Box::new(move || option.take().unwrap()()))
114+
}
115+
116+
fn unwrap(self) -> T {
117+
let Thunk(mut f) = self;
118+
f()
119+
}
120+
}
121+
122+
fn foobar() {
123+
let thunk = Thunk::new(|| println!("Hello, world!"));
124+
thunk.unwrap()
101125
}
102126

103127
fn meta<F>(f: F)

‎tests/ui/eta.rs

+24
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,30 @@ fn test_redundant_closures_containing_method_calls() {
9898
t.iter().filter(|x| x.trait_foo_ref());
9999
t.iter().map(|x| x.trait_foo_ref());
100100
}
101+
102+
let mut some = Some(|x| x * x);
103+
let arr = [Ok(1), Err(2)];
104+
let _: Vec<_> = arr.iter().map(|x| x.map_err(|e| some.take().unwrap()(e))).collect();
105+
}
106+
107+
struct Thunk<T>(Box<FnMut() -> T>);
108+
109+
impl<T> Thunk<T> {
110+
fn new<F: 'static + FnOnce() -> T>(f: F) -> Thunk<T> {
111+
let mut option = Some(f);
112+
// This should not trigger redundant_closure (#1439)
113+
Thunk(Box::new(move || option.take().unwrap()()))
114+
}
115+
116+
fn unwrap(self) -> T {
117+
let Thunk(mut f) = self;
118+
f()
119+
}
120+
}
121+
122+
fn foobar() {
123+
let thunk = Thunk::new(|| println!("Hello, world!"));
124+
thunk.unwrap()
101125
}
102126

103127
fn meta<F>(f: F)

‎tests/ui/eta.stderr

+9-3
Original file line numberDiff line numberDiff line change
@@ -69,16 +69,22 @@ LL | let e: std::vec::Vec<char> = vec!['a', 'b', 'c'].iter().map(|c| c.to_as
6969
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove closure as shown: `char::to_ascii_uppercase`
7070

7171
error: redundant closure found
72-
--> $DIR/eta.rs:145:27
72+
--> $DIR/eta.rs:104:50
73+
|
74+
LL | let _: Vec<_> = arr.iter().map(|x| x.map_err(|e| some.take().unwrap()(e))).collect();
75+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove closure as shown: `some.take().unwrap()`
76+
77+
error: redundant closure found
78+
--> $DIR/eta.rs:169:27
7379
|
7480
LL | let a = Some(1u8).map(|a| foo_ptr(a));
7581
| ^^^^^^^^^^^^^^ help: remove closure as shown: `foo_ptr`
7682

7783
error: redundant closure found
78-
--> $DIR/eta.rs:150:27
84+
--> $DIR/eta.rs:174:27
7985
|
8086
LL | let a = Some(1u8).map(|a| closure(a));
8187
| ^^^^^^^^^^^^^^ help: remove closure as shown: `closure`
8288

83-
error: aborting due to 13 previous errors
89+
error: aborting due to 14 previous errors
8490

0 commit comments

Comments
 (0)
Please sign in to comment.