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

unused-peekable can trigger when peek() is used #9456

Closed
trinity-1686a opened this issue Sep 10, 2022 · 0 comments · Fixed by #9465
Closed

unused-peekable can trigger when peek() is used #9456

trinity-1686a opened this issue Sep 10, 2022 · 0 comments · Fixed by #9465
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

@trinity-1686a
Copy link

Summary

In some cases unused-peekable triggers when having a Peekable<_> is required to satisfy the interface of some other function

Lint Name

unused-peekable

Reproducer

I tried this code:

use std::iter::Peekable;

struct PauseAt<'a, I: Iterator> {
    peek: &'a mut Peekable<I>,
}

impl<'a, I: Iterator> PauseAt<'a, I> {
    fn from_peekable(peek: &'a mut Peekable<I>) -> Self
    {
        PauseAt {
            peek,
        }
    }
    fn peek(&mut self) -> Option<&I::Item> {
        self.peek.peek()
    }
}

fn main() {
    let mut iter = (1..10).into_iter().peekable();

    let mut iter = PauseAt::from_peekable(&mut iter);
    assert_eq!(iter.peek(), Some(&1)); // we can do this multiple times,
    assert_eq!(iter.peek(), Some(&1)); // but count isn't advanced.
}

I saw this happen:

$> cargo +nightly clippy -- -D clippy::unused-peekable
    Checking testing v0.1.0 (/tmp/testing)
error: `peek` never called on `Peekable` iterator
  --> src/main.rs:20:13
   |
20 |     let mut iter = (1..10).into_iter().peekable();
   |             ^^^^
   |
   = note: requested on the command line with `-D clippy::unused-peekable`
   = help: consider removing the call to `peekable`
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unused_peekable

error: could not compile `testing` due to previous error

I expected to see this happen:

No error: having a Peekable is required to satisfy PauseAt::from_peekable interface (and peek() is called, indirectly)

Version

rustc 1.65.0-nightly (1d37ed661 2022-09-09)
binary: rustc
commit-hash: 1d37ed661a6922e7a167609b8cd7eb31e972b19b
commit-date: 2022-09-09
host: x86_64-unknown-linux-gnu
release: 1.65.0-nightly
LLVM version: 15.0.0

Additional Labels

No response

@trinity-1686a trinity-1686a 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 Sep 10, 2022
@bors bors closed this as completed in 9c9aa92 Sep 14, 2022
bors added a commit to rust-lang-ci/rust that referenced this issue Sep 15, 2022
Clippy pre beta branch fix

Before beta is branched on Friday, I want to move the `unused_peekable` lint  that was added in this release cycle (1.65) to `nursery`. This lint was already reported twice (rust-lang/rust-clippy#9456, rust-lang/rust-clippy#9462) in a short time, so it is probably a good idea to fix it before it hits beta and then stable.

r? `@Manishearth`
flip1995 pushed a commit to flip1995/rust-clippy that referenced this issue Sep 22, 2022
Clippy pre beta branch fix

Before beta is branched on Friday, I want to move the `unused_peekable` lint  that was added in this release cycle (1.65) to `nursery`. This lint was already reported twice (rust-lang#9456, rust-lang#9462) in a short time, so it is probably a good idea to fix it before it hits beta and then stable.

r? `@Manishearth`
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.

1 participant