Closed
Description
Iterators shouldn't implement copy but the lint suggests it anyway.
Given the following code:
#![warn(missing_copy_implementations)]
pub struct MyIterator {
num: u8,
}
impl Iterator for MyIterator {
type Item = u8;
fn next(&mut self) -> Option<Self::Item> {
if self.num > 42 {
None
} else {
self.num += 1;
Some(self.num)
}
}
}
fn main() {
let iter = MyIterator { num: 0 };
for i in iter {
println!("{}", i);
}
}
The current output is:
warning: type could implement `Copy`; consider adding `impl Copy`
--> src/main.rs:3:1
|
3 | / pub struct MyIterator {
4 | | num: u8,
5 | | }
| |_^
|
note: the lint level is defined here
--> src/main.rs:1:9
|
1 | #![warn(missing_copy_implementations)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Ideally there should be no warning.
It is widely accepted that iterators shouldn't implement Copy
because of it being prone to accidentally modifying a copy instead of original (by &mut
). However this lint suggests to implement Copy
anyway. In case the intention is "give the user the warning anyway because he asked for it" the message should probably mention this edge-case and suggest allow
instead.