Skip to content

[significant_drop_tightening] value inside tuple may or may not be recognised as a valid drop. #11189

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
VictorGamerLOL opened this issue Jul 19, 2023 · 1 comment · Fixed by #11196
Assignees
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have

Comments

@VictorGamerLOL
Copy link

VictorGamerLOL commented Jul 19, 2023

Summary

If values are dropped using a tuple in a drop statement, some of the values may not be recognized as being dropped there and the lint will trigger.

Lint Name

significant_drop_tightening

Reproducer

I tried this code:

#![warn(clippy::nursery)]

use lazy_static::lazy_static;
use std::sync::{ Arc, Mutex };

struct Number {
    pub value: u32,
}

lazy_static! {
    static ref NUMBER: Arc<Mutex<Number>> = Arc::new(Mutex::new(Number { value: 1 }));
    static ref NUMBER2: Arc<Mutex<Number>> = Arc::new(Mutex::new(Number { value: 2 }));
    static ref NUMBER3: Arc<Mutex<Number>> = Arc::new(Mutex::new(Number { value: 3 }));
}

fn main() {
    do_something().unwrap();
}

fn do_something() -> Result<(), ()> {
    let mut lock = NUMBER.lock().unwrap();
    let mut lock2 = NUMBER2.lock().unwrap();
    let mut lock3 = NUMBER3.lock().unwrap();
    lock.value += 1;
    lock2.value += 1;
    lock3.value += 1;
    drop((lock, lock2, lock3));
    Ok(())
}

I saw this happen:

warning: temporary with significant `Drop` can be early dropped
  --> src/main.rs:21:13
   |
20 |   fn do_something() -> Result<(), ()> {
   |  _____________________________________-
21 | |     let mut lock = NUMBER.lock().unwrap();
   | |             ^^^^
22 | |     let mut lock2 = NUMBER2.lock().unwrap();
23 | |     let mut lock3 = NUMBER3.lock().unwrap();
...  |
28 | |     Ok(())
29 | | }
   | |_- temporary `lock` is currently being dropped at the end of its contained scope
   |
   = 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:1:9
   |
1  | #![warn(clippy::nursery)]
   |         ^^^^^^^^^^^^^^^
   = note: `#[warn(clippy::significant_drop_tightening)]` implied by `#[warn(clippy::nursery)]`
help: drop the temporary after the end of its last usage
   |
27 ~     drop((lock, lock2, lock3));
28 +     drop(lock);
   |

I expected to see this happen:

Version

rustc 1.71.0 (8ede3aae2 2023-07-12)
binary: rustc
commit-hash: 8ede3aae28fe6e4d52b38157d7bfe0d3bceef225
commit-date: 2023-07-12
host: x86_64-unknown-linux-gnu
release: 1.71.0
LLVM version: 16.0.5

Additional Labels

No response

@VictorGamerLOL VictorGamerLOL added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels Jul 19, 2023
@c410-f3r
Copy link
Contributor

@rustbot claim

bors added a commit that referenced this issue Jul 22, 2023
[significant_drop_tightening] Fix #11189

Fix #11189

```
changelog: FP: [`significant_drop_tightening`]: Consider tuples in drop calls
```
@bors bors closed this as completed in f0a16bb Jul 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants