Skip to content

Commit 8484421

Browse files
committed
Auto merge of #109732 - Urgau:uplift_drop_forget_ref_lints, r=davidtwco
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)
2 parents 4f40a1e + fed5003 commit 8484421

File tree

4 files changed

+9
-0
lines changed

4 files changed

+9
-0
lines changed

tests/fail/stacked_borrows/illegal_write2.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![allow(drop_ref)]
2+
13
fn main() {
24
let target = &mut 42;
35
let target2 = target as *mut _;

tests/fail/uninit_buffer.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
//@error-in-other-file: memory is uninitialized at [0x4..0x10]
22

3+
#![allow(drop_copy)]
4+
35
use std::alloc::{alloc, dealloc, Layout};
46
use std::slice::from_raw_parts;
57

tests/fail/uninit_buffer_with_provenance.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
//@normalize-stderr-test: "a[0-9]+" -> "ALLOC"
33
#![feature(strict_provenance)]
44

5+
#![allow(drop_copy)]
6+
57
// Test printing allocations that contain single-byte provenance.
68

79
use std::alloc::{alloc, dealloc, Layout};

tests/pass/stacked-borrows/zst-field-retagging-terminates.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
//@compile-flags: -Zmiri-retag-fields
22
// Checks that the test does not run forever (which relies on a fast path).
3+
4+
#![allow(drop_copy)]
5+
36
fn main() {
47
let array = [(); usize::MAX];
58
drop(array); // Pass the array to a function, retagging its fields

0 commit comments

Comments
 (0)