rust-lang / rust-lang/rust-clippy

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

Open
#9,480 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug I-false-positive
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the provided Rust reproducer and inspect the implementation of the unused_peekable lint. Confirm that returning a Peekable through an accessor and consuming it only with next() does not produce a warning, while peek() remains handled correctly. Done means the reproducer matches the expected diagnostics without regressing the lint's warning for genuinely unused peekable() calls.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.