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

Suggest implementing IntoIterator for &'_ T where T has iter() method returning an iterator (or vice versa) #9736

Closed
Kixunil opened this issue Oct 28, 2022 · 2 comments · Fixed by #11527 or #11587
Assignees
Labels
A-lint Area: New lints

Comments

@Kixunil
Copy link

Kixunil commented Oct 28, 2022

What it does

If the trait implementation is missing it suggests adding it by forwarding to the method (can be automated).
If the trait implementation is present but the inherent method is not suggest adding it forwarding to trait implementation.
Same for mutable versions.

Both of these APIs are idiomatic.

Probably should be only triggered for public T.

Lint Name

impl_iter_into_iterator

Category

style

Advantage

  • The API is idiomatic and intuitive because all types in std and many other crates behave the same
  • It's possible to pass &T to for or functions expecting IntoIterator avoiding typing .iter()

Drawbacks

  • Increases boilerplate in the library

Example

pub struct Foo([u8; 32]);

impl Foo {
    fn iter(&self) -> Iter<'_> { // Iter implements Iterator<Item=&u8>
        Iter::new(&self.0)
    }
}

Could be written as:

pub struct Foo([u8; 32]);

impl Foo {
    fn iter(&self) -> Iter<'_> { // Iter implements Iterator<Item=&u8>
        Iter::new(&self.0)
    }
}

impl<'a> IntoIterator for &'a Foo {
    type IntoIter = Iter<'a>;
    type Item = &'a u8;

    fn into_iter(self) -> Self::IntoIter {
        self.iter()
    }
}
@Kixunil Kixunil added the A-lint Area: New lints label Oct 28, 2022
@llogiq llogiq changed the title Suggest implementing IntoIterator for &'_ T where T has inter() method returning an iterator (or vice versa) Suggest implementing IntoIterator for &'_ T where T has iter() method returning an iterator (or vice versa) Oct 29, 2022
@bors bors closed this as completed in d38fa1a Sep 29, 2023
@Jarcho
Copy link
Contributor

Jarcho commented Sep 29, 2023

Implementing IntoIterator for &T has been implemented. The reverse lint has not been.

@Jarcho Jarcho reopened this Sep 29, 2023
@y21
Copy link
Member

y21 commented Sep 29, 2023

@rustbot claim

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints
Projects
None yet
3 participants