Skip to content

Commit

Permalink
Auto merge of #109732 - Urgau:uplift_drop_forget_ref_lints, r=davidtwco
Browse files Browse the repository at this point in the history
Uplift `clippy::{drop,forget}_{ref,copy}` lints

This PR aims at uplifting the `clippy::drop_ref`, `clippy::drop_copy`, `clippy::forget_ref` and `clippy::forget_copy` lints.

Those lints are/were declared in the correctness category of clippy because they lint on useless and most probably is not what the developer wanted.

## `drop_ref` and `forget_ref`

The `drop_ref` and `forget_ref` lint checks for calls to `std::mem::drop` or `std::mem::forget` with a reference instead of an owned value.

### Example

```rust
let mut lock_guard = mutex.lock();
std::mem::drop(&lock_guard) // Should have been drop(lock_guard), mutex
// still locked
operation_that_requires_mutex_to_be_unlocked();
```

### Explanation

Calling `drop` or `forget` on a reference will only drop the reference itself, which is a no-op. It will not call the `drop` or `forget` method on the underlying referenced value, which is likely what was intended.

## `drop_copy` and `forget_copy`

The `drop_copy` and `forget_copy` lint checks for calls to `std::mem::forget` or `std::mem::drop` with a value that derives the Copy trait.

### Example

```rust
let x: i32 = 42; // i32 implements Copy
std::mem::forget(x) // A copy of x is passed to the function, leaving the
                    // original unaffected
```

### Explanation

Calling `std::mem::forget` [does nothing for types that implement Copy](https://doc.rust-lang.org/std/mem/fn.drop.html) since the value will be copied and moved into the function on invocation.

-----

Followed the instructions for uplift a clippy describe here: rust-lang/rust#99696 (review)

cc `@m-ou-se` (as T-libs-api leader because the uplifting was discussed in a recent meeting)
  • Loading branch information
bors committed May 12, 2023
2 parents 4f40a1e + fed5003 commit 8484421
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 0 deletions.
2 changes: 2 additions & 0 deletions tests/fail/stacked_borrows/illegal_write2.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(drop_ref)]

fn main() {
let target = &mut 42;
let target2 = target as *mut _;
Expand Down
2 changes: 2 additions & 0 deletions tests/fail/uninit_buffer.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//@error-in-other-file: memory is uninitialized at [0x4..0x10]

#![allow(drop_copy)]

use std::alloc::{alloc, dealloc, Layout};
use std::slice::from_raw_parts;

Expand Down
2 changes: 2 additions & 0 deletions tests/fail/uninit_buffer_with_provenance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
//@normalize-stderr-test: "a[0-9]+" -> "ALLOC"
#![feature(strict_provenance)]

#![allow(drop_copy)]

// Test printing allocations that contain single-byte provenance.

use std::alloc::{alloc, dealloc, Layout};
Expand Down
3 changes: 3 additions & 0 deletions tests/pass/stacked-borrows/zst-field-retagging-terminates.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
//@compile-flags: -Zmiri-retag-fields
// Checks that the test does not run forever (which relies on a fast path).

#![allow(drop_copy)]

fn main() {
let array = [(); usize::MAX];
drop(array); // Pass the array to a function, retagging its fields
Expand Down

0 comments on commit 8484421

Please sign in to comment.