rust-lang / rust-lang/rust

`unconditional_recursion` lint false positive on abort-on-drop type.

Open
#138,897 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics L-unconditional_recursion T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the supplied DropBomb example on the linked Rust Playground and inspect the implementation and tests for the unconditional_recursion lint. Determine how abort-on-drop behavior should be recognized without suggesting loop replacement, then add coverage showing the example compiles without a warning while ordinary unconditional recursion remains diagnosed.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.