rust-lang / rust-lang/rust-clippy

map_collect_result_unit does not recognize for loop

Open
#7,710 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-lint
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

What it does

This is a suggestion for the map_collect_result_unit lint.
I just refactored this piece of code:

fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    for (uuid, err) in self.0.iter() {
        writeln!(f, "{}: {}", uuid, err)?;
    }
    Ok(())
}

into this:

fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    self.0.iter().map(|(uuid, err)| writeln!(f, "{}: {}", uuid, err)).collect()
}

And then the lint told me that I can do

fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    self.0.iter().try_for_each(|(uuid, err)| writeln!(f, "{}: {}", uuid, err))
}

But maybe it is possible that the lint also tells me that my first version can be written better?

I don't know whether this is possible, but it would be nice to catch the for-loop-version as well here.

Categories (optional)
  • Kind: clippy::style

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating the map_collect_result_unit lint implementation and its existing tests. Compare how the lint recognizes iterator-based collection with how it analyzes for loops, then determine whether the example should produce the same try_for_each suggestion. Done means the supported loop form is handled and regression coverage verifies the suggested rewrite.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
devtools
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.