Skip to content

new lint: method_without_self_relation - #15913

Open
andrewgazelka wants to merge 4 commits into
rust-lang:masterfrom
andrewgazelka:method_without_self_relation
Open

new lint: method_without_self_relation#15913
andrewgazelka wants to merge 4 commits into
rust-lang:masterfrom
andrewgazelka:method_without_self_relation

Conversation

@andrewgazelka

Copy link
Copy Markdown

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

@rustbot rustbot added the needs-fcp PRs that add, remove, or rename lints and need an FCP label Oct 20, 2025
@andrewgazelka
andrewgazelka force-pushed the method_without_self_relation branch 3 times, most recently from 23a34a6 to fb5ddaf Compare October 20, 2025 03:28
@github-actions

github-actions Bot commented Oct 20, 2025

Copy link
Copy Markdown

Lintcheck changes for 17410f9

Lint Added Removed Changed
clippy::method_without_self_relation 354 0 0

This comment will be updated if you push new changes

@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

rustbot commented Oct 20, 2025

Copy link
Copy Markdown
Collaborator

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

y21 commented Oct 20, 2025

Copy link
Copy Markdown
Member

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

@andrewgazelka

Copy link
Copy Markdown
Author

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

38587b0

@andrewgazelka

Copy link
Copy Markdown
Author

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

any updates?

@andrewgazelka

andrewgazelka commented Dec 12, 2025

Copy link
Copy Markdown
Author

Hey @y21 👋

Just wanted to clarify - the trait impl false positive you flagged was fixed in commit 38587b0 ("do not trigger for trait impls"). The lint now explicitly skips trait implementations:

// Don't lint trait implementations - these methods are defined by the trait
if implements_trait {
    return;
}

The lintcheck was re-run after this fix and shows 354 warnings on real-world crates, none of which are from trait impls anymore.

Just rebased onto latest master. Let me know if there's anything else needed for review!

- 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 force-pushed the method_without_self_relation branch from 38587b0 to 17410f9 Compare December 12, 2025 19:57
@rustbot

rustbot commented Dec 12, 2025

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@andrewgazelka

Copy link
Copy Markdown
Author

@rustbot ready

@rustbot

rustbot commented Mar 5, 2026

Copy link
Copy Markdown
Collaborator

☔ The latest upstream changes (possibly #15979) made this pull request unmergeable. Please resolve the merge conflicts.

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

Labels

needs-fcp PRs that add, remove, or rename lints and need an 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