2023-04-20 14:37:15 +00:00
|
|
|
//@run-rustfix
|
2023-02-10 11:04:24 +00:00
|
|
|
|
|
|
|
#![warn(clippy::significant_drop_tightening)]
|
|
|
|
|
|
|
|
use std::sync::Mutex;
|
|
|
|
|
2023-02-18 12:14:41 +00:00
|
|
|
pub fn complex_return_triggers_the_lint() -> i32 {
|
|
|
|
fn foo() -> i32 {
|
|
|
|
1
|
|
|
|
}
|
|
|
|
let mutex = Mutex::new(1);
|
|
|
|
let lock = mutex.lock().unwrap();
|
|
|
|
let _ = *lock;
|
|
|
|
let _ = *lock;
|
|
|
|
drop(lock);
|
|
|
|
foo()
|
|
|
|
}
|
|
|
|
|
2023-05-14 00:45:03 +00:00
|
|
|
pub fn issue_10413() {
|
|
|
|
let mutex = Mutex::new(Some(1));
|
|
|
|
let opt = Some(1);
|
|
|
|
if opt.is_some() {
|
|
|
|
let lock = mutex.lock().unwrap();
|
|
|
|
let _ = *lock;
|
|
|
|
if opt.is_some() {
|
|
|
|
let _ = *lock;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-08 20:43:34 +00:00
|
|
|
pub fn issue_11128() {
|
|
|
|
use std::mem::drop as unlock;
|
|
|
|
|
|
|
|
struct Foo {
|
|
|
|
droppable: Option<Vec<i32>>,
|
|
|
|
mutex: Mutex<Vec<i32>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for Foo {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
if let Some(droppable) = self.droppable.take() {
|
|
|
|
let lock = self.mutex.lock().unwrap();
|
|
|
|
let idx_opt = lock.iter().copied().find(|el| Some(el) == droppable.first());
|
|
|
|
if let Some(idx) = idx_opt {
|
|
|
|
let local_droppable = vec![lock.first().copied().unwrap_or_default()];
|
|
|
|
unlock(lock);
|
|
|
|
drop(local_droppable);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-18 12:14:41 +00:00
|
|
|
pub fn path_return_can_be_ignored() -> i32 {
|
|
|
|
let mutex = Mutex::new(1);
|
|
|
|
let lock = mutex.lock().unwrap();
|
|
|
|
let rslt = *lock;
|
|
|
|
let _ = *lock;
|
|
|
|
rslt
|
|
|
|
}
|
|
|
|
|
2023-02-16 21:34:33 +00:00
|
|
|
pub fn post_bindings_can_be_ignored() {
|
|
|
|
let mutex = Mutex::new(1);
|
|
|
|
let lock = mutex.lock().unwrap();
|
|
|
|
let rslt = *lock;
|
|
|
|
let another = rslt;
|
|
|
|
let _ = another;
|
|
|
|
}
|
|
|
|
|
2023-02-10 11:04:24 +00:00
|
|
|
pub fn unnecessary_contention_with_multiple_owned_results() {
|
|
|
|
{
|
|
|
|
let mutex = Mutex::new(1i32);
|
|
|
|
let lock = mutex.lock().unwrap();
|
|
|
|
let _ = lock.abs();
|
|
|
|
let _ = lock.is_positive();
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
let mutex = Mutex::new(1i32);
|
|
|
|
let lock = mutex.lock().unwrap();
|
|
|
|
let rslt0 = lock.abs();
|
|
|
|
let rslt1 = lock.is_positive();
|
|
|
|
drop(lock);
|
|
|
|
do_heavy_computation_that_takes_time((rslt0, rslt1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn unnecessary_contention_with_single_owned_results() {
|
|
|
|
{
|
|
|
|
let mutex = Mutex::new(1i32);
|
|
|
|
let lock = mutex.lock().unwrap();
|
|
|
|
let _ = lock.abs();
|
|
|
|
}
|
|
|
|
{
|
|
|
|
let mutex = Mutex::new(vec![1i32]);
|
|
|
|
let mut lock = mutex.lock().unwrap();
|
|
|
|
lock.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
let mutex = Mutex::new(1i32);
|
|
|
|
|
|
|
|
let rslt0 = mutex.lock().unwrap().abs();
|
|
|
|
|
|
|
|
do_heavy_computation_that_takes_time(rslt0);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
let mutex = Mutex::new(vec![1i32]);
|
|
|
|
|
|
|
|
mutex.lock().unwrap().clear();
|
|
|
|
|
|
|
|
do_heavy_computation_that_takes_time(());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Marker used for illustration purposes.
|
|
|
|
pub fn do_heavy_computation_that_takes_time<T>(_: T) {}
|
|
|
|
|
|
|
|
fn main() {}
|