rust-lang / rust-lang/rust-clippy
`significant_drop_tightening` lint doesn't trigger when manually dropping
Nobody has claimed this yet.
- 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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