Skip to content

Commit ddd7d10

Browse files
committed
Add ui test for map_unit_fn lint
1 parent a914f37 commit ddd7d10

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

tests/ui/lint/lint_map_unit_fn.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#![deny(map_unit_fn)]
2+
3+
fn foo(items: &mut Vec<u8>) {
4+
items.sort();
5+
}
6+
7+
fn main() {
8+
let mut x: Vec<Vec<u8>> = vec![vec![0, 2, 1], vec![5, 4, 3]];
9+
x.iter_mut().map(foo);
10+
//~^ ERROR `Iterator::map` call that discard the iterator's values
11+
}

tests/ui/lint/lint_map_unit_fn.stderr

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
error: `Iterator::map` call that discard the iterator's values
2+
--> $DIR/lint_map_unit_fn.rs:9:18
3+
|
4+
LL | fn foo(items: &mut Vec<u8>) {
5+
| --------------------------- this function returns `()`, which is likely not what you wanted
6+
...
7+
LL | x.iter_mut().map(foo);
8+
| ^^^^---^
9+
| | |
10+
| | called `Iterator::map` with callable that returns `()`
11+
| 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
12+
|
13+
= 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
14+
note: the lint level is defined here
15+
--> $DIR/lint_map_unit_fn.rs:1:9
16+
|
17+
LL | #![deny(map_unit_fn)]
18+
| ^^^^^^^^^^^
19+
help: you might have meant to use `Iterator::for_each`
20+
|
21+
LL | x.iter_mut().for_each(foo);
22+
| ~~~~~~~~
23+
24+
error: aborting due to previous error
25+

0 commit comments

Comments
 (0)