-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Closed
Labels
C-bugCategory: Clippy is not doing the correct thingCategory: Clippy is not doing the correct thingC-enhancementCategory: Enhancement of lints, like adding more cases or adding help messagesCategory: Enhancement of lints, like adding more cases or adding help messages
Description
fn count_and_sum(numbers: &Vec<u32>) -> (usize, u32) {
let mut sum: u32 = 0;
let count = numbers.iter().map(|n| sum += n).count();
(count, sum)
}
In this example, we use map
to process all values and do something with them that involves another mutable variable or function, in the end, we use count()
to get the total number of items processed.
The lint explanation says
Maybe map was confused with filter. If the map call is intentional, this should be rewritten. Or, if you intend to drive the iterator to completion, you can just use for_each instead.
But a filter is not useful here. Using for_each
is out of option since for_each().count()
does not work since for_each()
does not return anything.
I'm not sure if inspect()
is the right choice here, at least it is not noted in the lints description.
demurgos
Metadata
Metadata
Assignees
Labels
C-bugCategory: Clippy is not doing the correct thingCategory: Clippy is not doing the correct thingC-enhancementCategory: Enhancement of lints, like adding more cases or adding help messagesCategory: Enhancement of lints, like adding more cases or adding help messages