Open
Description
Code
struct DropBomb;
impl Drop for DropBomb {
fn drop(&mut self) {
let _this = DropBomb;
panic!()
}
}
Current output
warning: function cannot return without recursing
--> src/lib.rs:5:5
|
5 | fn drop(&mut self) {
| ^^^^^^^^^^^^^^^^^^ cannot return without recursing
...
8 | }
| - recursive call site
|
= help: a `loop` may express intention better if this is on purpose
= note: `#[warn(unconditional_recursion)]` on by default
Desired output
No output; compiles as intended.
Rationale and extra context
The lint is semantically incorrect, in that the recursion occurs at most once (after that, the entire program is aborted due to double-panic). The help message is also not useful, because replacing the panic!()
with a loop {}
changes the program from aborting when dropping DropBomb
, to hanging indefinitely.
This is a common construction in no_std
for an AbortOnDrop
type, usually to stop a program from unwinding out of a piece of unsafe code and leaving broken invariants.
Other cases
Rust Version
rustc 1.87.0-nightly (aa8f0fd71 2025-03-23)
binary: rustc
commit-hash: aa8f0fd7163a2f23aa958faed30c9c2b77b934a5
commit-date: 2025-03-23
host: x86_64-unknown-linux-gnu
release: 1.87.0-nightly
LLVM version: 20.1.1
Anything else?
Playground Link: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2024&gist=8dbe0e6fb92d8f66787d7c3f14c386a5