Email me

Clippy lints / Pedantic

Clippy lint · Pedantic · checked with Clippy 0.1.99

clippy::self_only_used_in_recursion: `self` is only used in recursion

What it meansChecks for self receiver that is only used in recursion with no side-effects. It is in the pedantic group and off by default.

What you see

warning: `self` is only used in recursion
 --> main.rs:5:11
  |
5 |     fn f(&self, n: u32) -> u32 {
  |           ^^^^
  |
note: `self` used here
 --> main.rs:9:17
  |
9 |             n * self.f(n - 1)
  |                 ^^^^
  = help: for further information visit https://rust-lang.github.io/rust-clippy/stable/index.html#self_only_used_in_recursion
  = note: `-W clippy::self-only-used-in-recursion` implied by `-W clippy::pedantic`

Real output of Clippy 0.1.99 (Rust 1.99) on the example below. Names and line numbers in yours will differ.

Does it matter?

It may be possible to remove the self argument, allowing the function to be used without an object of type Self.

Stricter lints with some false positives. Off by default; opt in with clippy::pedantic.

How to fix it

Clippy does not fix this one automatically. Change the code by hand:

Flagged

struct Foo;
impl Foo {
    fn f(&self, n: u32) -> u32 {
        if n == 0 {
            1
        } else {
            n * self.f(n - 1)
        }
    }
}

Write this instead

struct Foo;
impl Foo {
    fn f(n: u32) -> u32 {
        if n == 0 {
            1
        } else {
            n * Self::f(n - 1)
        }
    }
}

Turn it off, or on

It is off by default. To use it, set it to warn or deny:

Whole crate (Cargo.toml)

[lints.clippy]
self_only_used_in_recursion = "warn"

In code

#![warn(clippy::self_only_used_in_recursion)]

One run

cargo clippy -- -W clippy::self-only-used-in-recursion

The [lints] table needs Rust 1.74 or newer. In a workspace, put it under [workspace.lints.clippy] and add [lints] workspace = true to each member.

Pedanticgroup
allowdefault level
nofixed by cargo clippy --fix
1.92.0added in Rust

Clippy's own documentation

What it does

Checks for self receiver that is only used in recursion with no side-effects.

Why is this bad?

It may be possible to remove the self argument, allowing the function to be used without an object of type Self.

Example

struct Foo;
impl Foo {
    fn f(&self, n: u32) -> u32 {
        if n == 0 {
            1
        } else {
            n * self.f(n - 1)
        }
    }
}

Use instead:

struct Foo;
impl Foo {
    fn f(n: u32) -> u32 {
        if n == 0 {
            1
        } else {
            n * Self::f(n - 1)
        }
    }
}

Known problems

Too many code paths in the linting code are currently untested and prone to produce false positives or are prone to have performance implications.

In some cases, this would not catch all useless arguments.

struct Foo;
impl Foo {
    fn foo(&self, a: usize) -> usize {
        let f = |x| x;

        if a == 0 {
            1
        } else {
            f(self).foo(a)
        }
    }
}

For example, here self is only used in recursion, but the lint would not catch it.

List of some examples that can not be caught:

  • binary operation of non-primitive types
  • closure usage
  • some break relative operations
  • struct pattern binding

Also, when you recurse the function name with path segments, it is not possible to detect.

From the Clippy lint list (0.1.99), part of the Rust project, MIT or Apache-2.0. Official entry.

Related lints

All 847 Clippy lints · Rust compiler errors · Cargo.toml checker

Up next

Related lintclippy::too_many_linesChecks for functions with a large amount of linesRead it next →

Did this answer your question?

Get the receipts by email.

What my AI agents tried this week, with the real numbers: what worked, what flopped, what made money. One email when there is something worth reading.

No spam, never sold. Every email has a one-click unsubscribe.

Popular in the shop

All 124 tools →

Browse: Bundles and kitsHome, family and schoolMoney and businessAI toolsSports and fitnessMaking and hobbiesFood and drinkIndie dev and launchTrading and quant

Free: Landing page teardown iPhone app cost calculator LLM API cost calculator Paid online: $49 Launch Check App builds Articles FAQ