Skip to content

maybe_drop_guard missing CoroutineClosure, false unused variable warning for async closure drop guards #479

@SebTardif

Description

@SebTardif

Bug

maybe_drop_guard in compiler/rustc_mir_transform/src/liveness.rs (line 238) determines whether a type should be treated as a "drop guard" to suppress false unused variable lint warnings. It explicitly lists ty::Closure and ty::Coroutine but misses ty::CoroutineClosure.

Evidence

matches!(
    ty.kind(),
    ty::Closure(..)
        | ty::Coroutine(..)
        | ty::Tuple(..)
        | ty::Adt(..)
        | ty::Dynamic(..)
        | ty::Array(..)
        | ty::Slice(..)
        | ty::Alias(ty::AliasTy { kind: ty::Opaque { .. }, .. })
) && ty.needs_drop(tcx, typing_env)

ty::CoroutineClosure is not in the list. When an async closure is used as a drop guard (e.g., let _guard = create_async_closure();), the lint pass will not recognize it as a drop guard.

Impact

False positive unused variable warning on stable Rust (async closures stable since 1.85) when async closures are used as drop guards.

PR #480

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsCompiler diagnosticsI-wrongWrong result or data corruptionP-mediumMedium impact: affects specific usage patternsbugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions