Skip to content

False positive for dropping_copy_types #112653

Closed
@ia0

Description

@ia0

The false positive detection done in rust-lang/rust-clippy#9491 is actually too restrictive. The test is whether drop is used with the following pattern: pat => drop(fun(args, ...)). In particular, this doesn't match pat => drop(expr) which would be more general and cover things like pat => drop(fun(args, ...)?) (notice the ?).

Here's an example where the false positive triggers:

fn foo() -> Result<u8, ()> {
    println!("doing foo");
    Ok(0) // result is not always useful, the side-effect matters
}
fn bar() {
    println!("doing bar");
}

pub fn stuff() -> Result<(), ()> {
    match 42 {
        0 => drop(foo()?),  // drop is needed because we only care about side-effects
        1 => bar(),
        _ => (),  // doing nothing (no side-effects needed here)
    }
    Ok(())
}
warning: calls to `std::mem::drop` with a value that implements `Copy` does nothing
  --> src/lib.rs:11:14
   |
11 |         0 => drop(foo()?),  // drop is needed because we only care about side-effects
   |              ^^^^^------^
   |                   |
   |                   argument has type `u8`
   |
   = note: use `let _ = ...` to ignore the expression or result
   = note: `#[warn(dropping_copy_types)]` on by default

Metadata

Metadata

Assignees

Labels

A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.C-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions