Skip to content

missing_copy_implementations probably shouldn't fire if the type implements Iterator #98348

Closed
@Kixunil

Description

@Kixunil

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions