Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prefer iterator.copied() over iterator.cloned() where possible #3870

Closed
oli-obk opened this issue Mar 11, 2019 · 2 comments · Fixed by #7098
Closed

Prefer iterator.copied() over iterator.cloned() where possible #3870

oli-obk opened this issue Mar 11, 2019 · 2 comments · Fixed by #7098
Labels
A-lint Area: New lints C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages good-first-issue These issues are a good way to get started with Clippy

Comments

@oli-obk
Copy link
Contributor

oli-obk commented Mar 11, 2019

... and adjust our current iterator.map(|x| *x) lint to suggest .copied()

Requires rust-lang/rust#57127 to get merged first though.

@oli-obk oli-obk added C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages A-lint Area: New lints labels Mar 11, 2019
@smklein
Copy link
Contributor

smklein commented Apr 19, 2020

Hey Oli! Is this as simple as the following?

  • Find all cases of iterator.cloned(), for Iterator <Item = T>,
  • If T : Copy, take the span of iterator.cloned() and recommend updating that?

Are there cases where that wouldn't work?

I made the following as a test case: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=28a932471d654ec882306df36e5a7abc

(I'm interested in digging into this more if there is still interest!)

@flip1995
Copy link
Member

Yes that should be exactly as simple as you described. This lint should fit well in the methods module.

@giraffate giraffate added the good-first-issue These issues are a good way to get started with Clippy label Feb 9, 2021
bors added a commit that referenced this issue Apr 16, 2021
Add `cloned_instead_of_copied` lint

Don't go cloning all willy-nilly.

Featuring a new `get_iterator_item_ty` util!

changelog: Add cloned_instead_of_copied lint

Closes #3870
@bors bors closed this as completed in 28dbcd8 Apr 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages good-first-issue These issues are a good way to get started with Clippy
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants