Skip to content

Commit 83b7d71

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 54a0e18 + a15064d commit 83b7d71

File tree

7 files changed

+22
-9
lines changed

7 files changed

+22
-9
lines changed

core/src/mem/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -968,6 +968,7 @@ pub const fn replace<T>(dest: &mut T, src: T) -> T {
968968
/// Integers and other types implementing [`Copy`] are unaffected by `drop`.
969969
///
970970
/// ```
971+
/// # #![cfg_attr(not(bootstrap), allow(drop_copy))]
971972
/// #[derive(Copy, Clone)]
972973
/// struct Foo(u8);
973974
///

core/src/task/poll.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ impl<T> Poll<T> {
116116
/// let fut = Pin::new(&mut fut);
117117
///
118118
/// let num = fut.poll(cx).ready()?;
119-
/// # drop(num);
119+
/// # let _ = num; // to silence unused warning
120120
/// // ... use num
121121
///
122122
/// Poll::Ready(())

core/src/task/ready.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use core::task::Poll;
2222
/// let fut = Pin::new(&mut fut);
2323
///
2424
/// let num = ready!(fut.poll(cx));
25-
/// # drop(num);
25+
/// # let _ = num;
2626
/// // ... use num
2727
///
2828
/// Poll::Ready(())
@@ -44,7 +44,7 @@ use core::task::Poll;
4444
/// Poll::Ready(t) => t,
4545
/// Poll::Pending => return Poll::Pending,
4646
/// };
47-
/// # drop(num);
47+
/// # let _ = num; // to silence unused warning
4848
/// # // ... use num
4949
/// #
5050
/// # Poll::Ready(())

std/src/panicking.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,7 @@ pub fn begin_panic_handler(info: &PanicInfo<'_>) -> ! {
541541
// Lazily, the first time this gets called, run the actual string formatting.
542542
self.string.get_or_insert_with(|| {
543543
let mut s = String::new();
544-
drop(s.write_fmt(*inner));
544+
let _err = s.write_fmt(*inner);
545545
s
546546
})
547547
}

std/src/sys/sgx/waitqueue/mod.rs

+13-3
Original file line numberDiff line numberDiff line change
@@ -202,12 +202,18 @@ impl WaitQueue {
202202
pub fn notify_one<T>(
203203
mut guard: SpinMutexGuard<'_, WaitVariable<T>>,
204204
) -> Result<WaitGuard<'_, T>, SpinMutexGuard<'_, WaitVariable<T>>> {
205+
// SAFETY: lifetime of the pop() return value is limited to the map
206+
// closure (The closure return value is 'static). The underlying
207+
// stack frame won't be freed until after the WaitGuard created below
208+
// is dropped.
205209
unsafe {
206-
if let Some(entry) = guard.queue.inner.pop() {
210+
let tcs = guard.queue.inner.pop().map(|entry| -> Tcs {
207211
let mut entry_guard = entry.lock();
208-
let tcs = entry_guard.tcs;
209212
entry_guard.wake = true;
210-
drop(entry);
213+
entry_guard.tcs
214+
});
215+
216+
if let Some(tcs) = tcs {
211217
Ok(WaitGuard { mutex_guard: Some(guard), notified_tcs: NotifiedTcs::Single(tcs) })
212218
} else {
213219
Err(guard)
@@ -223,13 +229,17 @@ impl WaitQueue {
223229
pub fn notify_all<T>(
224230
mut guard: SpinMutexGuard<'_, WaitVariable<T>>,
225231
) -> Result<WaitGuard<'_, T>, SpinMutexGuard<'_, WaitVariable<T>>> {
232+
// SAFETY: lifetime of the pop() return values are limited to the
233+
// while loop body. The underlying stack frames won't be freed until
234+
// after the WaitGuard created below is dropped.
226235
unsafe {
227236
let mut count = 0;
228237
while let Some(entry) = guard.queue.inner.pop() {
229238
count += 1;
230239
let mut entry_guard = entry.lock();
231240
entry_guard.wake = true;
232241
}
242+
233243
if let Some(count) = NonZeroUsize::new(count) {
234244
Ok(WaitGuard { mutex_guard: Some(guard), notified_tcs: NotifiedTcs::All { count } })
235245
} else {

std/src/sys/unix/fs.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1210,7 +1210,7 @@ impl File {
12101210
// Redox doesn't appear to support `UTIME_OMIT`.
12111211
// ESP-IDF and HorizonOS do not support `futimens` at all and the behavior for those OS is therefore
12121212
// the same as for Redox.
1213-
drop(times);
1213+
let _ = times;
12141214
Err(io::const_io_error!(
12151215
io::ErrorKind::Unsupported,
12161216
"setting file times not supported",

std/src/thread/tests.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,9 @@ fn test_scoped_threads_nll() {
375375
// this is mostly a *compilation test* for this exact function:
376376
fn foo(x: &u8) {
377377
thread::scope(|s| {
378-
s.spawn(|| drop(x));
378+
s.spawn(|| match x {
379+
_ => (),
380+
});
379381
});
380382
}
381383
// let's also run it for good measure

0 commit comments

Comments
 (0)