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-recursionThe [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.
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
breakrelative 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
- too_many_linesChecks for functions with a large amount of lines
- missing_errors_docChecks the doc comments of publicly visible functions that return a Result type and warns if there is no # Errors section
- must_use_candidateChecks for public functions that have no #[must_use] attribute, but return something not already marked must-use, have no mutable arg and mutate no statics
- return_self_not_must_usemissing `#[must_use]` attribute on a method returning `Self`
- same_functions_in_if_conditionChecks for consecutive ifs with the same function call
- same_length_and_capacityChecks for usages of Vec::from_raw_parts and String::from_raw_parts where the same expression is used for the length and the capacity
- semicolon_if_nothing_returnedconsider adding a `;` to the last statement for consistent formatting
- should_panic_without_expectChecks for #[should_panic] attributes without specifying the expected panic message
- similar_namesbinding's name is too similar to existing binding
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?