Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[significant_drop_tightening] Ignore inexpensive statements #10363

Merged
merged 1 commit into from
Feb 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions clippy_lints/src/significant_drop_tightening.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,18 @@ pub struct SignificantDropTightening<'tcx> {
}

impl<'tcx> SignificantDropTightening<'tcx> {
/// Searches for at least one statement that could slow down the release of a significant drop.
fn at_least_one_stmt_is_expensive(stmts: &[hir::Stmt<'_>]) -> bool {
for stmt in stmts {
match stmt.kind {
hir::StmtKind::Local(local) if let Some(expr) = local.init
&& let hir::ExprKind::Path(_) = expr.kind => {},
_ => return true
};
}
false
}

/// Verifies if the expression is of type `drop(some_lock_path)` to assert that the temporary
/// is already being dropped before the end of its scope.
fn has_drop(expr: &'tcx hir::Expr<'_>, init_bind_ident: Ident) -> bool {
Expand Down Expand Up @@ -198,13 +210,15 @@ impl<'tcx> LateLintPass<'tcx> for SignificantDropTightening<'tcx> {
}
self.modify_sdap_if_sig_drop_exists(cx, expr, idx, &mut sdap, stmt, |_| {});
},
_ => continue
_ => {}
};
}
if sdap.number_of_stmts > 1 && {
let last_stmts_idx = block.stmts.len().wrapping_sub(1);
sdap.last_use_stmt_idx != last_stmts_idx
} {
let stmts_after_last_use = sdap
.last_use_stmt_idx
.checked_add(1)
.and_then(|idx| block.stmts.get(idx..))
.unwrap_or_default();
if sdap.number_of_stmts > 1 && Self::at_least_one_stmt_is_expensive(stmts_after_last_use) {
span_lint_and_then(
cx,
SIGNIFICANT_DROP_TIGHTENING,
Expand Down
8 changes: 8 additions & 0 deletions tests/ui/significant_drop_tightening.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@

use std::sync::Mutex;

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;
}

pub fn unnecessary_contention_with_multiple_owned_results() {
{
let mutex = Mutex::new(1i32);
Expand Down
8 changes: 8 additions & 0 deletions tests/ui/significant_drop_tightening.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@

use std::sync::Mutex;

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;
}

pub fn unnecessary_contention_with_multiple_owned_results() {
{
let mutex = Mutex::new(1i32);
Expand Down
6 changes: 3 additions & 3 deletions tests/ui/significant_drop_tightening.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: temporary with significant `Drop` can be early dropped
--> $DIR/significant_drop_tightening.rs:17:13
--> $DIR/significant_drop_tightening.rs:25:13
|
LL | / {
LL | | let mutex = Mutex::new(1i32);
Expand All @@ -20,7 +20,7 @@ LL + drop(lock);
|

error: temporary with significant `Drop` can be early dropped
--> $DIR/significant_drop_tightening.rs:38:13
--> $DIR/significant_drop_tightening.rs:46:13
|
LL | / {
LL | | let mutex = Mutex::new(1i32);
Expand All @@ -44,7 +44,7 @@ LL +
|

error: temporary with significant `Drop` can be early dropped
--> $DIR/significant_drop_tightening.rs:44:17
--> $DIR/significant_drop_tightening.rs:52:17
|
LL | / {
LL | | let mutex = Mutex::new(vec![1i32]);
Expand Down