rust-lang / rust-lang/rust-clippy

Clippy makes a broken correction to bad collect()

Open
#8,965 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug I-suggestion-causes-error
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

Summary

Playground to the sample working code:

https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=f728413bb6ef3793b4075f6b51fc8a2a

Clippy suggests changing the main function from this:

    let size = 3;
    let ids: HashSet<&str> = HashSet::new();
    let chunked_ids = ids
        .iter()
        .map(|s| s.to_string())
        .chunks(size)
        .into_iter()
        .map(|chunk| chunk.collect())
        .collect::<Vec<Vec<String>>>();

    let tasks = chunked_ids
        .into_iter()
        .map(|c| tokio::spawn(async move { fetch_chunk(&c, size).await }));
    let _set = futures::future::join_all(tasks).await;

To this:

    let size = 3;
    let ids: HashSet<&str> = HashSet::new();
    

    let tasks = ids
        .iter()
        .map(|s| s.to_string())
        .chunks(size)
        .into_iter()
        .map(|chunk| chunk.collect())
        .map(|c| tokio::spawn(async move { fetch_chunk(&c, size).await }));
    let _set = futures::future::join_all(tasks).await;

(Note it also inserts an extra unneeded whitespace, not sure what for, but that isn't the main issue)

This won't compile though:

image

I think it meant to do this:

    let size = 3;
    let ids: HashSet<&str> = HashSet::new();
    let chunks = ids
        .iter()
        .map(|s| s.to_string())
        .chunks(size);

    let tasks = chunks
        .into_iter()
        .map(|chunk| chunk.collect::<Vec<String>>())
        .map(|c| tokio::spawn(async move { fetch_chunk(&c, size).await }));
    let _set = futures::future::join_all(tasks).await;
Reproducer

See summary

Version
rustc 1.61.0 (fe5b13d68 2022-05-18)
binary: rustc
commit-hash: fe5b13d681f25ee6474be29d748c65adcd91f69e
commit-date: 2022-05-18
host: x86_64-pc-windows-msvc
release: 1.61.0
LLVM version: 14.0.0
Additional Labels

No response

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 with the linked Rust Playground reproducer and identify which Clippy lint produces the broken collect() suggestion. Compare the suggested correction with the compiling example in the issue; done means the diagnostic produces a correction that compiles and preserves the intended chunk collection behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
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.