rust-lang / rust-lang/rust

Conflicting unhelpful suggestion related to `Peekable::next_if_eq`

Open
#149,801 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Code
let mut it = vec![].iter().peekable();
it.next_if_eq(&0); // suggests removing `&`
it.next_if_eq(0); // suggests adding `&`
it.next_if_eq(&&0); // actually correct
Current output
error[E0277]: can't compare `&_` with `{integer}`
   --> src/main.rs:3:15
    |
  3 | it.next_if_eq(&0); // suggests removing `&`
    |    ---------- ^^ no implementation for `&_ == {integer}`
    |    |
    |    required by a bound introduced by this call
    |
    = help: the trait `PartialEq<{integer}>` is not implemented for `&_`
note: required by a bound in `Peekable::<I>::next_if_eq`
   --> /playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/iter/adapters/peekable.rs:316:18
    |
313 |     pub fn next_if_eq<T>(&mut self, expected: &T) -> Option<I::Item>
    |            ---------- required by a bound in this associated function
...
316 |         I::Item: PartialEq<T>,
    |                  ^^^^^^^^^^^^ required by this bound in `Peekable::<I>::next_if_eq`
help: consider removing the leading `&`-reference
    |
  3 - it.next_if_eq(&0); // suggests removing `&`
  3 + it.next_if_eq(0); // suggests removing `&`
    |

error[E0308]: mismatched types
   --> src/main.rs:4:15
    |
  4 | it.next_if_eq(0); // suggests adding `&`
    |    ---------- ^ expected `&_`, found integer
    |    |
    |    arguments to this method are incorrect
    |
    = note: expected reference `&_`
                    found type `{integer}`
note: method defined here
   --> /playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/iter/adapters/peekable.rs:313:12
    |
313 |     pub fn next_if_eq<T>(&mut self, expected: &T) -> Option<I::Item>
    |            ^^^^^^^^^^
help: consider borrowing here
    |
  4 | it.next_if_eq(&0); // suggests adding `&`
    |               +
Desired output

Rationale and extra context

The current diagnostic suggests removing the borrow on the first line, however the real solution is to add a second borrow, as the iterator is yielding &{integer}, so one needs to pass a &&{integer} to satisfy the fn args.

Other cases

Rust Version
rustc 1.91.1 (ed61e7d7e 2025-11-07)
binary: rustc
commit-hash: ed61e7d7e242494fb7057f2657300d9e77bb4fcb
commit-date: 2025-11-07
host: x86_64-pc-windows-msvc
release: 1.91.1
LLVM version: 21.1.2
Anything else?

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

Reproduce the diagnostic with the Rust example in the issue, then inspect the Peekable::next_if_eq signature and the compiler diagnostic that handles the two argument-type errors. Trace why the first case recommends removing a borrow, and update the relevant suggestion logic so the output reflects the required additional borrow. Done means the conflicting suggestions are corrected for both examples and covered by an appropriate compiler test.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.