Skip to content

False positive explicit_into_iter_loop on an into_iter method with type parameter #6900

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

Closed
dtolnay opened this issue Mar 14, 2021 · 1 comment · Fixed by #6982
Closed

False positive explicit_into_iter_loop on an into_iter method with type parameter #6900

dtolnay opened this issue Mar 14, 2021 · 1 comment · Fixed by #6982
Assignees
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have

Comments

@dtolnay
Copy link
Member

dtolnay commented Mar 14, 2021

As of most recent nightly:

#![deny(clippy::explicit_into_iter_loop)]

use std::marker::PhantomData;

pub struct S;

impl S {
    #[allow(clippy::should_implement_trait)]
    pub fn into_iter<T>(self) -> I<T> {
        let _ = self;
        I { _marker: PhantomData }
    }
}

pub struct I<T> {
    _marker: PhantomData<T>,
}

impl<T> Iterator for I<T> {
    type Item = T;
    fn next(&mut self) -> Option<Self::Item> {
        None
    }
}

fn main() {
    for t in S.into_iter::<i32>() {
        println!("{}", t);
    }
}
error: it is more concise to loop over containers instead of using explicit iteration methods
  --> src/main.rs:27:14
   |
27 |     for t in S.into_iter::<i32>() {
   |              ^^^^^^^^^^^^^^^^^^^^ help: to write this more concisely, try: `S`
   |
note: the lint level is defined here
  --> src/main.rs:1:9
   |
1  | #![deny(clippy::explicit_into_iter_loop)]
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#explicit_into_iter_loop

Clippy wants to see this rewritten as:

-     for t in S.into_iter::<i32>() {
+     for t in S {

However, that replacement code does not compile. Therefore I believe the clippy::explicit_into_iter_loop lint should be fixed to not trigger on this code.

error[E0277]: `S` is not an iterator
  --> src/main.rs:27:14
   |
27 |     for t in S {
   |              ^ `S` is not an iterator
   |
   = help: the trait `std::iter::Iterator` is not implemented for `S`
   = note: required because of the requirements on the impl of `std::iter::IntoIterator` for `S`
   = note: required by `std::iter::IntoIterator::into_iter`

Meta

  • cargo clippy -V: e.g. clippy 0.1.52 (acca818 2021-03-13)
  • rustc -Vv:
    rustc 1.52.0-nightly (acca81892 2021-03-13)
    binary: rustc
    commit-hash: acca818928654807ed3bc1ce0e97df118f8716c8
    commit-date: 2021-03-13
    host: x86_64-unknown-linux-gnu
    release: 1.52.0-nightly
    LLVM version: 12.0.0
    
@dtolnay dtolnay added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels Mar 14, 2021
@Jarcho
Copy link
Contributor

Jarcho commented Mar 27, 2021

@rustbot claim

bors added a commit that referenced this issue Apr 1, 2021
Fix `explicit_into_iter_loop`

fixes: #6900

changelog: Only lint when `into_iter` is an implementation of `IntoIterator`
@bors bors closed this as completed in c3ef585 Apr 12, 2021
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-false-positive Issue: The lint was triggered on code it shouldn't have
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants