Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lint against Iterator::map receiving a callable that returns () #107890

Merged
merged 5 commits into from
Feb 26, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add ui test for map_unit_fn lint
  • Loading branch information
obeis committed Feb 23, 2023
commit ddd7d1087998ef692e46520d0ceb56a0eb5fd7a6
11 changes: 11 additions & 0 deletions tests/ui/lint/lint_map_unit_fn.rs
Original file line number Diff line number Diff line change
@@ -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
}
25 changes: 25 additions & 0 deletions tests/ui/lint/lint_map_unit_fn.stderr
Original file line number Diff line number Diff line change
@@ -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