Skip to content

unconditional_recursion lint false positive on abort-on-drop type. #138897

Open
@chorman0773

Description

@chorman0773

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsL-unconditional_recursionLint: unconditional_recursionT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions