Skip to content

Lint request: drop_flags #7025

@SoniEx2

Description

@SoniEx2

What it does

Detects usage of drop flags.

Categories (optional)

  • Kind: Restriction

Drop flags come with a hidden runtime cost, and can sometimes be an oversight.

Drawbacks

None.

Example

let x;
{
  x = RequiresDrop;
  x.do_something();
}

Could be written as:

{
  let x = RequiresDrop;
  x.do_something();
}

This is a restriction lint because there are some cases where drop flags seemingly make sense, such as a lazy or optional lock:

let _x; // _x is a LockGuard
if let Some(lock) = self.lock {
  _x = lock.lock();
}
// protected code here
// drop flags take care of _x here

altho we'd argue this code is confusing and an explicit drop flag would be better:

let _x = self.lock.by_ref().map(Lock::lock); // _x is an Option<LockGuard> i.e. it has a drop flag attached to it in the form of an Option.
// protected code here
// Option takes care of _x, not drop flags

Activity

kvverti

kvverti commented on Apr 3, 2021

@kvverti

Your first example does not require runtime conditional drop, since r#use takes ownership of x. The second is a valid demonstration of conditional drop.

I would also suggest this be called conditional_drop, so as not to allude to any particular implementation.

added
E-hardCall for participation: This a hard problem and requires more experience or effort to work on
L-restrictionLint: Belongs in the restriction lint group
on May 3, 2021
SoniEx2

SoniEx2 commented on May 8, 2021

@SoniEx2
Author

maybe it should be called hidden_runtime_cost then, because Option<T> also provides conditional drop. ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintArea: New lintsE-hardCall for participation: This a hard problem and requires more experience or effort to work onL-restrictionLint: Belongs in the restriction lint group

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @SoniEx2@camsteffen@kvverti

        Issue actions

          Lint request: drop_flags · Issue #7025 · rust-lang/rust-clippy