rust-lang / rust-lang/rust-clippy

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

Open
#9,332 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug I-false-positive
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the drop_non_drop lint and reproduce the provided std::sync::Mutex example for both target triples shown. Trace how the lint determines whether the value implements Drop; done means identical diagnostics for the two targets, with the expected target-independent behavior covered by a test.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.