rust-lang / rust-lang/rust-clippy

op_ref false positive

Open
#12,463 0 comments 1 reaction 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

playground

#[derive(Clone, Copy)]
struct Matrix {}

trait Trait {}

impl<T> std::ops::Mul<T> for &Matrix
where
    T: Trait,
{
    type Output = T;
    fn mul(self, _: T) -> Self::Output {
        todo!()
    }
}

impl<T> std::ops::Mul<T> for Matrix
where
    T: Trait,
{
    type Output = T;
    fn mul(self, rhs: T) -> Self::Output {
        &self * rhs
    }
}

Gives this warning:

warning: needlessly taken reference of left operand
  --> src/lib.rs:22:9
   |
22 |         &self * rhs
   |         -----^^^^^^
   |         |
   |         help: use the left value directly: `self`
   |
   = 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

Clippy incorrectly complains about adding reference here. It suggests removing '&', but that obviously leads to infinitely recursive multiplication implementation. The reference is needed because the actual multiplication is implemented for borrowed type.

Removing Copy from Matrix solves it: playground
Implementing Mul trait on a type directly fixes issue: playground

Lint Name

op_ref

Reproducer

I tried this code:

#[derive(Clone, Copy)]
struct Matrix {}

trait Trait {}

impl<T> std::ops::Mul<T> for &Matrix
where
    T: Trait,
{
    type Output = T;
    fn mul(self, _: T) -> Self::Output {
        todo!()
    }
}

impl<T> std::ops::Mul<T> for Matrix
where
    T: Trait,
{
    type Output = T;
    fn mul(self, rhs: T) -> Self::Output {
        &self * rhs
    }
}

I saw this happen:

warning: needlessly taken reference of left operand
  --> src/lib.rs:22:9
   |
22 |         &self * rhs
   |         -----^^^^^^
   |         |
   |         help: use the left value directly: `self`
   |
   = 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

I expected to see this happen:

Version
rustc 1.76.0 (07dca489a 2024-02-04)
binary: rustc
commit-hash: 07dca489ac2d933c78d3c5158e3f43beefeb02ce
commit-date: 2024-02-04
host: x86_64-unknown-linux-gnu
release: 1.76.0
LLVM version: 17.0.6
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 op_ref lint implementation and its existing regression tests, using the provided Rust reproducer as the failing case. Confirm that the lint distinguishes the borrowed multiplication implementation from a removable reference, then add coverage so the reproducer produces no warning.

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
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.