rust-clippy icon indicating copy to clipboard operation
rust-clippy copied to clipboard

temporary with significant Drop can be early dropped

Open madser123 opened this issue 3 months ago • 0 comments

Summary

Originally submitted for Rocket, as i thought it was the macro.

Lint Name

clippy::significant-drop-tightening

Reproducer

I tried this code:

/// Library "hipster":
use rocket_sync_db_pools::{database, diesel};

#[database("hipster")]
pub struct DBPool(diesel::PgConnection);

/// Rocket application:
#[get("/some/route")] <-- This macro invokes the lint
pub fn route(conn: hipster::DBPool) {
    conn.run(//do stuff...);
}

The macro generates this code, which shouldn't warn, as mentioned here

let __rocket__db: DBPool = match <DBPool as ::rocket::request::FromRequest>::from_request( __req,) .await {
    ::rocket::outcome::Outcome::Success(__v) => __v,
    ::rocket::outcome::Outcome::Forward(__e) => {
        return ::rocket::outcome::Outcome::Forward((__data, __e));
    }
    ::rocket::outcome::Outcome::Error((__c, __e)) => {
        return ::rocket::outcome::Outcome::Error(__c);
    }
};

let ___responder = route(__rocket__db);

I saw this happen:

warning: temporary with significant `Drop` can be early dropped
  --> bin/hipster-backend/src/web/api/mod.rs:69:35
   |
68 | #[get("/validate_credentials/<credentials>")]
   | --------------------------------------------- temporary `__rocket_conn` is currently being dropped at the end of its contained scope
69 | pub async fn validate_credentials(conn: hipster::DBPool, credentials: &str) -> Json<Validation> {
   |                                   ^^^^
   |
   = 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: to override `-W clippy::nursery` add `#[allow(clippy::significant_drop_tightening)]`
help: merge the temporary construction with its single usage
   |
69 ~ pub async fn validate_credentials(conn: 
70 ~ let Json = hipster.;::DBPool, credentials: &str) -> Json<Validation> {
   |
help: remove separated single usage
   |
69 - pub async fn validate_credentials(conn: hipster::DBPool, credentials: &str) -> Json<Validation> {
69 + pub async fn validate_credentials(conn: hipster::DBPool, credentials: &str) -> <Validation> {
   |

I expected to see this happen: Nothing

Version

rustc 1.77.2 (25ef9e3d8 2024-04-09)
binary: rustc
commit-hash: 25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04
commit-date: 2024-04-09
host: x86_64-unknown-linux-gnu
release: 1.77.2
LLVM version: 17.0.6

Additional Labels

No response

madser123 avatar Apr 30 '24 08:04 madser123