Skip to content

Commit ce88c84

Browse files
committed
[significant_drop_tightening] Ignore inexpensive statements
1 parent dfe23dc commit ce88c84

4 files changed

+36
-7
lines changed

clippy_lints/src/significant_drop_tightening.rs

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

6363
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+
6475
/// Verifies if the expression is of type `drop(some_lock_path)` to assert that the temporary
6576
/// is already being dropped before the end of its scope.
6677
fn has_drop(expr: &'tcx hir::Expr<'_>, init_bind_ident: Ident) -> bool {
@@ -201,10 +212,12 @@ impl<'tcx> LateLintPass<'tcx> for SignificantDropTightening<'tcx> {
201212
_ => continue
202213
};
203214
}
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) {
208221
span_lint_and_then(
209222
cx,
210223
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)