Skip to content

drop_non_drop should not conditionally trigger depending on target-specific implementations of Mutex #9332

Open
@yvt

Description

@yvt

Summary

Starting from rust-lang/rust#95035, std::sync::Mutex and other similar synchronization primitives have trivial destructors on some but not all platforms, causing the drop_non_drop lint to conditionally trigger on drop(mutex) depending on current build targets.

Since this makes #[expect(...)] hard to use and might lead programmers to incorrectly generalize the behavior from what they see on their local machines, I think Clippy should assume non-trivial destructors for these types regardless of their underlying implementations.

Lint Name

drop_non_drop

Reproducer

I tried this code:

#[allow(unused_assignments)]
pub fn hoge() {
    let mut x = 42;
    let m = std::sync::Mutex::new(&mut x);
    drop(m);
    x = 56;
}

I saw this happen:

$ cargo clippy --target aarch64-unknown-linux-gnu
    Checking hoge v0.1.0 (/private/tmp/hoge)
warning: call to `std::mem::drop` with a value that does not implement `Drop`. Dropping such a type only extends its contained lifetimes
 --> src/lib.rs:5:5
  |
5 |     drop(m);
  |     ^^^^^^^
  |
  = note: `#[warn(clippy::drop_non_drop)]` on by default
note: argument has type `std::sync::Mutex<&mut i32>`
 --> src/lib.rs:5:10
  |
5 |     drop(m);
  |          ^
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#drop_non_drop

warning: `hoge` (lib) generated 1 warning
    Finished dev [unoptimized + debuginfo] target(s) in 0.14s

$ cargo clippy --target aarch64-apple-darwin
    Checking hoge v0.1.0 (/private/tmp/hoge)
    Finished dev [unoptimized + debuginfo] target(s) in 0.38s

I expected to see this happen: Identical results for both targets

$ cargo clippy --target aarch64-unknown-linux-gnu
    Checking hoge v0.1.0 (/private/tmp/hoge)
    Finished dev [unoptimized + debuginfo] target(s) in 0.38s

$ cargo clippy --target aarch64-apple-darwin
    Checking hoge v0.1.0 (/private/tmp/hoge)
    Finished dev [unoptimized + debuginfo] target(s) in 0.38s

Version

rustc 1.65.0-nightly (29e4a9ee0 2022-08-10)
binary: rustc
commit-hash: 29e4a9ee0253cd39e552a77f51f11f9a5f1c41e6
commit-date: 2022-08-10
host: aarch64-apple-darwin
release: 1.65.0-nightly
LLVM version: 14.0.6

Additional Labels

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't have

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions