Skip to content

incorrect suggestion for while_let_on_iterator #1924

@tspiteri

Description

@tspiteri

For the snippet below, clippy suggests changing while let Some(i) = self.s.next() into for i in self.s { .. }, but that results in a "cannot move out of borrowed content" error. In this case, for i in &mut self.s { .. } would work.

pub struct S {
    i: i32,
}
impl Iterator for S {
    type Item = i32;
    fn next(&mut self) -> Option<i32> {
        if self.i > 0 {
            self.i -= 1;
            Some(self.i)
        } else {
            None
        }
    }
}

pub struct T {
    s: S,
}
impl Iterator for T {
    type Item = i32;
    fn next(&mut self) -> Option<i32> {
        while let Some(i) = self.s.next() {
            if i < 3 || i > 7 {
                return Some(i);
            }
        }
        None
    }
}

#[test]
fn test() {
    let s = S { i: 9 };
    let mut t = T { s };
    assert_eq!(t.next(), Some(8));
    assert_eq!(t.next(), Some(2));
    assert_eq!(t.next(), Some(1));
    assert_eq!(t.next(), Some(0));
    assert_eq!(t.next(), None);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintArea: New lintsE-mediumCall for participation: Medium difficulty level problem and requires some initial experience.S-needs-discussionStatus: Needs further discussion before merging or work can be startedT-middleType: Probably requires verifiying types

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions