Skip to content

Commit 6ae065f

Browse files
committed
Auto merge of #11129 - c410-f3r:lock-1, r=Manishearth
[significant_drop_tightening] Fix #11128 Fix #11128 ``` changelog: [`significant_drop_tightening`]: Consider manual alias of the `drop` function. ```
2 parents 8e261c0 + 6384221 commit 6ae065f

4 files changed

+55
-7
lines changed

clippy_lints/src/significant_drop_tightening.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap};
77
use rustc_errors::Applicability;
88
use rustc_hir::{
99
self as hir,
10+
def::{DefKind, Res},
1011
intravisit::{walk_expr, Visitor},
1112
};
1213
use rustc_lint::{LateContext, LateLintPass, LintContext};
1314
use rustc_middle::ty::{subst::GenericArgKind, Ty, TypeAndMut};
1415
use rustc_session::{declare_tool_lint, impl_lint_pass};
16+
use rustc_span::sym;
1517
use rustc_span::{symbol::Ident, Span, DUMMY_SP};
1618
use std::borrow::Cow;
1719

@@ -333,7 +335,7 @@ impl<'ap, 'lc, 'others, 'stmt, 'tcx> Visitor<'tcx> for StmtsChecker<'ap, 'lc, 'o
333335
}
334336
},
335337
hir::StmtKind::Semi(expr) => {
336-
if has_drop(expr, &apa.first_bind_ident) {
338+
if has_drop(expr, &apa.first_bind_ident, self.cx) {
337339
apa.has_expensive_expr_after_last_attr = false;
338340
apa.last_stmt_span = DUMMY_SP;
339341
return;
@@ -430,11 +432,11 @@ fn dummy_stmt_expr<'any>(expr: &'any hir::Expr<'any>) -> hir::Stmt<'any> {
430432
}
431433
}
432434

433-
fn has_drop(expr: &hir::Expr<'_>, first_bind_ident: &Ident) -> bool {
435+
fn has_drop(expr: &hir::Expr<'_>, first_bind_ident: &Ident, lcx: &LateContext<'_>) -> bool {
434436
if let hir::ExprKind::Call(fun, args) = expr.kind
435437
&& let hir::ExprKind::Path(hir::QPath::Resolved(_, fun_path)) = &fun.kind
436-
&& let [fun_ident, ..] = fun_path.segments
437-
&& fun_ident.ident.name == rustc_span::sym::drop
438+
&& let Res::Def(DefKind::Fn, did) = fun_path.res
439+
&& lcx.tcx.is_diagnostic_item(sym::mem_drop, did)
438440
&& let [first_arg, ..] = args
439441
&& let hir::ExprKind::Path(hir::QPath::Resolved(_, arg_path)) = &first_arg.kind
440442
&& let [first_arg_ps, .. ] = arg_path.segments

tests/ui/significant_drop_tightening.fixed

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,29 @@ pub fn issue_10413() {
2828
}
2929
}
3030

31+
pub fn issue_11128() {
32+
use std::mem::drop as unlock;
33+
34+
struct Foo {
35+
droppable: Option<Vec<i32>>,
36+
mutex: Mutex<Vec<i32>>,
37+
}
38+
39+
impl Drop for Foo {
40+
fn drop(&mut self) {
41+
if let Some(droppable) = self.droppable.take() {
42+
let lock = self.mutex.lock().unwrap();
43+
let idx_opt = lock.iter().copied().find(|el| Some(el) == droppable.first());
44+
if let Some(idx) = idx_opt {
45+
let local_droppable = vec![lock.first().copied().unwrap_or_default()];
46+
unlock(lock);
47+
drop(local_droppable);
48+
}
49+
}
50+
}
51+
}
52+
}
53+
3154
pub fn path_return_can_be_ignored() -> i32 {
3255
let mutex = Mutex::new(1);
3356
let lock = mutex.lock().unwrap();

tests/ui/significant_drop_tightening.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,29 @@ pub fn issue_10413() {
2727
}
2828
}
2929

30+
pub fn issue_11128() {
31+
use std::mem::drop as unlock;
32+
33+
struct Foo {
34+
droppable: Option<Vec<i32>>,
35+
mutex: Mutex<Vec<i32>>,
36+
}
37+
38+
impl Drop for Foo {
39+
fn drop(&mut self) {
40+
if let Some(droppable) = self.droppable.take() {
41+
let lock = self.mutex.lock().unwrap();
42+
let idx_opt = lock.iter().copied().find(|el| Some(el) == droppable.first());
43+
if let Some(idx) = idx_opt {
44+
let local_droppable = vec![lock.first().copied().unwrap_or_default()];
45+
unlock(lock);
46+
drop(local_droppable);
47+
}
48+
}
49+
}
50+
}
51+
}
52+
3053
pub fn path_return_can_be_ignored() -> i32 {
3154
let mutex = Mutex::new(1);
3255
let lock = mutex.lock().unwrap();

tests/ui/significant_drop_tightening.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ LL + drop(lock);
2323
|
2424

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

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

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

0 commit comments

Comments
 (0)