Skip to content

[significant_drop_tightening] Fix #11128 #11129

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

Merged
merged 2 commits into from
Jul 8, 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
10 changes: 6 additions & 4 deletions clippy_lints/src/significant_drop_tightening.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap};
use rustc_errors::Applicability;
use rustc_hir::{
self as hir,
def::{DefKind, Res},
intravisit::{walk_expr, Visitor},
};
use rustc_lint::{LateContext, LateLintPass, LintContext};
use rustc_middle::ty::{subst::GenericArgKind, Ty, TypeAndMut};
use rustc_session::{declare_tool_lint, impl_lint_pass};
use rustc_span::sym;
use rustc_span::{symbol::Ident, Span, DUMMY_SP};
use std::borrow::Cow;

Expand Down Expand Up @@ -333,7 +335,7 @@ impl<'ap, 'lc, 'others, 'stmt, 'tcx> Visitor<'tcx> for StmtsChecker<'ap, 'lc, 'o
}
},
hir::StmtKind::Semi(expr) => {
if has_drop(expr, &apa.first_bind_ident) {
if has_drop(expr, &apa.first_bind_ident, self.cx) {
apa.has_expensive_expr_after_last_attr = false;
apa.last_stmt_span = DUMMY_SP;
return;
Expand Down Expand Up @@ -430,11 +432,11 @@ fn dummy_stmt_expr<'any>(expr: &'any hir::Expr<'any>) -> hir::Stmt<'any> {
}
}

fn has_drop(expr: &hir::Expr<'_>, first_bind_ident: &Ident) -> bool {
fn has_drop(expr: &hir::Expr<'_>, first_bind_ident: &Ident, lcx: &LateContext<'_>) -> bool {
if let hir::ExprKind::Call(fun, args) = expr.kind
&& let hir::ExprKind::Path(hir::QPath::Resolved(_, fun_path)) = &fun.kind
&& let [fun_ident, ..] = fun_path.segments
&& fun_ident.ident.name == rustc_span::sym::drop
&& let Res::Def(DefKind::Fn, did) = fun_path.res
&& lcx.tcx.is_diagnostic_item(sym::mem_drop, did)
&& let [first_arg, ..] = args
&& let hir::ExprKind::Path(hir::QPath::Resolved(_, arg_path)) = &first_arg.kind
&& let [first_arg_ps, .. ] = arg_path.segments
Expand Down
23 changes: 23 additions & 0 deletions tests/ui/significant_drop_tightening.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,29 @@ pub fn issue_10413() {
}
}

pub fn issue_11128() {
use std::mem::drop as unlock;

struct Foo {
droppable: Option<Vec<i32>>,
mutex: Mutex<Vec<i32>>,
}

impl Drop for Foo {
fn drop(&mut self) {
if let Some(droppable) = self.droppable.take() {
let lock = self.mutex.lock().unwrap();
let idx_opt = lock.iter().copied().find(|el| Some(el) == droppable.first());
if let Some(idx) = idx_opt {
let local_droppable = vec![lock.first().copied().unwrap_or_default()];
unlock(lock);
drop(local_droppable);
}
}
}
}
}

pub fn path_return_can_be_ignored() -> i32 {
let mutex = Mutex::new(1);
let lock = mutex.lock().unwrap();
Expand Down
23 changes: 23 additions & 0 deletions tests/ui/significant_drop_tightening.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,29 @@ pub fn issue_10413() {
}
}

pub fn issue_11128() {
use std::mem::drop as unlock;

struct Foo {
droppable: Option<Vec<i32>>,
mutex: Mutex<Vec<i32>>,
}

impl Drop for Foo {
fn drop(&mut self) {
if let Some(droppable) = self.droppable.take() {
let lock = self.mutex.lock().unwrap();
let idx_opt = lock.iter().copied().find(|el| Some(el) == droppable.first());
if let Some(idx) = idx_opt {
let local_droppable = vec![lock.first().copied().unwrap_or_default()];
unlock(lock);
drop(local_droppable);
}
}
}
}
}

pub fn path_return_can_be_ignored() -> i32 {
let mutex = Mutex::new(1);
let lock = mutex.lock().unwrap();
Expand Down
6 changes: 3 additions & 3 deletions tests/ui/significant_drop_tightening.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ LL + drop(lock);
|

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

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

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