Closed
Description
Summary
Using nightly, cargo clippy
incorrectly reports an unconditional_recursion
warning.
Lint Name
unconditional_recursion
Reproducer
I tried this code:
#[derive(PartialEq)]
pub struct Foo;
impl PartialEq<&Self> for Foo {
#[deny(unconditional_recursion)]
fn eq(&self, other: &&Self) -> bool {
// This is _not_ recursive since this calls the derived `PartialEq` implementation.
*self == **other
}
}
// Uncommenting below will lead to a compilation failure since the compiler correctly detects
// an actual recursion.
//impl PartialEq<Foo> for &Foo {
// #[deny(unconditional_recursion)]
// fn eq(&self, other: &Foo) -> bool {
// *self == *other
// }
//}
#[cfg(test)]
mod tests {
use super::Foo;
#[test]
fn not_recursive() {
let x = Foo;
let y = &x;
// If the call were recursive, this won't ever finish but it does.
assert!(x == y);
// Same as above, but more obviously illustrates it's relying on the
// "recursive" implementation.
assert!(<Foo as PartialEq<&Foo>>::eq(&x, &y));
}
}
I saw this happen:
[zack@laptop src]$ cargo clippy
Checking bar v0.1.0 (/home/zack/projects/bar)
warning: function cannot return without recursing
--> src/lib.rs:5:5
|
5 | / fn eq(&self, other: &&Self) -> bool {
6 | | // This is _not_ recursive since this calls the derived `PartialEq` implementation.
7 | | *self == **other
8 | | }
| |_____^
|
note: recursive call site
--> src/lib.rs:7:9
|
7 | *self == **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
warning: `bar` (lib) generated 1 warning
Finished dev [unoptimized + debuginfo] target(s) in 0.06s
[zack@laptop src]$ cargo t
Finished test [unoptimized + debuginfo] target(s) in 0.03s
Running unittests src/lib.rs (/home/zack/projects/bar/target/debug/deps/bar-5f4c34b6090a377c)
running 1 test
test tests::not_recursive ... ok
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
Doc-tests bar
running 0 tests
test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
I expected to see this happen:
no warnings
Version
[zack@laptop src]$ rustc -Vv
rustc 1.78.0-nightly (8ace7ea1f 2024-02-07)
binary: rustc
commit-hash: 8ace7ea1f7cbba7b4f031e66c54ca237a0d65de6
commit-date: 2024-02-07
host: x86_64-unknown-linux-gnu
release: 1.78.0-nightly
LLVM version: 17.0.6
[zack@laptop src]$ uname -a
Linux laptop 6.7.4-arch1-1 #1 SMP PREEMPT_DYNAMIC Mon, 05 Feb 2024 22:07:49 +0000 x86_64 GNU/Linux
Additional Labels
No response