rust-lang / rust-lang/rust-clippy

Significant drop tightening raises at an effective last statement

Open
#11,491 2 comments 0 reactions 1 assignee View on GitHub

@c410-f3r is already working on this.

Since Sep 23, 2023.

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

Description

Summary

I was using an asynchronous lock. The last usage of that lock is inside a match branch and an if. There is no more statements after the match, so it is a tail call and a manual drop doesn't help.

https://github.com/jdh8/natsuki/blob/d4d22b3eee530b47d2d24a98ac394647daa593fd/src/topgg.rs#L58

Lint Name

significant-drop-tightening

Reproducer

I tried this code:

async fn ready(&self, _: serenity::Context, ready: serenity::Ready) {
    let Some(token) = self.token.as_deref() else { return };
    match ready.shard {
        None => post(token, Stats { guilds: ready.guilds.len() as u64, shards: 0 }).await,
        Some([_, shards]) => {
            let mut stats = self.stats.lock().await;
            stats.guilds += ready.guilds.len() as u64;
            stats.shards += 1;
            
            if stats.shards == shards {
                post(token, *stats).await;
            }
        },
    };
}

I saw this happen:

warning: temporary with significant `Drop` can be early dropped
  --> src/topgg.rs:53:25
   |
52 |               Some([_, shards]) => {
   |  __________________________________-
53 | |                 let mut stats = self.stats.lock().await;
   | |                         ^^^^^
54 | |                 stats.guilds += ready.guilds.len() as u64;
55 | |                 stats.shards += 1;
...  |
59 | |                 }
60 | |             },
   | |_____________- temporary `stats` 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: `-W clippy::significant-drop-tightening` implied by `-W clippy::nursery`
help: drop the temporary after the end of its last usage
   |
58 ~                     post(token, *stats).await;
59 +                     drop(stats);
   |

I expected to see this happen:

No warning

Version
rustc 1.72.0 (5680fa18f 2023-08-23) (Fedora 1.72.0-1.fc38)
binary: rustc
commit-hash: 5680fa18feaa87f3ff04063800aec256c3d4b4be
commit-date: 2023-08-23
host: x86_64-unknown-linux-gnu
release: 1.72.0
LLVM version: 16.0.6
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.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.