Skip to content

Commit e1da002

Browse files
committed
Auto merge of #10363 - c410-f3r:lock-1, r=xFrednet
[significant_drop_tightening] Ignore inexpensive statements Not all statements that follow the last use of a lock guard are expensive and can therefore be ignored by the lint. ```rust pub fn foo() -> i32 { let mutex = Mutex::new(1); let lock = mutex.lock().unwrap(); let rslt = *lock; let another = rslt; another } ``` --- changelog: [`significant_drop_tightening`]: No longer lints for inexpensive statements after the lock guard [#10363](#10363) <!-- changelog_checked -->
2 parents 6444621 + 747f81e commit e1da002

4 files changed

+38
-8
lines changed

clippy_lints/src/significant_drop_tightening.rs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,18 @@ pub struct SignificantDropTightening<'tcx> {
6161
}
6262

6363
impl<'tcx> SignificantDropTightening<'tcx> {
64+
/// Searches for at least one statement that could slow down the release of a significant drop.
65+
fn at_least_one_stmt_is_expensive(stmts: &[hir::Stmt<'_>]) -> bool {
66+
for stmt in stmts {
67+
match stmt.kind {
68+
hir::StmtKind::Local(local) if let Some(expr) = local.init
69+
&& let hir::ExprKind::Path(_) = expr.kind => {},
70+
_ => return true
71+
};
72+
}
73+
false
74+
}
75+
6476
/// Verifies if the expression is of type `drop(some_lock_path)` to assert that the temporary
6577
/// is already being dropped before the end of its scope.
6678
fn has_drop(expr: &'tcx hir::Expr<'_>, init_bind_ident: Ident) -> bool {
@@ -198,13 +210,15 @@ impl<'tcx> LateLintPass<'tcx> for SignificantDropTightening<'tcx> {
198210
}
199211
self.modify_sdap_if_sig_drop_exists(cx, expr, idx, &mut sdap, stmt, |_| {});
200212
},
201-
_ => continue
213+
_ => {}
202214
};
203215
}
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-
} {
216+
let stmts_after_last_use = sdap
217+
.last_use_stmt_idx
218+
.checked_add(1)
219+
.and_then(|idx| block.stmts.get(idx..))
220+
.unwrap_or_default();
221+
if sdap.number_of_stmts > 1 && Self::at_least_one_stmt_is_expensive(stmts_after_last_use) {
208222
span_lint_and_then(
209223
cx,
210224
SIGNIFICANT_DROP_TIGHTENING,

tests/ui/significant_drop_tightening.fixed

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@
44

55
use std::sync::Mutex;
66

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+
715
pub fn unnecessary_contention_with_multiple_owned_results() {
816
{
917
let mutex = Mutex::new(1i32);

tests/ui/significant_drop_tightening.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@
44

55
use std::sync::Mutex;
66

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+
715
pub fn unnecessary_contention_with_multiple_owned_results() {
816
{
917
let mutex = Mutex::new(1i32);

tests/ui/significant_drop_tightening.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
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
33
|
44
LL | / {
55
LL | | let mutex = Mutex::new(1i32);
@@ -20,7 +20,7 @@ LL + drop(lock);
2020
|
2121

2222
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
2424
|
2525
LL | / {
2626
LL | | let mutex = Mutex::new(1i32);
@@ -44,7 +44,7 @@ LL +
4444
|
4545

4646
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
4848
|
4949
LL | / {
5050
LL | | let mutex = Mutex::new(vec![1i32]);

0 commit comments

Comments
 (0)