Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unconditional_recursion false positive in PartialEq with Deref #12154

Closed
madsmtm opened this issue Jan 16, 2024 · 1 comment · Fixed by #12177
Closed

unconditional_recursion false positive in PartialEq with Deref #12154

madsmtm opened this issue Jan 16, 2024 · 1 comment · Fixed by #12177
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have

Comments

@madsmtm
Copy link

madsmtm commented Jan 16, 2024

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

@madsmtm madsmtm added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels Jan 16, 2024
@joshlf
Copy link

joshlf commented Jan 17, 2024

I believe this bug is also causing this CI failure in this zerocopy PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants