Skip to content

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

@kawadakk

Description

@kawadakk

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 (rust-lang/rust#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

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't haveI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when applied

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions