rust-lang / rust-lang/rust-clippy

Incorrect collect suggestion - borrow doesn't live long enough

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

Nobody has claimed this yet.

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

Description

I tried this code:

    let string_clone: Vec<_> = concat_string.item.lines().map(|x| x.to_string()).collect();

    if objects {
        Ok(string_clone
            .into_iter()...

Clippy suggests turning this into:

Ok(concat_string.item.lines().map(|x| x.to_string())...

I expected to see this happen: clippy to suggest a correct replacement

Instead, this happened: clippy suggests the above, which will not compile as concat_string.item will not live long enough.

Meta
  • cargo clippy -V: e.g. clippy 0.0.212 (f455e46 2020-06-20)
  • rustc -Vv:
C:\Source\nushell(main)> cargo clippy -V
clippy 0.1.54 (ed597e7e 2021-06-08)
C:\Source\nushell(main)> rustc -Vv
rustc 1.54.0-nightly (ed597e7e1 2021-06-08)
binary: rustc
commit-hash: ed597e7e19d0fe716d9f81b1e840a5abbfd7c28d
commit-date: 2021-06-08
host: x86_64-pc-windows-msvc
release: 1.54.0-nightly
LLVM version: 12.0.1
Backtrace

<backtrace>

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 reproducing the reported snippet with cargo clippy and identify which lint emits the collect suggestion. Trace that lint's suggestion logic, then add a regression case showing that the suggested replacement compiles and preserves the intended lifetime behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.