Skip to content

Lint request: warn when temporaries are used for values for which dropping has side effects #1904

Open

Description

@PieterPenninckx

Temporary values (that I tend to call 'anonymous values') are values for which there is no variable binding. E.g.:

let a = Vec::<u8>::new().len();

Here the value of the type Vec<u8> is a temporary value.

Temporary values are -- generally speaking -- dropped at the semi-colon that terminates the statement in which they were created. More details can be found in the reference. This is not so well known and can be confusing, see e.g. rust-lang/book#1871. This can lead to bugs when a temporary is used to store a value for which the precise point where it is dropped is important, such as a LockResult. An example can be found in rust-lang/rust#37612.

To prevent such bugs, I think it would be a good idea if clippy would warn when a 'sensitive' value is stored in a temporary. With a 'sensitive' value, I mean something like a LockResult where calling the drop() has important side effects (so not just releasing memory).

Activity

  1. PieterPenninckx commented on Jul 27, 2017

    @PieterPenninckx
    Author

    I realized that my request was too vague about which datatypes have important side effects when being dropped. Since there is no way clippy could judge this, I would just work with a list. This will not capture all problems, but hopefully enough of them. This is the list I came up with:

    • std::sync::LockResult<T>
    • std::sync::TryLockResult<T>
    • std::sync::MutexGuard<T>
    • std::sync::RwLockReadGuard<T>
    • std::sync::RwLockWriteGuard<T>
    • std::cell::Ref<T>
    • std::cell::RefMut<T>
  2. added
    L-correctnessLint: Belongs in the correctness lint group
    E-mediumCall for participation: Medium difficulty level problem and requires some initial experience.
    T-middleType: Probably requires verifiying types
    T-MIRType: This lint will require working with the MIR
    on Jul 27, 2017
  3. PieterPenninckx commented on Jul 1, 2018

    @PieterPenninckx
    Author

    I'm working on this.
    Update: I gave up a long time ago.

  4. academician commented on Feb 21, 2025

    @academician

    Is it worth revisiting and possibly closing this issue after the changes to temporary scope in the 2024 edition?

  5. PieterPenninckx commented on Feb 24, 2025

    @PieterPenninckx
    Author

    @academician , I think the issue is still there in the 2024 edition: the 2024 edition only narrowed the scope when the temporary is in a tail expression, which is a case I hadn't ever considered. If the temporary is not in a tail expression, the issue is still there.

    Also, reading into the documentation again after seven years, I now read "The exact rules for temporary lifetime extension are subject to change.", which to me is a sign that storing something of which the drop order is of importance in a temporary can still lead to issues.

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

    E-mediumCall for participation: Medium difficulty level problem and requires some initial experience.L-correctnessLint: Belongs in the correctness lint groupT-MIRType: This lint will require working with the MIRT-middleType: Probably requires verifiying types

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions