File tree 4 files changed +36
-7
lines changed 4 files changed +36
-7
lines changed Original file line number Diff line number Diff line change @@ -61,6 +61,17 @@ pub struct SignificantDropTightening<'tcx> {
61
61
}
62
62
63
63
impl < ' tcx > SignificantDropTightening < ' tcx > {
64
+ fn at_least_one_stmt_is_expensive ( stmts : & [ hir:: Stmt < ' _ > ] ) -> bool {
65
+ for stmt in stmts {
66
+ match stmt. kind {
67
+ hir:: StmtKind :: Local ( local) if let Some ( expr) = local. init
68
+ && let hir:: ExprKind :: Path ( _) = expr. kind => { } ,
69
+ _ => return true
70
+ } ;
71
+ }
72
+ false
73
+ }
74
+
64
75
/// Verifies if the expression is of type `drop(some_lock_path)` to assert that the temporary
65
76
/// is already being dropped before the end of its scope.
66
77
fn has_drop ( expr : & ' tcx hir:: Expr < ' _ > , init_bind_ident : Ident ) -> bool {
@@ -201,10 +212,12 @@ impl<'tcx> LateLintPass<'tcx> for SignificantDropTightening<'tcx> {
201
212
_ => continue
202
213
} ;
203
214
}
204
- if sdap. number_of_stmts > 1 && {
205
- let last_stmts_idx = block. stmts . len ( ) . wrapping_sub ( 1 ) ;
206
- sdap. last_use_stmt_idx != last_stmts_idx
207
- } {
215
+ let stmts_after_last_use = sdap
216
+ . last_use_stmt_idx
217
+ . checked_add ( 1 )
218
+ . and_then ( |idx| block. stmts . get ( idx..) )
219
+ . unwrap_or_default ( ) ;
220
+ if sdap. number_of_stmts > 1 && Self :: at_least_one_stmt_is_expensive ( stmts_after_last_use) {
208
221
span_lint_and_then (
209
222
cx,
210
223
SIGNIFICANT_DROP_TIGHTENING ,
Original file line number Diff line number Diff line change 4
4
5
5
use std::sync::Mutex;
6
6
7
+ pub fn post_bindings_can_be_ignored() {
8
+ let mutex = Mutex::new(1);
9
+ let lock = mutex.lock().unwrap();
10
+ let rslt = *lock;
11
+ let another = rslt;
12
+ let _ = another;
13
+ }
14
+
7
15
pub fn unnecessary_contention_with_multiple_owned_results() {
8
16
{
9
17
let mutex = Mutex::new(1i32);
Original file line number Diff line number Diff line change 4
4
5
5
use std:: sync:: Mutex ;
6
6
7
+ pub fn post_bindings_can_be_ignored ( ) {
8
+ let mutex = Mutex :: new ( 1 ) ;
9
+ let lock = mutex. lock ( ) . unwrap ( ) ;
10
+ let rslt = * lock;
11
+ let another = rslt;
12
+ let _ = another;
13
+ }
14
+
7
15
pub fn unnecessary_contention_with_multiple_owned_results ( ) {
8
16
{
9
17
let mutex = Mutex :: new ( 1i32 ) ;
Original file line number Diff line number Diff line change 1
1
error: temporary with significant `Drop` can be early dropped
2
- --> $DIR/significant_drop_tightening.rs:17 :13
2
+ --> $DIR/significant_drop_tightening.rs:25 :13
3
3
|
4
4
LL | / {
5
5
LL | | let mutex = Mutex::new(1i32);
@@ -20,7 +20,7 @@ LL + drop(lock);
20
20
|
21
21
22
22
error: temporary with significant `Drop` can be early dropped
23
- --> $DIR/significant_drop_tightening.rs:38 :13
23
+ --> $DIR/significant_drop_tightening.rs:46 :13
24
24
|
25
25
LL | / {
26
26
LL | | let mutex = Mutex::new(1i32);
44
44
|
45
45
46
46
error: temporary with significant `Drop` can be early dropped
47
- --> $DIR/significant_drop_tightening.rs:44 :17
47
+ --> $DIR/significant_drop_tightening.rs:52 :17
48
48
|
49
49
LL | / {
50
50
LL | | let mutex = Mutex::new(vec![1i32]);
You can’t perform that action at this time.
0 commit comments