Add ui test for map_unit_fn lint

This commit is contained in:
Obei Sideg 2023-02-16 22:05:35 +03:00
parent a914f37409
commit ddd7d10879
2 changed files with 36 additions and 0 deletions

View File

@ -0,0 +1,11 @@
#![deny(map_unit_fn)]
fn foo(items: &mut Vec<u8>) {
items.sort();
}
fn main() {
let mut x: Vec<Vec<u8>> = vec![vec![0, 2, 1], vec![5, 4, 3]];
x.iter_mut().map(foo);
//~^ ERROR `Iterator::map` call that discard the iterator's values
}

View File

@ -0,0 +1,25 @@
error: `Iterator::map` call that discard the iterator's values
--> $DIR/lint_map_unit_fn.rs:9:18
|
LL | fn foo(items: &mut Vec<u8>) {
| --------------------------- this function returns `()`, which is likely not what you wanted
...
LL | x.iter_mut().map(foo);
| ^^^^---^
| | |
| | called `Iterator::map` with callable that returns `()`
| 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
|
= 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
note: the lint level is defined here
--> $DIR/lint_map_unit_fn.rs:1:9
|
LL | #![deny(map_unit_fn)]
| ^^^^^^^^^^^
help: you might have meant to use `Iterator::for_each`
|
LL | x.iter_mut().for_each(foo);
| ~~~~~~~~
error: aborting due to previous error