Skip to content

Commit b27a2bb

Browse files
authored
Clarify example for unconditional_recursion (#14385)
Clarify example for unconditional_recursion changelog: none
2 parents e9aed87 + 63e7815 commit b27a2bb

File tree

1 file changed

+25
-3
lines changed

1 file changed

+25
-3
lines changed

clippy_lints/src/unconditional_recursion.rs

+25-3
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ declare_clippy_lint! {
2323
/// implementations.
2424
///
2525
/// ### Why is this bad?
26-
/// This is a hard to find infinite recursion that will crash any code
27-
/// using it.
26+
/// Infinite recursion in trait implementation will either cause crashes
27+
/// or result in an infinite loop, and it is hard to detect.
2828
///
2929
/// ### Example
3030
/// ```no_run
@@ -39,9 +39,31 @@ declare_clippy_lint! {
3939
/// }
4040
/// }
4141
/// ```
42+
///
4243
/// Use instead:
4344
///
44-
/// In such cases, either use `#[derive(PartialEq)]` or don't implement it.
45+
/// ```no_run
46+
/// #[derive(PartialEq)]
47+
/// enum Foo {
48+
/// A,
49+
/// B,
50+
/// }
51+
/// ```
52+
///
53+
/// As an alternative, rewrite the logic without recursion:
54+
///
55+
/// ```no_run
56+
/// enum Foo {
57+
/// A,
58+
/// B,
59+
/// }
60+
///
61+
/// impl PartialEq for Foo {
62+
/// fn eq(&self, other: &Self) -> bool {
63+
/// matches!((self, other), (Foo::A, Foo::A) | (Foo::B, Foo::B))
64+
/// }
65+
/// }
66+
/// ```
4567
#[clippy::version = "1.77.0"]
4668
pub UNCONDITIONAL_RECURSION,
4769
suspicious,

0 commit comments

Comments
 (0)