RS.CLIPPY.UNCONDITIONAL_RECURSION

Detect unconditional recursion in some traits implementation

This checker is a Clippy lint created by The Rust Project Contributors. The documentation shown here is a copy of the original documentation for: unconditional_recursion. Copyright ©2025 The Rust Team. All rights reserved.

What it does

Checks that there isn't an infinite recursion in trait implementations.

Why is this bad?

Infinite recursion in trait implementation will either cause crashes or result in an infinite loop, and it is hard to detect.

Example

enum Foo {
    A,
    B,
}

impl PartialEq for Foo {
    fn eq(&self, other: &Self) -> bool {
        self == other // bad!
    }
}

Use instead:

#[derive(PartialEq)]
enum Foo {
    A,
    B,
}

As an alternative, rewrite the logic without recursion:

enum Foo {
    A,
    B,
}

impl PartialEq for Foo {
    fn eq(&self, other: &Self) -> bool {
        matches!((self, other), (Foo::A, Foo::A) | (Foo::B, Foo::B))
    }
}