Skip to content

Commit 01e131f

Browse files
committed
add test from lintcheck
1 parent e4797f5 commit 01e131f

File tree

3 files changed

+106
-2
lines changed

3 files changed

+106
-2
lines changed

tests/ui/significant_drop_tightening.fixed

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
#![warn(clippy::significant_drop_tightening)]
1+
2+
drop(inner);#![warn(clippy::significant_drop_tightening)]
23

34
use std::sync::Mutex;
45

@@ -142,6 +143,48 @@ pub fn unnecessary_contention_with_single_owned_results() {
142143
}
143144
}
144145

146+
fn issue_15574() {
147+
mod lintcheck1 {
148+
struct Inner<T> {
149+
is_disconnected: bool,
150+
receivers: Receivers<T>,
151+
}
152+
153+
struct Receivers<T>(Vec<T>);
154+
155+
impl<T> Receivers<T> {
156+
fn try_select(&mut self) -> Option<T> {
157+
todo!()
158+
}
159+
}
160+
161+
struct Operation {
162+
packet: *mut (),
163+
}
164+
165+
struct Token {
166+
zero: Zero,
167+
}
168+
struct Zero(*mut ());
169+
170+
fn start_send(inner: &std::sync::Mutex<Inner<Operation>>, token: &mut Token) -> bool {
171+
let mut inner = inner.lock().unwrap();
172+
//~^ significant_drop_tightening
173+
174+
// If there's a waiting receiver, pair up with it.
175+
if let Some(operation) = inner.receivers.try_select() {
176+
token.zero.0 = operation.packet;
177+
true
178+
} else if inner.is_disconnected {
179+
token.zero.0 = std::ptr::null_mut();
180+
true
181+
} else {
182+
false
183+
}
184+
}
185+
}
186+
}
187+
145188
// Marker used for illustration purposes.
146189
pub fn do_heavy_computation_that_takes_time<T>(_: T) {}
147190

tests/ui/significant_drop_tightening.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,48 @@ pub fn unnecessary_contention_with_single_owned_results() {
138138
}
139139
}
140140

141+
fn issue_15574() {
142+
mod lintcheck1 {
143+
struct Inner<T> {
144+
is_disconnected: bool,
145+
receivers: Receivers<T>,
146+
}
147+
148+
struct Receivers<T>(Vec<T>);
149+
150+
impl<T> Receivers<T> {
151+
fn try_select(&mut self) -> Option<T> {
152+
todo!()
153+
}
154+
}
155+
156+
struct Operation {
157+
packet: *mut (),
158+
}
159+
160+
struct Token {
161+
zero: Zero,
162+
}
163+
struct Zero(*mut ());
164+
165+
fn start_send(inner: &std::sync::Mutex<Inner<Operation>>, token: &mut Token) -> bool {
166+
let mut inner = inner.lock().unwrap();
167+
//~^ significant_drop_tightening
168+
169+
// If there's a waiting receiver, pair up with it.
170+
if let Some(operation) = inner.receivers.try_select() {
171+
token.zero.0 = operation.packet;
172+
true
173+
} else if inner.is_disconnected {
174+
token.zero.0 = std::ptr::null_mut();
175+
true
176+
} else {
177+
false
178+
}
179+
}
180+
}
181+
}
182+
141183
// Marker used for illustration purposes.
142184
pub fn do_heavy_computation_that_takes_time<T>(_: T) {}
143185

tests/ui/significant_drop_tightening.stderr

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,5 +83,24 @@ LL |
8383
LL ~
8484
|
8585

86-
error: aborting due to 4 previous errors
86+
error: temporary with significant `Drop` can be early dropped
87+
--> tests/ui/significant_drop_tightening.rs:166:21
88+
|
89+
LL | fn start_send(inner: &std::sync::Mutex<Inner<Operation>>, token: &mut Token) -> bool {
90+
| ______________________________________________________________________________________________-
91+
LL | | let mut inner = inner.lock().unwrap();
92+
| | ^^^^^
93+
... |
94+
LL | | }
95+
| |_________- temporary `inner` is currently being dropped at the end of its contained scope
96+
|
97+
= note: this might lead to unnecessary resource contention
98+
help: drop the temporary after the end of its last usage
99+
--> /home/ada4a/dev/minor/clippy/tests/clippy.toml:1:1
100+
|
101+
LL +
102+
LL ~ drop(inner);# default config for tests, overrides clippy.toml at the project root
103+
|
104+
105+
error: aborting due to 5 previous errors
87106

0 commit comments

Comments
 (0)