rust-clippy icon indicating copy to clipboard operation
rust-clippy copied to clipboard

`unnecessary_to_owned` false positive when iterating over `Copy` types.

Open jiftoo opened this issue 2 years ago • 0 comments

Summary

Clippy suggests to remove .copied() from an iterator over Copy elements, the which are later borrowed in a `'static' future.

Lint Name

unnecessary_to_owned

Reproducer

I tried this code:

fn foo() {
	let bar: Vec<usize> = Default::default();

	fn fut<F: Future + 'static>(_: F) {}
	fn func(_: &usize) {}

	for site in bar.iter().copied() {
		fut(async move {
			func(&site);
		})
	}
}

I saw this happen:

warning: unnecessary use of `copied`
   --> src\main.rs:204:14
    |
204 |     for site in bar.iter().copied() {
    |                 ^^^^^^^^^^^^^^^^^^^
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_to_owned
    = note: `#[warn(clippy::unnecessary_to_owned)]` on by default
help: use
    |
204 |     for site in bar.iter() {
    |                 ~~~~~~~~~~
help: remove this `&`
    |
206 -             func(&site);
206 +             func(site);
    |

I expected to see this happen:

Lint not triggered.


Note: for &site in bar.iter() { ... } behaves as expected. I believe .copied() in that position is a matter of preference, therefore it must also not trigger a warning. That's my reasoning to opening this issue. Apologies if I'm wrong.

Version

rustc 1.76.0 (07dca489a 2024-02-04)
binary: rustc
commit-hash: 07dca489ac2d933c78d3c5158e3f43beefeb02ce
commit-date: 2024-02-04
host: x86_64-pc-windows-msvc
release: 1.76.0
LLVM version: 17.0.6

Additional Labels

@rustbot label I-suggestion-causes-error

jiftoo avatar Feb 20 '24 23:02 jiftoo