rust-lang / rust-lang/rust-clippy

`op_ref` false positive(?) when comparing `Box<dyn Tr>` where `dyn Tr: PartialEq`

Open
#12,113 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Summary

op_ref triggers when equating values of type {Box,Rc,Arc}<dyn Tr> (where dyn Tr: PartialEq) with extra &s (as in &lhs == &rhs). The extra &s are necessary to avoid a borrowck error, though this is a bug on the rustc side (https://github.com/rust-lang/rust/issues/31740).

Lint Name

op_ref

Reproducer

I tried this code (Playground, minimized from an in-the-wild usage of arrow::array::ArrayRef):

pub trait Tr {}

impl PartialEq for dyn Tr {
    fn eq(&self, _: &Self) -> bool { todo!() }
}

pub fn foo(x: Box<dyn Tr>, y: Box<dyn Tr>) -> Box<dyn Tr> {
    assert!(&x == &y);
    y
}

I saw this happen:

warning: needlessly taken reference of both operands
 --> src/lib.rs:8:13
  |
8 |     assert!(&x == &y);
  |             ^^^^^^^^
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#op_ref
  = note: `#[warn(clippy::op_ref)]` on by default
help: use the values directly
  |
8 |     assert!(x == y);
  |             ~    ~

I expected to see this happen:
No warnings, because removing & as per suggestion would cause a compile error:

error[E0382]: use of moved value: `y`
 --> src/lib.rs:9:5
  |
7 | pub fn foo(x: Box<dyn Tr>, y: Box<dyn Tr>) -> Box<dyn Tr> {
  |                            - move occurs because `y` has type `std::boxed::Box<dyn Tr>`, which does not implement the `Copy` trait
8 |     assert!(x == y);
  |                  - value moved here
9 |     y
  |     ^ value used here after move

For more information about this error, try `rustc --explain E0382`.
Version
rustc 1.77.0-nightly (ca663b06c 2024-01-08)
binary: rustc
commit-hash: ca663b06c5492ac2dde5e53cd11579fa8e4d68bd
commit-date: 2024-01-08
host: x86_64-unknown-linux-gnu
release: 1.77.0-nightly
LLVM version: 17.0.6
Additional Labels

@rustbot label +I-suggestion-causes-error

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 by reproducing the op_ref warning with the code in src/lib.rs and the linked Rust Playground. Trace the op_ref lint implementation to understand why references around Box are suggested for removal. Done means the valid borrowed comparison no longer produces a misleading warning or an error-causing suggestion, with coverage for the reproducer.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
devtools
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.