Skip to content

[significant_drop_tightening] Add #11125 test #11131

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 43 additions & 3 deletions tests/ui/significant_drop_tightening.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@

#![warn(clippy::significant_drop_tightening)]

use std::sync::Mutex;
use std::{
cell::UnsafeCell,
mem::{drop as unlock, MaybeUninit},
sync::{
atomic::{AtomicU64, AtomicUsize},
Mutex,
},
task::Waker,
};

pub fn complex_return_triggers_the_lint() -> i32 {
fn foo() -> i32 {
Expand All @@ -28,9 +36,41 @@ pub fn issue_10413() {
}
}

pub fn issue_11128() {
use std::mem::drop as unlock;
pub fn issue_11125() {
pub struct Channel<T> {
pub inner: Inner,
pub slots: [UnsafeCell<MaybeUninit<T>>],
}

pub struct Inner {
pub join_wakers: Mutex<Vec<Waker>>,
pub ref_count: AtomicUsize,
pub sender_wakers: Mutex<Vec<Waker>>,
pub status: AtomicU64,
}

pub struct SendValue<'s, T> {
pub channel: &'s Channel<T>,
pub registered_waker: Option<Waker>,
pub value: Option<T>,
}

impl<'s, T> Drop for SendValue<'s, T> {
fn drop(&mut self) {
if let Some(waker) = self.registered_waker.take() {
let mut sender_wakers = self.channel.inner.sender_wakers.lock().unwrap();
let idx_opt = sender_wakers.iter().position(|w| w.will_wake(&waker));
if let Some(idx) = idx_opt {
let local_waker = sender_wakers.swap_remove(idx);
unlock(sender_wakers);
drop(local_waker);
}
}
}
}
}

pub fn issue_11128() {
struct Foo {
droppable: Option<Vec<i32>>,
mutex: Mutex<Vec<i32>>,
Expand Down
46 changes: 43 additions & 3 deletions tests/ui/significant_drop_tightening.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@

#![warn(clippy::significant_drop_tightening)]

use std::sync::Mutex;
use std::{
cell::UnsafeCell,
mem::{drop as unlock, MaybeUninit},
sync::{
atomic::{AtomicU64, AtomicUsize},
Mutex,
},
task::Waker,
};

pub fn complex_return_triggers_the_lint() -> i32 {
fn foo() -> i32 {
Expand All @@ -27,9 +35,41 @@ pub fn issue_10413() {
}
}

pub fn issue_11128() {
use std::mem::drop as unlock;
pub fn issue_11125() {
pub struct Channel<T> {
pub inner: Inner,
pub slots: [UnsafeCell<MaybeUninit<T>>],
}

pub struct Inner {
pub join_wakers: Mutex<Vec<Waker>>,
pub ref_count: AtomicUsize,
pub sender_wakers: Mutex<Vec<Waker>>,
pub status: AtomicU64,
}

pub struct SendValue<'s, T> {
pub channel: &'s Channel<T>,
pub registered_waker: Option<Waker>,
pub value: Option<T>,
}

impl<'s, T> Drop for SendValue<'s, T> {
fn drop(&mut self) {
if let Some(waker) = self.registered_waker.take() {
let mut sender_wakers = self.channel.inner.sender_wakers.lock().unwrap();
let idx_opt = sender_wakers.iter().position(|w| w.will_wake(&waker));
if let Some(idx) = idx_opt {
let local_waker = sender_wakers.swap_remove(idx);
unlock(sender_wakers);
drop(local_waker);
}
}
}
}
}

pub fn issue_11128() {
struct Foo {
droppable: Option<Vec<i32>>,
mutex: Mutex<Vec<i32>>,
Expand Down
8 changes: 4 additions & 4 deletions tests/ui/significant_drop_tightening.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: temporary with significant `Drop` can be early dropped
--> $DIR/significant_drop_tightening.rs:12:9
--> $DIR/significant_drop_tightening.rs:20:9
|
LL | pub fn complex_return_triggers_the_lint() -> i32 {
| __________________________________________________-
Expand All @@ -23,7 +23,7 @@ LL + drop(lock);
|

error: temporary with significant `Drop` can be early dropped
--> $DIR/significant_drop_tightening.rs:79:13
--> $DIR/significant_drop_tightening.rs:119:13
|
LL | / {
LL | | let mutex = Mutex::new(1i32);
Expand All @@ -43,7 +43,7 @@ LL + drop(lock);
|

error: temporary with significant `Drop` can be early dropped
--> $DIR/significant_drop_tightening.rs:100:13
--> $DIR/significant_drop_tightening.rs:140:13
|
LL | / {
LL | | let mutex = Mutex::new(1i32);
Expand All @@ -67,7 +67,7 @@ LL +
|

error: temporary with significant `Drop` can be early dropped
--> $DIR/significant_drop_tightening.rs:106:17
--> $DIR/significant_drop_tightening.rs:146:17
|
LL | / {
LL | | let mutex = Mutex::new(vec![1i32]);
Expand Down