Inherent method uses a name that belongs to a standard trait

ID

rust.maintainability.should_implement_trait

Severity

low

Remediation Complexity

medium

Remediation Risk

medium

Remediation Effort

medium

Resource

Api Design

Language

Rust

Description

Reports an inherent impl block that defines a method whose name is the method of a standard library trait - add, next, default, from_str, clone, cmp, deref and the like - instead of implementing that trait.

Rationale

The name creates an expectation the type does not meet: callers cannot use +, a for loop, ?, .collect() or any generic function bounded on the trait, because the type does not implement it. Worse, the inherent method takes precedence over the trait method during resolution, so if the trait is implemented later the two silently coexist and which one runs depends on how the call is written.

The following code illustrates the pattern detected by this rule:

use std::fmt;
pub struct Money {
    cents: i64,
}

// FLAGGED: Inherent method uses a name that belongs to a standard trait
impl Money {
    pub fn add(&self, other: &Money) -> Money {
        Money { cents: self.cents + other.cents }
    }

    pub fn cents(&self) -> i64 {
        self.cents
    }
}

Remediation

Implement the trait (impl Add for Money, impl Iterator for Cursor, impl Default for Wrapper<T>) and move the body into it, so operators and generic code work. If the method is deliberately not the trait’s behaviour, rename it to something the trait does not claim.

Configuration

This detector does not need any configuration.

© 2026 Xygeni. All rights reserved