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

iter_without_into_iter suggests doubly incorrect IntoIter type #14004

Open
samueltardieu opened this issue Jan 15, 2025 · 0 comments
Open

iter_without_into_iter suggests doubly incorrect IntoIter type #14004

samueltardieu opened this issue Jan 15, 2025 · 0 comments
Labels
C-bug Category: Clippy is not doing the correct thing I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied

Comments

@samueltardieu
Copy link
Contributor

Summary

When suggesting an implementation of IntoIterator, iter_without_into_iter suggests using a type for IntoIter which has two problems:

  • If the type has been defined in the current crate, the path given is from the top-level of the create, without prefixing it with crate::, making it invalid if we happen to be in a module.
  • If the type contains lifetimes, they will be copied verbatim even if they make no sense at the place of the suggestion. I think that in this case the lint should not trigger.

Reproducer

I tried this code:

#![warn(clippy::iter_without_into_iter)]
#![allow(unused)]

pub mod inner {
    pub struct S;
    impl S {
        pub fn iter(&self) -> I {
            todo!()
        }
    }

    pub struct I<'a> {
        field: &'a ()
    }
    impl Iterator for I<'_> {
        type Item = ();
        fn next(&mut self) -> Option<Self::Item> {
            todo!()
        }
    }
}

fn main() {}

The suggestion is:

help: consider implementing `IntoIterator` for `&S`
  |
6 ~     
7 + impl IntoIterator for &S {
8 +     type Item = ();
9 +     type IntoIter = inner::I<'_>;
10+     fn into_iter(self) -> Self::IntoIter {
11+         self.iter()
12+     }
13+ }

inner::I<'_> is invalid, because of the lifetime, and because it should be either I or crate::inner::I.

Version

rustc 1.86.0-nightly (8361aef0d 2025-01-14)
binary: rustc
commit-hash: 8361aef0d7c29b1501a316a208ed84cd8a2ae5da
commit-date: 2025-01-14
host: x86_64-unknown-linux-gnu
release: 1.86.0-nightly
LLVM version: 19.1.6

Additional Labels

@rustbot label +I-suggestion-causes-error

@samueltardieu samueltardieu added the C-bug Category: Clippy is not doing the correct thing label Jan 15, 2025
@rustbot rustbot added the I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied label Jan 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied
Projects
None yet
Development

No branches or pull requests

2 participants