Skip to content

Commit b93d545

Browse files
committed
Add ui test for map_unit_fn lint in closure case
1 parent a87443a commit b93d545

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

tests/ui/lint/lint_map_unit_fn.rs

+9
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,13 @@ fn main() {
88
let mut x: Vec<Vec<u8>> = vec![vec![0, 2, 1], vec![5, 4, 3]];
99
x.iter_mut().map(foo);
1010
//~^ ERROR `Iterator::map` call that discard the iterator's values
11+
x.iter_mut().map(|items| {
12+
//~^ ERROR `Iterator::map` call that discard the iterator's values
13+
items.sort();
14+
});
15+
let f = |items: &mut Vec<u8>| {
16+
items.sort();
17+
};
18+
x.iter_mut().map(f);
19+
//~^ ERROR `Iterator::map` call that discard the iterator's values
1120
}

tests/ui/lint/lint_map_unit_fn.stderr

+42-1
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,46 @@ help: you might have meant to use `Iterator::for_each`
2121
LL | x.iter_mut().for_each(foo);
2222
| ~~~~~~~~
2323

24-
error: aborting due to previous error
24+
error: `Iterator::map` call that discard the iterator's values
25+
--> $DIR/lint_map_unit_fn.rs:11:18
26+
|
27+
LL | x.iter_mut().map(|items| {
28+
| ^ -------
29+
| | |
30+
| ____________________|___this function returns `()`, which is likely not what you wanted
31+
| | __________________|
32+
| | |
33+
LL | | |
34+
LL | | | items.sort();
35+
LL | | | });
36+
| | | -^ after this call to map, the resulting iterator is `impl Iterator<Item = ()>`, which means the only information carried by the iterator is the number of items
37+
| | |_____||
38+
| |_______|
39+
| called `Iterator::map` with callable that returns `()`
40+
|
41+
= note: `Iterator::map`, like many of the methods on `Iterator`, gets executed lazily, meaning that its effects won't be visible until it is iterated
42+
help: you might have meant to use `Iterator::for_each`
43+
|
44+
LL | x.iter_mut().for_each(|items| {
45+
| ~~~~~~~~
46+
47+
error: `Iterator::map` call that discard the iterator's values
48+
--> $DIR/lint_map_unit_fn.rs:18:18
49+
|
50+
LL | let f = |items: &mut Vec<u8>| {
51+
| --------------------- this function returns `()`, which is likely not what you wanted
52+
...
53+
LL | x.iter_mut().map(f);
54+
| ^^^^-^
55+
| | |
56+
| | called `Iterator::map` with callable that returns `()`
57+
| after this call to map, the resulting iterator is `impl Iterator<Item = ()>`, which means the only information carried by the iterator is the number of items
58+
|
59+
= note: `Iterator::map`, like many of the methods on `Iterator`, gets executed lazily, meaning that its effects won't be visible until it is iterated
60+
help: you might have meant to use `Iterator::for_each`
61+
|
62+
LL | x.iter_mut().for_each(f);
63+
| ~~~~~~~~
64+
65+
error: aborting due to 3 previous errors
2566

0 commit comments

Comments
 (0)