Skip to content

Commit 1baf8ac

Browse files
committed
[significant_drop_tightening] Add #11125 test
1 parent 757fe49 commit 1baf8ac

3 files changed

+90
-6
lines changed

tests/ui/significant_drop_tightening.fixed

+43-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,15 @@
22

33
#![warn(clippy::significant_drop_tightening)]
44

5-
use std::sync::Mutex;
5+
use std::{
6+
cell::UnsafeCell,
7+
mem::{drop as unlock, MaybeUninit},
8+
sync::{
9+
atomic::{AtomicU64, AtomicUsize},
10+
Mutex,
11+
},
12+
task::Waker,
13+
};
614

715
pub fn complex_return_triggers_the_lint() -> i32 {
816
fn foo() -> i32 {
@@ -28,6 +36,40 @@ pub fn issue_10413() {
2836
}
2937
}
3038

39+
pub fn issue_11125() {
40+
pub struct Channel<T> {
41+
pub inner: Inner,
42+
pub slots: [UnsafeCell<MaybeUninit<T>>],
43+
}
44+
45+
pub struct Inner {
46+
pub join_wakers: Mutex<Vec<Waker>>,
47+
pub ref_count: AtomicUsize,
48+
pub sender_wakers: Mutex<Vec<Waker>>,
49+
pub status: AtomicU64,
50+
}
51+
52+
pub struct SendValue<'s, T> {
53+
pub channel: &'s Channel<T>,
54+
pub registered_waker: Option<Waker>,
55+
pub value: Option<T>,
56+
}
57+
58+
impl<'s, T> Drop for SendValue<'s, T> {
59+
fn drop(&mut self) {
60+
if let Some(waker) = self.registered_waker.take() {
61+
let mut sender_wakers = self.channel.inner.sender_wakers.lock().unwrap();
62+
let idx_opt = sender_wakers.iter().position(|w| w.will_wake(&waker));
63+
if let Some(idx) = idx_opt {
64+
let local_waker = sender_wakers.swap_remove(idx);
65+
unlock(sender_wakers);
66+
drop(local_waker);
67+
}
68+
}
69+
}
70+
}
71+
}
72+
3173
pub fn issue_11128() {
3274
use std::mem::drop as unlock;
3375

tests/ui/significant_drop_tightening.rs

+43-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,15 @@
22

33
#![warn(clippy::significant_drop_tightening)]
44

5-
use std::sync::Mutex;
5+
use std::{
6+
cell::UnsafeCell,
7+
mem::{drop as unlock, MaybeUninit},
8+
sync::{
9+
atomic::{AtomicU64, AtomicUsize},
10+
Mutex,
11+
},
12+
task::Waker,
13+
};
614

715
pub fn complex_return_triggers_the_lint() -> i32 {
816
fn foo() -> i32 {
@@ -27,6 +35,40 @@ pub fn issue_10413() {
2735
}
2836
}
2937

38+
pub fn issue_11125() {
39+
pub struct Channel<T> {
40+
pub inner: Inner,
41+
pub slots: [UnsafeCell<MaybeUninit<T>>],
42+
}
43+
44+
pub struct Inner {
45+
pub join_wakers: Mutex<Vec<Waker>>,
46+
pub ref_count: AtomicUsize,
47+
pub sender_wakers: Mutex<Vec<Waker>>,
48+
pub status: AtomicU64,
49+
}
50+
51+
pub struct SendValue<'s, T> {
52+
pub channel: &'s Channel<T>,
53+
pub registered_waker: Option<Waker>,
54+
pub value: Option<T>,
55+
}
56+
57+
impl<'s, T> Drop for SendValue<'s, T> {
58+
fn drop(&mut self) {
59+
if let Some(waker) = self.registered_waker.take() {
60+
let mut sender_wakers = self.channel.inner.sender_wakers.lock().unwrap();
61+
let idx_opt = sender_wakers.iter().position(|w| w.will_wake(&waker));
62+
if let Some(idx) = idx_opt {
63+
let local_waker = sender_wakers.swap_remove(idx);
64+
unlock(sender_wakers);
65+
drop(local_waker);
66+
}
67+
}
68+
}
69+
}
70+
}
71+
3072
pub fn issue_11128() {
3173
use std::mem::drop as unlock;
3274

tests/ui/significant_drop_tightening.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: temporary with significant `Drop` can be early dropped
2-
--> $DIR/significant_drop_tightening.rs:12:9
2+
--> $DIR/significant_drop_tightening.rs:20:9
33
|
44
LL | pub fn complex_return_triggers_the_lint() -> i32 {
55
| __________________________________________________-
@@ -23,7 +23,7 @@ LL + drop(lock);
2323
|
2424

2525
error: temporary with significant `Drop` can be early dropped
26-
--> $DIR/significant_drop_tightening.rs:79:13
26+
--> $DIR/significant_drop_tightening.rs:121:13
2727
|
2828
LL | / {
2929
LL | | let mutex = Mutex::new(1i32);
@@ -43,7 +43,7 @@ LL + drop(lock);
4343
|
4444

4545
error: temporary with significant `Drop` can be early dropped
46-
--> $DIR/significant_drop_tightening.rs:100:13
46+
--> $DIR/significant_drop_tightening.rs:142:13
4747
|
4848
LL | / {
4949
LL | | let mutex = Mutex::new(1i32);
@@ -67,7 +67,7 @@ LL +
6767
|
6868

6969
error: temporary with significant `Drop` can be early dropped
70-
--> $DIR/significant_drop_tightening.rs:106:17
70+
--> $DIR/significant_drop_tightening.rs:148:17
7171
|
7272
LL | / {
7373
LL | | let mutex = Mutex::new(vec![1i32]);

0 commit comments

Comments
 (0)