rust-lang / rust-lang/rust-clippy

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

Open
#12,128 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

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

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 reproducer and the significant_drop_tightening lint described in the issue. Investigate why the suggested drop is emitted after ownership has moved to other_function, including the MutexGuard and RwLock guard cases. Done means the lint no longer produces an invalid suggestion, with coverage for the reported examples.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.