Skip to content

Commit 5ac1805

Browse files
authored
chore: use multipart_suggestion in significant_drop_tightening lint (rust-lang#13823)
This addresses rust-lang/rust-clippy#13099 for the significant_drop_tightening lint. changelog: none
2 parents bfb87b9 + db7e453 commit 5ac1805

4 files changed

+153
-23
lines changed

clippy_lints/src/significant_drop_tightening.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -99,16 +99,10 @@ impl<'tcx> LateLintPass<'tcx> for SignificantDropTightening<'tcx> {
9999
snippet(cx, apa.last_bind_ident.span, ".."),
100100
)
101101
};
102-
diag.span_suggestion_verbose(
103-
apa.first_stmt_span,
102+
103+
diag.multipart_suggestion_verbose(
104104
"merge the temporary construction with its single usage",
105-
stmt,
106-
Applicability::MaybeIncorrect,
107-
);
108-
diag.span_suggestion(
109-
apa.last_stmt_span,
110-
"remove separated single usage",
111-
"",
105+
vec![(apa.first_stmt_span, stmt), (apa.last_stmt_span, String::new())],
112106
Applicability::MaybeIncorrect,
113107
);
114108
},
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
#![warn(clippy::significant_drop_tightening)]
2+
3+
use std::sync::Mutex;
4+
5+
pub fn complex_return_triggers_the_lint() -> i32 {
6+
fn foo() -> i32 {
7+
1
8+
}
9+
let mutex = Mutex::new(1);
10+
let lock = mutex.lock().unwrap();
11+
let _ = *lock;
12+
let _ = *lock;
13+
drop(lock);
14+
foo()
15+
}
16+
17+
pub fn issue_10413() {
18+
let mutex = Mutex::new(Some(1));
19+
let opt = Some(1);
20+
if opt.is_some() {
21+
let lock = mutex.lock().unwrap();
22+
let _ = *lock;
23+
if opt.is_some() {
24+
let _ = *lock;
25+
}
26+
}
27+
}
28+
29+
pub fn issue_11128() {
30+
use std::mem::drop as unlock;
31+
32+
struct Foo {
33+
droppable: Option<Vec<i32>>,
34+
mutex: Mutex<Vec<i32>>,
35+
}
36+
37+
impl Drop for Foo {
38+
fn drop(&mut self) {
39+
if let Some(droppable) = self.droppable.take() {
40+
let lock = self.mutex.lock().unwrap();
41+
let idx_opt = lock.iter().copied().find(|el| Some(el) == droppable.first());
42+
if let Some(idx) = idx_opt {
43+
let local_droppable = vec![lock.first().copied().unwrap_or_default()];
44+
unlock(lock);
45+
drop(local_droppable);
46+
}
47+
}
48+
}
49+
}
50+
}
51+
52+
pub fn issue_11160() -> bool {
53+
let mutex = Mutex::new(1i32);
54+
let lock = mutex.lock().unwrap();
55+
let _ = lock.abs();
56+
true
57+
}
58+
59+
pub fn issue_11189() {
60+
struct Number {
61+
pub value: u32,
62+
}
63+
64+
fn do_something() -> Result<(), ()> {
65+
let number = Mutex::new(Number { value: 1 });
66+
let number2 = Mutex::new(Number { value: 2 });
67+
let number3 = Mutex::new(Number { value: 3 });
68+
let mut lock = number.lock().unwrap();
69+
let mut lock2 = number2.lock().unwrap();
70+
let mut lock3 = number3.lock().unwrap();
71+
lock.value += 1;
72+
lock2.value += 1;
73+
lock3.value += 1;
74+
drop((lock, lock2, lock3));
75+
Ok(())
76+
}
77+
}
78+
79+
pub fn path_return_can_be_ignored() -> i32 {
80+
let mutex = Mutex::new(1);
81+
let lock = mutex.lock().unwrap();
82+
let rslt = *lock;
83+
let _ = *lock;
84+
rslt
85+
}
86+
87+
pub fn post_bindings_can_be_ignored() {
88+
let mutex = Mutex::new(1);
89+
let lock = mutex.lock().unwrap();
90+
let rslt = *lock;
91+
let another = rslt;
92+
let _ = another;
93+
}
94+
95+
pub fn unnecessary_contention_with_multiple_owned_results() {
96+
{
97+
let mutex = Mutex::new(1i32);
98+
let lock = mutex.lock().unwrap();
99+
let _ = lock.abs();
100+
let _ = lock.is_positive();
101+
}
102+
103+
{
104+
let mutex = Mutex::new(1i32);
105+
let lock = mutex.lock().unwrap();
106+
let rslt0 = lock.abs();
107+
let rslt1 = lock.is_positive();
108+
drop(lock);
109+
do_heavy_computation_that_takes_time((rslt0, rslt1));
110+
}
111+
}
112+
113+
pub fn unnecessary_contention_with_single_owned_results() {
114+
{
115+
let mutex = Mutex::new(1i32);
116+
let lock = mutex.lock().unwrap();
117+
let _ = lock.abs();
118+
}
119+
{
120+
let mutex = Mutex::new(vec![1i32]);
121+
let mut lock = mutex.lock().unwrap();
122+
lock.clear();
123+
}
124+
125+
{
126+
let mutex = Mutex::new(1i32);
127+
128+
let rslt0 = mutex.lock().unwrap().abs();
129+
130+
do_heavy_computation_that_takes_time(rslt0);
131+
}
132+
{
133+
let mutex = Mutex::new(vec![1i32]);
134+
135+
mutex.lock().unwrap().clear();
136+
137+
do_heavy_computation_that_takes_time(());
138+
}
139+
}
140+
141+
// Marker used for illustration purposes.
142+
pub fn do_heavy_computation_that_takes_time<T>(_: T) {}
143+
144+
fn main() {}

