rust-lang / rust-lang/rust-clippy

False warning: unnecessary use of `cloned`

Open
#16,212 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Summary
warning: failed to automatically apply fixes suggested by rustc to crate `dispenser`

after fixes were automatically applied the compiler reported errors within these files:

  * src/main.rs

This likely indicates a bug in either rustc or cargo itself,
and we would appreciate a bug report! You're likely to see
a number of compiler warnings after this message which cargo
attempted to fix but failed. If you could open an issue at
https://github.com/rust-lang/rust-clippy/issues
quoting the full output of this command we'd be very appreciative!
Note that you may be able to make some more progress in the near-term
fixing code with the `--broken-code` flag

The following errors were reported:
error[E0597]: `instances.inner` does not live long enough
  --> src/main.rs:53:25
   |
46 |           let instances = instances.lock().await.clone();
   |               --------- binding `instances` declared here
...
53 |           for instance in instances.inner.iter() {
   |                           ^^^^^^^^^^^^^^^ borrowed value does not live long enough
54 | /             tokio::spawn(async move {
55 | |                 instance.lock().await.poll(poll_images).await;
56 | |             });
   | |______________- argument requires that `instances.inner` is borrowed for `'static`
57 |           }
58 |       }
   |       - `instances.inner` dropped here while still borrowed

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0597`.
Original diagnostics will follow.

warning: unnecessary use of `cloned`
  --> src/main.rs:53:25
   |
53 |         for instance in instances.inner.iter().cloned() {
   |                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove any references to the binding: `instances.inner.iter()`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/rust-1.91.0/index.html#unnecessary_to_owned
   = note: `#[warn(clippy::unnecessary_to_owned)]` on by default

warning: `dispenser` (bin "dispenser") generated 1 warning (run `cargo clippy --fix --bin "dispenser"` to apply 1 suggestion)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.27s
Lint Name

unnecessary_to_owned

Reproducer

I tried this code:

<code>

I saw this happen:

<output>

I expected to see this happen:

Version

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 by reconstructing the reported src/main.rs case and running cargo clippy --fix --bin "dispenser" to confirm the unnecessary_to_owned suggestion and resulting E0597 error. Trace the lint's handling of .iter().cloned() in the Clippy source and add a regression test showing that the suggestion must not produce code that fails to compile.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.