Skip to content

False positive wrong_self_convention inside trait impls #6983

Closed
@dtolnay

Description

@dtolnay

Beginning with the most recent nightly (clippy 0.1.53 5e65467 2021-03-26, rustc 1.53.0-nightly 5e65467 2021-03-26) the following code incorrectly triggers wrong_self_convention. The trait method signature is controlled by the trait; it doesn't matter that the impl is for a type implementing Copy, that doesn't mean that the trait can or should take self by value. In particular there are likely other impls which are not Copy.

pub struct Thing;

pub trait Trait {
    fn to_thing(&self) -> Thing;
}

impl Trait for u8 {
    fn to_thing(&self) -> Thing {
        Thing
    }
}
warning: methods with the following characteristics: (`to_*` and `self` type is `Copy`) usually take `self` by value
 --> src/main.rs:8:17
  |
8 |     fn to_thing(&self) -> Thing {
  |                 ^^^^^
  |
  = note: `#[warn(clippy::wrong_self_convention)]` on by default
  = help: consider choosing a less ambiguous name
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#wrong_self_convention

Mentioning @mgacek8 @llogiq who touched this lint recently in #6924.

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 have

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions