rust-lang / rust-lang/rust-clippy

`significant_drop_tightening` lint doesn't trigger when manually dropping

Open
#13,429 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Summary

when manually dropping the lock the lint doesn't recognize, that it could be dropped earlier. it would be nice to lint that as well.

Lint Name

significant_drop_tightening

Reproducer

I tried this code:

use std::sync::Mutex;

static THING: Mutex<u32> = Mutex::new(0);

#[warn(clippy::significant_drop_tightening)]
fn main() {
	let mut thing = THING.lock().unwrap();
	*thing = 1;
	*thing += 1;

	println!("this is here to demonstrate the issue");
	println!("this lint does get triggered without the drop");

	drop(thing);
}

I expected to see this happen:

warning: temporary with significant `Drop` can be early dropped
  --> src/main.rs:7:10
   |
6  |   fn main() {
7  |       let mut thing = THING.lock().unwrap();
   |               ^^^^^
8  |       *thing = 1;
9  |       *thing += 1;
...   
14 |       drop(thing);
   |       ^^^^^^^^^^^
15 |   }
   |  _- temporary `thing` is currently being dropped later than necessary
   |
   = note: this might lead to unnecessary resource contention
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#significant_drop_tightening
note: the lint level is defined here
  --> src/main.rs:5:8
   |
5  | #[warn(clippy::significant_drop_tightening)]
   |        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: drop the temporary after the end of its last usage
   |
9  ~     *thing += 1;
10 +  drop(thing);
...
14 -     drop(thing);
   |

Instead, this happened:

nothing

More info:

if i remove the drop clippy correctly complains that it is dropped at the end of the scope and could be dropped earlier.

Version

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 significant_drop_tightening lint and the Rust reproducer in the issue, comparing behavior with and without the explicit drop(thing). Confirm that manually dropping the mutex guard is recognized and that the diagnostic points to the guard's last use with the suggested earlier drop.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.