rust-lang / rust-clippy

A bunch of lints to catch common mistakes and improve your Rust code. Book: https://doc.rust-lang.org/clippy/
https://rust-lang.github.io/rust-clippy/
Other
11.01k stars 1.47k forks source link

lint suggests dropping temporary with significant drop after another function takes ownership of it. #12128

Open VictorGamerLOL opened 5 months ago

VictorGamerLOL commented 5 months ago

Summary

Clippy suggests dropping a temporary with a significant Drop after another function has taken ownership of it. Implementing its suggestion leads to an error.

Lint Name

significant_drop_tightening

Reproducer

I tried this code:

#[warn(clippy::significant_drop_tightening)]
fn main() -> Result<(), Box<dyn std::error::Error>> {
    let lock = std::sync::Mutex::new(5);
    let mut lock = lock.lock().unwrap();
    *lock += 1;
    other_function(lock);
    Ok(())
}

fn other_function(_lock: std::sync::MutexGuard<i32>) {}

// #[warn(clippy::significant_drop_tightening)]
// fn main() -> Result<(), Box<dyn std::error::Error>> {
//     let lock = std::sync::RwLock::new(5);
//     let mut lock = lock.write().unwrap();
//     *lock += 1;
//     other_function(lock);
//     Ok(())
// }

// fn other_function(_lock: std::sync::RwLockWriteGuard<i32>) {}

// #[warn(clippy::significant_drop_tightening)]
// fn main() -> Result<(), Box<dyn std::error::Error>> {
//     let lock = std::sync::RwLock::new(5);
//     let lock = lock.read().unwrap();
//     assert!(*lock == 5);
//     other_function(lock);
//     Ok(())
// }

// fn other_function(_lock: std::sync::RwLockReadGuard<i32>) {}

I saw this happen:

  = 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:8
  |
1 | #[warn(clippy::significant_drop_tightening)]
  |        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: drop the temporary after the end of its last usage
  |
6 ~     other_function(lock);
7 +     drop(lock);
  |

warning: `rust-bug-reports` (bin "rust-bug-reports") generated 1 warning
    Finished dev [unoptimized + debuginfo] target(s) in 0.00s

I expected to see this happen:

Version

rustc 1.75.0 (82e1608df 2023-12-21)
binary: rustc
commit-hash: 82e1608dfa6e0b5569232559e3d385fea5a93112
commit-date: 2023-12-21
host: x86_64-unknown-linux-gnu
release: 1.75.0
LLVM version: 17.0.6

Additional Labels

@rustbot label +I-suggestion-causes-error

rustbot commented 5 months ago

Error: Parsing relabel command in comment failed: ...' label +' | error: empty label at >| ' I-suggest'...

Please file an issue on GitHub at triagebot if there's a problem with this bot, or reach out on #t-infra on Zulip.