Skip to content

suspicious_map oddity  #5253

Closed
Closed
@matthiaskrgr

Description

@matthiaskrgr
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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingC-enhancementCategory: Enhancement of lints, like adding more cases or adding help messages

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions