Skip to content

Lint against Self as an arbitrary self type #5861

@Luro02

Description

@Luro02

What it does

Suggest replacing self: Self with self, because as far as I know self is the same as self: Self.

Categories (optional)

  • Kind: clippy::style

What is the advantage of the recommended code over the original code?

  • reduces the amount of code
  • improves the readability of the code

Drawbacks

None.

Example

#![forbid(unsafe_code)]
#![warn(rust_2018_idioms)]
#![warn(clippy::all, clippy::pedantic, clippy::nursery)]

pub enum ValType {
    I32,
    I64,
    F32,
    F64,
}

impl ValType {
    #[must_use]
    pub fn bytes(self: Self) -> usize {
        match self {
            Self::I32 | Self::F32 => 4,
            Self::I64 | Self::F64 => 8,
        }
    }
}

fn main() {
    assert_eq!(ValType::I32.bytes(), 4);
    assert_eq!(ValType::bytes(ValType::I32), 4);
}

Could be written as:

#![forbid(unsafe_code)]
#![warn(rust_2018_idioms)]
#![warn(clippy::all, clippy::pedantic, clippy::nursery)]

pub enum ValType {
    I32,
    I64,
    F32,
    F64,
}

impl ValType {
    #[must_use]
    pub fn bytes(self) -> usize {
        match self {
            Self::I32 | Self::F32 => 4,
            Self::I64 | Self::F64 => 8,
        }
    }
}

fn main() {
    assert_eq!(ValType::I32.bytes(), 4);
    assert_eq!(ValType::bytes(ValType::I32), 4);
}

Relevant issue for the feature: rust-lang/rust#44874

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintArea: New lintsL-complexityLint: Belongs in the complexity lint groupgood first issueThese issues are a good way to get started with Clippy

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions