rust-clippy icon indicating copy to clipboard operation
rust-clippy copied to clipboard

`significant_drop_tightening` lint doesn't trigger when manually dropping

Open m4rch3n1ng opened this issue 1 year ago • 5 comments

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

m4rch3n1ng avatar Sep 21 '24 08:09 m4rch3n1ng