Skip to content

Conversation

andrewgazelka
Copy link

Adds a new restriction lint that detects methods in impl blocks that have no relationship to Self, neither through parameters, return types, nor receiver. These are better expressed as standalone functions.

Motivation

Methods that don't reference Self anywhere in their signature are not truly "methods" but rather associated functions that happen to be namespaced under a type. Moving them to standalone functions can improve code modularity and discoverability.

Implementation Details

  • Category: restriction - intentionally conservative since there are valid reasons to keep functions in impl blocks (helpers, private implementation details, namespace organization)
  • Detection: Recursively checks function signatures for Self references in: receiver and return types

Examples

Triggers lint:

impl Calculator {
    fn add(a: i32, b: i32) -> i32 {  // No Self relation
        a + b
    }
}

Does not trigger:

impl Calculator {
    // Direct Self references
    fn new(precision: u32) -> Self { ... }  // Returns Self
    fn from_self(other: Self) -> u32 { ... }  // Takes Self as parameter

    // Self in generics (symmetric - parameters and return types)
    fn from_option(opt: Option<Self>) -> u32 { ... }  // Self in parameter generic
    fn maybe_new(precision: u32) -> Option<Self> { ... }  // Self in return type generic
    fn try_new(precision: u32) -> Result<Self, String> { ... }  // Self in Result
    fn tuple_with_self(x: i32) -> (i32, Self) { ... }  // Self in tuple return

    // Receivers
    fn get_precision(&self) -> u32 { ... }  // Has &self receiver
    fn consume(self) -> u32 { ... }  // Has self receiver
}

changelog: [method_without_self_relation]: new restriction lint to detect methods with no relationship to Self

@andrewgazelka andrewgazelka force-pushed the method_without_self_relation branch 2 times, most recently from 0c6cadf to 23a34a6 Compare October 20, 2025 03:27
@andrewgazelka andrewgazelka force-pushed the method_without_self_relation branch from 23a34a6 to fb5ddaf Compare October 20, 2025 03:28
Copy link

github-actions bot commented Oct 20, 2025

Lintcheck changes for b9739fd

Lint Added Removed Changed
clippy::method_without_self_relation 1132 0 0

This comment will be updated if you push new changes

- Merge identical match arms for Ref and RawPtr types
- Remove unused cx parameter from contains_self function
- Remove unnecessary .iter() call in loop over method inputs
@andrewgazelka andrewgazelka marked this pull request as ready for review October 20, 2025 05:13
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Oct 20, 2025
@rustbot
Copy link
Collaborator

rustbot commented Oct 20, 2025

r? @y21

rustbot has assigned @y21.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@y21
Copy link
Member

y21 commented Oct 20, 2025

It looks like this triggers for methods in trait impls - that seems like a FP? E.g. there's a lintcheck warning on https://docs.rs/ahash/0.8.11/src/ahash/specialize.rs.html#95-100

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-fcp S-waiting-on-review Status: Awaiting review from the assignee but also interested parties

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants