about summary refs log tree commit diff
diff options
context:
space:
mode:
authorAlex Macleod <alex@macleod.io>2025-03-23 13:49:21 +0000
committerGitHub <noreply@github.com>2025-03-23 13:49:21 +0000
commitb27a2bbe26b5d4499ca71091e955e7bc7b8b1a4a (patch)
tree84cd01aca895922c0bcdc787f5ed6011e4fbf85d
parente9aed8764e2055eab34564b591a6fbd3d786b718 (diff)
parent63e7815390c420c16e1dde7efa919cc15e15afec (diff)
downloadrust-b27a2bbe26b5d4499ca71091e955e7bc7b8b1a4a.tar.gz
rust-b27a2bbe26b5d4499ca71091e955e7bc7b8b1a4a.zip
Clarify example for unconditional_recursion (#14385)
Clarify example for unconditional_recursion

changelog: none
-rw-r--r--clippy_lints/src/unconditional_recursion.rs28
1 files changed, 25 insertions, 3 deletions
diff --git a/clippy_lints/src/unconditional_recursion.rs b/clippy_lints/src/unconditional_recursion.rs
index 51c7d6fce31..11e938b5e4d 100644
--- a/clippy_lints/src/unconditional_recursion.rs
+++ b/clippy_lints/src/unconditional_recursion.rs
@@ -23,8 +23,8 @@ declare_clippy_lint! {
     /// implementations.
     ///
     /// ### Why is this bad?
-    /// This is a hard to find infinite recursion that will crash any code
-    /// using it.
+    /// Infinite recursion in trait implementation will either cause crashes
+    /// or result in an infinite loop, and it is hard to detect.
     ///
     /// ### Example
     /// ```no_run
@@ -39,9 +39,31 @@ declare_clippy_lint! {
     ///     }
     /// }
     /// ```
+    ///
     /// Use instead:
     ///
-    /// In such cases, either use `#[derive(PartialEq)]` or don't implement it.
+    /// ```no_run
+    /// #[derive(PartialEq)]
+    /// enum Foo {
+    ///     A,
+    ///     B,
+    /// }
+    /// ```
+    ///
+    /// As an alternative, rewrite the logic without recursion:
+    ///
+    /// ```no_run
+    /// 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))
+    ///     }
+    /// }
+    /// ```
     #[clippy::version = "1.77.0"]
     pub UNCONDITIONAL_RECURSION,
     suspicious,