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

while_let_on_iterator suggestion doesn't compile #7510

Closed
jyn514 opened this issue Jul 29, 2021 · 2 comments · Fixed by #7520
Closed

while_let_on_iterator suggestion doesn't compile #7510

jyn514 opened this issue Jul 29, 2021 · 2 comments · Fixed by #7520
Labels
C-bug Category: Clippy is not doing the correct thing

Comments

@jyn514
Copy link
Member

jyn514 commented Jul 29, 2021

I tried this code:

impl<T: Stackable> Drop for IntoIter<T> {
    fn drop(&mut self) {
        unsafe {
            while let Some(_) = self.next() {}
            OPENSSL_sk_free(self.stack as *mut _);
        }
    }
}

I expected to see this happen: Clippy suggests code that compiles:

diff --git a/boring/src/stack.rs b/boring/src/stack.rs
index 043ee178..70f366f6 100644
--- a/boring/src/stack.rs
+++ b/boring/src/stack.rs
@@ -131,7 +131,7 @@ pub struct IntoIter<T: Stackable> {
 impl<T: Stackable> Drop for IntoIter<T> {
     fn drop(&mut self) {
         unsafe {
-            while let Some(_) = self.next() {}
+            for _ in &mut *self {}
             OPENSSL_sk_free(self.stack as *mut _);
         }
     }

Instead, this happened: Clippy's suggestion doesn't compile.

warning: this loop could be written as a `for` loop
   --> boring/src/stack.rs:134:13
    |
134 |             while let Some(_) = self.next() {}
    |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `for _ in &mut self`
    |
    = note: `#[warn(clippy::while_let_on_iterator)]` on by default
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#while_let_on_iterator
error[E0596]: cannot borrow `self` as mutable, as it is not declared as mutable
   --> boring/src/stack.rs:134:22
    |
134 |             for _ in &mut self {}
    |                      ^^^^^^^^^
    |                      |
    |                      cannot borrow as mutable
    |                      try removing `&mut` here

(for _ in self {} doesn't compile either because it moves self, and it's used again afterwards.)

Meta

  • cargo clippy -V: clippy 0.1.54 (a178d03 2021-07-26)
  • rustc -Vv:

rustc 1.54.0 (a178d0322 2021-07-26)
binary: rustc
commit-hash: a178d0322ce20e33eac124758e837cbd80a6f633
commit-date: 2021-07-26
host: x86_64-unknown-linux-gnu
release: 1.54.0
LLVM version: 12.0.1

@jyn514 jyn514 added the C-bug Category: Clippy is not doing the correct thing label Jul 29, 2021
@SamRodri
Copy link

SamRodri commented Jul 29, 2021

I got the same false positive in a tree iterator code, reduced:

pub struct Foo<I> {
    iter: I
}
impl<I: Iterator<Item=u8>> Foo<I> {
    pub fn test(&mut self) {
        while let Some(_) = self.iter.next() {
            for _ in &mut self.iter {
            }
        }
    }

    // pub fn incorrect_suggestion(&mut self) {
    //     for _ in &mut self.iter {
    //         for _ in &mut self.iter {
    //         }
    //     }
    // }
}

I don’t think there is a correct way to use a for loop in this case.

@pickfire
Copy link
Contributor

pickfire commented Jul 30, 2021

Same issue here https://github.com/helix-editor/helix/pull/525/files#diff-0dd7da00658aae6b00b285feac8d5a8a9f41717a1000be65fac7fa2874d322f7R248, we just had #[allow(clippy::while_let_on_iterator)].

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
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants