rust-lang / rust-lang/rust-clippy

`wrong_self_invocation` violated for non-`self` `Self` types

Open
#10,210 3 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

The wrong_self_convention lint is violated when a to_* function takes a parameter of type &Self when that parameter isn't a &self receiver or when an into_* function takes a parameter of type Self when that parameter isn't a self receiver.

The lint page references the API guidelines for this lint, however those API guidelines only say that the parameter must be borrowed or owned etc., not that it must be in the form &self or self.

Lint Name

wrong_self_invocation

Reproducer

I tried this code (I cannot vouch for its quality, but nonetheless):

pub fn to_hex(Self(red, green, blue): &Self) -> u32 {
    let (red, green, blue) = (
        u32::from(red >> 8),
        u32::from(green >> 8),
        u32::from(blue >> 8),
    );
    
    (red << 16) | (green << 8) | blue
}

and the lint was violated:

methods called `to_*` usually take self by reference or self by value; consider choosing a less ambiguous name

but I wouldn't have expected that the lint would be violated, considering it takes a borrowed Self type. One can certainly argue that the parameter should be written as &self instead, but that is not the point of this lint.

Version
rustc 1.68.0-nightly (bdb07a8ec 2022-12-11)
binary: rustc
commit-hash: bdb07a8ec8e77aa10fb84fae1d4ff71c21180bb4
commit-date: 2022-12-11
host: x86_64-unknown-linux-gnu
release: 1.68.0-nightly
LLVM version: 15.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 at the wrong_self_invocation lint entry point and reproduce the reported behavior with the Rust snippet in the issue. Trace how parameter types are classified, then add or update a regression test for non-self Self parameters. Done means the lint no longer reports the described valid borrowed or owned Self parameter, with the intended behavior covered by tests.

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
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.