-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Lint against Iterator::map
receiving a callable that returns ()
#106991
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
Comments
Like this? 😄 https://rust-lang.github.io/rust-clippy/master/index.html#option_map_unit_fn edit: ah this might only lint options but maybe not too hard to expand? |
@matthiaskrgr yes 😅 I'd like to uplift it so that it still triggers even in the face of other errors. If you run clippy on https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=51211f1c12a2b1995b24bab269a02ce0, it will only complain about the return type. |
@rustbot claim |
Lint against `Iterator::map` receiving a callable that returns `()` Close rust-lang#106991
Lint against `Iterator::map` receiving a callable that returns `()` Close rust-lang#106991
I'm reopening the ticket to track the comment in this one case: fn foo(items: &mut Vec<u8>) {
items.sort();
}
fn bar() -> impl Iterator<Item = i32> {
//~^ ERROR expected `foo` to be a fn item that returns `i32`, but it returns `()` [E0271]
let mut x: Vec<Vec<u8>> = vec![vec![0, 2, 1], vec![5, 4, 3]];
x.iter_mut().map(foo)
}
fn main() {
bar();
} should provide more context than
|
Code
Current output
Desired output
Rationale and extra context
Mapping to
()
is almost always a mistake. Thefor_each
suggestion should only be emitted if it would make sense withfoo
, like if it modifies the argument or has side-effects like printing or logging (the later would be hard to check for).Other cases
No response
Anything else?
No response
The text was updated successfully, but these errors were encountered: