Skip to content

unconditional_recursion false positive in PartialEq with Deref #12154

Closed
@madsmtm

Description

@madsmtm

Summary

When implementing PartialEq for a custom smart pointer that uses Deref for the implementation, specifically (**self).eq(&**other), the lint triggers.

Lint Name

unconditional_recursion

Reproducer

I tried this code:

struct MyBox<T>(T);

impl<T> std::ops::Deref for MyBox<T> {
    type Target = T;
    fn deref(&self) -> &T {
        &self.0
    }
}

impl<T: PartialEq> PartialEq for MyBox<T> {
    fn eq(&self, other: &Self) -> bool {
        (**self).eq(&**other)
    }
}

I saw this happen:

warning: function cannot return without recursing
  --> src/lib.rs:11:5
   |
11 | /     fn eq(&self, other: &Self) -> bool {
12 | |         (**self).eq(&**other)
13 | |     }
   | |_____^
   |
note: recursive call site
  --> src/lib.rs:12:9
   |
12 |         (**self).eq(&**other)
   |         ^^^^^^^^^^^^^^^^^^^^^
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unconditional_recursion
   = note: `#[warn(clippy::unconditional_recursion)]` on by default

I expected to see this happen:

No warning, the code does not recurse because of the deref in **self.

Version

rustc 1.77.0-nightly (30dfb9e04 2024-01-14)
binary: rustc
commit-hash: 30dfb9e046aeb878db04332c74de76e52fb7db10
commit-date: 2024-01-14
host: aarch64-apple-darwin
release: 1.77.0-nightly
LLVM version: 17.0.6

Additional Labels

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't have

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions