Skip to content

struct with accessor to a Peekable field can cause unused_peekable to trigger #9480

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

Open
trinity-1686a opened this issue Sep 14, 2022 · 0 comments
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

I'm not sure whether this count as FP, so I'm reporting just in case. If it doesn't, feel free to close the issue

A struct containing a Peekable, and giving an accessor to it causes unused_peekable to be raised if the consumer of the accessor only use it as an iterator, without peeking.
I think this lint should only trigger following a call to .peekable()

Lint Name

unused_peekable

Reproducer

I tried this code:

use std::iter::Peekable;

struct Wrapper<I: Iterator<Item = T>, T> {
    iter: std::iter::Peekable<I>,
}

impl<I: Iterator<Item = T>, T> Wrapper<I, T> {
    fn new(iter: I) -> Self {
        Wrapper {
            iter: iter.peekable(),
        }
    }

    fn iter(
        &mut self,
    ) -> &mut Peekable<impl Iterator<Item = T>> {
        &mut self.iter
    }
}

fn next() -> usize {
    let mut reader = Wrapper::new([1usize,2,3].iter());
    // ko
    let it = reader.iter();
    let tok = it.next().unwrap();
    *tok
}

fn peek() -> usize {
    let mut reader = Wrapper::new([1usize,2,3].iter());
    // ok
    let it = reader.iter();
    let tok = it.peek().unwrap();
    **tok
}

fn main() {
    next();
    peek();
}

I saw this happen:

--> /tmp/testing/src/main.rs:24:9
   |
24 |     let it = reader.iter();
   |         ^^
   |
   = note: `#[warn(clippy::unused_peekable)]` on by default
   = help: consider removing the call to `peekable`
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unused_peekable

warning: 1 warning emitted

I expected to see this happen:
no warn

Version

clippy-driver compiled from 2ddbc86bef837b1072159c020c35940ce52ae696 (after #9465)

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 14, 2022
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

No branches or pull requests

1 participant