rust-lang / rust-lang/rust-clippy

False positive `significant-drop-tightening` with `tokioRwLockGuard.downgrade()`

Open
#11,549 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Summary

Tokio provides RwLockWriteGuard::downgrade(self) (doc, which takes ownership of the write guard (i.e. dropping it) and returns only a read guard. Nursery lint significant-drop-tightening, however, doesn't catch this.

Playground with the repro: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=842ef76625594b0ddd713af3eff6064d

Version of rustc etc. below is from my local machine where I saw this happen in a larger codebase, in playground I reproduced this with v1.72.1 & Clippy 0.1.74 (2023-09-20 3223b0b)

Lint Name

significant-drop-tightening

Reproducer

I tried this code:

async fn main() {
    let lock = tokio::sync::RwLock::new(4);
    let mut guard = lock.write().await;
    *guard = 5;
    let guard = guard.downgrade();
    println!("{}", *guard);
}

I saw this happen:

warning: temporary with significant `Drop` can be early dropped
  --> src/main.rs:8:13
   |
6  |   async fn main() {
   |  _________________-
7  | |     let lock = tokio::sync::RwLock::new(4);
8  | |     let mut guard = lock.write().await;
   | |             ^^^^^
9  | |     *guard = 5;
10 | |     let guard = guard.downgrade();
11 | |     println!("{}", *guard);
12 | | }
   | |_- temporary `guard` is currently being dropped at the end of its contained scope
   |
   = note: this might lead to unnecessary resource contention

I expected to see this happen:

No warning.

Version
rustc 1.73.0-nightly (4c8bb79d9 2023-07-15)
binary: rustc
commit-hash: 4c8bb79d9f565115637cc6da739f8389e79f3a29
commit-date: 2023-07-15
host: x86_64-unknown-linux-gnu
release: 1.73.0-nightly
LLVM version: 16.0.5
Additional Labels

No response

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 linked Rust Playground reproducer and the significant-drop-tightening lint. Trace how the lint handles tokioRwLockGuard.downgrade() consuming the write guard, then verify that the reproducer no longer emits a warning and that the behavior is covered by lint tests.

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.