tests/ui/significant_drop_tightening.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
#![warn(clippy::significant_drop_tightening)]
22

3-
//@no-rustfix: need to change the suggestion to a multipart suggestion
4-
53
use std::sync::Mutex;
64

75
pub fn complex_return_triggers_the_lint() -> i32 {

tests/ui/significant_drop_tightening.stderr

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: temporary with significant `Drop` can be early dropped
2-
--> tests/ui/significant_drop_tightening.rs:12:9
2+
--> tests/ui/significant_drop_tightening.rs:10:9
33
|
44
LL | pub fn complex_return_triggers_the_lint() -> i32 {
55
| __________________________________________________-
@@ -24,7 +24,7 @@ LL + drop(lock);
2424
|
2525

2626
error: temporary with significant `Drop` can be early dropped
27-
--> tests/ui/significant_drop_tightening.rs:106:13
27+
--> tests/ui/significant_drop_tightening.rs:104:13
2828
|
2929
LL | / {
3030
LL | | let mutex = Mutex::new(1i32);
@@ -44,7 +44,7 @@ LL + drop(lock);
4444
|
4545

4646
error: temporary with significant `Drop` can be early dropped
47-
--> tests/ui/significant_drop_tightening.rs:127:13
47+
--> tests/ui/significant_drop_tightening.rs:125:13
4848
|
4949
LL | / {
5050
LL | | let mutex = Mutex::new(1i32);
@@ -60,14 +60,11 @@ help: merge the temporary construction with its single usage
6060
|
6161
LL ~
6262
LL + let rslt0 = mutex.lock().unwrap().abs();
63-
|
64-
help: remove separated single usage
65-
|
66-
LL - let rslt0 = lock.abs();
63+
LL ~
6764
|
6865

6966
error: temporary with significant `Drop` can be early dropped
70-
--> tests/ui/significant_drop_tightening.rs:133:17
67+
--> tests/ui/significant_drop_tightening.rs:131:17
7168
|
7269
LL | / {
7370
LL | | let mutex = Mutex::new(vec![1i32]);
@@ -83,10 +80,7 @@ help: merge the temporary construction with its single usage
8380
|
8481
LL ~
8582
LL + mutex.lock().unwrap().clear();
86-
|
87-
help: remove separated single usage
88-
|
89-
LL - lock.clear();
83+
LL ~
9084
|
9185

9286
error: aborting due to 4 previous errors

0 commit comments

Comments
 (0)