Skip to content

drop_copy should not trigger when drop is used in a match branch #9482

Closed
@ia0

Description

@ia0

Summary

The lint description says:

drop does nothing for types that implement Copy

This is wrong, drop can be used to forget that a function returns a non-unit type when the result value is sometimes useless.

Lint Name

drop_copy

Reproducer

I tried this code:

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

fn main() {
    match 42 {
        0 => drop(foo()),  // drop is needed because we only care about side-effects
        1 => bar(),
        _ => (),  // doing nothing (no side-effects needed here)
    }
}

I saw this happen:

error: calls to `std::mem::drop` with a value that implements `Copy`. Dropping a copy leaves the original intact
  --> src/main.rs:11:14
   |
11 |         0 => drop(foo()),  // drop is needed because we only care about side-effects
   |              ^^^^^^^^^^^
   |
   = note: `#[deny(clippy::drop_copy)]` on by default
note: argument has type `u8`
  --> src/main.rs:11:19
   |
11 |         0 => drop(foo()),  // drop is needed because we only care about side-effects
   |                   ^^^^^
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#drop_copy

I expected to see this happen:

No error

Version

rustc 1.63.0 (4b91a6ea7 2022-08-08)
binary: rustc
commit-hash: 4b91a6ea7258a947e59c6522cd5898e7c0a6a88f
commit-date: 2022-08-08
host: x86_64-unknown-linux-gnu
release: 1.63.0
LLVM version: 14.0.5

Additional Labels

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't have

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions