rust-lang / rust-lang/rust-clippy
FP cloned_instead_of_copied: explicit Iter::Cloned, HRTBs
Open
Nobody has claimed this yet.
C-bug
I-false-positive
I-suggestion-causes-error
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
Summary
.
Lint Name
cloned_instead_of_copied
Reproducer
I tried this code:
// Tests that HRTBs are correctly accepted -- https://github.com/rust-lang/rust/issues/50301
// check-pass
trait Trait
where
for<'a> &'a Self::IntoIter: IntoIterator<Item = u32>,
{
type IntoIter;
fn get(&self) -> Self::IntoIter;
}
struct Impl(Vec<u32>);
impl Trait for Impl {
type IntoIter = ImplIntoIter;
fn get(&self) -> Self::IntoIter {
ImplIntoIter(self.0.clone())
}
}
struct ImplIntoIter(Vec<u32>);
impl<'a> IntoIterator for &'a ImplIntoIter {
type Item = <Self::IntoIter as Iterator>::Item;
type IntoIter = std::iter::Cloned<std::slice::Iter<'a, u32>>;
fn into_iter(self) -> Self::IntoIter {
(&self.0).into_iter().cloned()
}
}
fn main() {
}
I saw this happen:
cargo clippy --fix -- -Aclippy::all -Wclippy::cloned_instead_of_copied
Checking clpy v0.1.0 (/tmp/clpy)
warning: failed to automatically apply fixes suggested by rustc to crate `clpy`
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/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[E0308]: mismatched types
--> src/main.rs:26:9
|
25 | fn into_iter(self) -> Self::IntoIter {
| -------------- expected `std::iter::Cloned<std::slice::Iter<'_, u32>>` because of return type
26 | (&self.0).into_iter().copied()
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected struct `std::iter::Cloned`, found struct `std::iter::Copied`
|
= note: expected struct `std::iter::Cloned<std::slice::Iter<'_, u32>>`
found struct `std::iter::Copied<std::slice::Iter<'_, u32>>`
error: aborting due to previous error
For more information about this error, try `rustc --explain E0308`.
Original diagnostics will follow.
warning: used `cloned` where `copied` could be used instead
--> src/main.rs:26:31
|
26 | (&self.0).into_iter().cloned()
| ^^^^^^ help: try: `copied`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#cloned_instead_of_copied
= note: requested on the command line with `-W clippy::cloned-instead-of-copied`
warning: `clpy` (bin "clpy") generated 1 warning (run `cargo fix --bin "clpy"` to apply 1 suggestion)
warning: `clpy` (bin "clpy" test) generated 1 warning (1 duplicate)
Finished dev [unoptimized + debuginfo] target(s) in 0.19s
Version
rustc 1.67.0-nightly (c5d82ed7a 2022-11-19)
binary: rustc
commit-hash: c5d82ed7a4ad94a538bb87e5016e7d5ce0bd434b
commit-date: 2022-11-19
host: x86_64-unknown-linux-gnu
release: 1.67.0-nightly
LLVM version: 15.0.4
Additional Labels
No response
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the cloned_instead_of_copied lint and reproduce the report using the provided Rust example and cargo clippy command. Trace why the suggestion changes an explicitly declared std::iter::Cloned return type to Copied; done means the lint no longer emits an invalid fix for this HRTB case.
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
- 42/100