Skip to content

Commit ea21ed7

Browse files
committed
Auto merge of rust-lang#11196 - c410-f3r:let-chain, r=xFrednet
[significant_drop_tightening] Fix rust-lang#11189 Fix rust-lang#11189 ``` changelog: FP: [`significant_drop_tightening`]: Consider tuples in drop calls ```
2 parents df3804a + f0a16bb commit ea21ed7

4 files changed

+64
-12
lines changed

clippy_lints/src/significant_drop_tightening.rs

+21-9
Original file line numberDiff line numberDiff line change
@@ -331,13 +331,13 @@ impl<'ap, 'lc, 'others, 'stmt, 'tcx> Visitor<'tcx> for StmtsChecker<'ap, 'lc, 'o
331331
apa.last_method_span = span;
332332
}
333333
},
334-
hir::StmtKind::Semi(expr) => {
335-
if has_drop(expr, &apa.first_bind_ident, self.cx) {
334+
hir::StmtKind::Semi(semi_expr) => {
335+
if has_drop(semi_expr, &apa.first_bind_ident, self.cx) {
336336
apa.has_expensive_expr_after_last_attr = false;
337337
apa.last_stmt_span = DUMMY_SP;
338338
return;
339339
}
340-
if let hir::ExprKind::MethodCall(_, _, _, span) = expr.kind {
340+
if let hir::ExprKind::MethodCall(_, _, _, span) = semi_expr.kind {
341341
apa.last_method_span = span;
342342
}
343343
},
@@ -435,14 +435,26 @@ fn has_drop(expr: &hir::Expr<'_>, first_bind_ident: &Ident, lcx: &LateContext<'_
435435
&& let Res::Def(DefKind::Fn, did) = fun_path.res
436436
&& lcx.tcx.is_diagnostic_item(sym::mem_drop, did)
437437
&& let [first_arg, ..] = args
438-
&& let hir::ExprKind::Path(hir::QPath::Resolved(_, arg_path)) = &first_arg.kind
439-
&& let [first_arg_ps, .. ] = arg_path.segments
440438
{
441-
&first_arg_ps.ident == first_bind_ident
442-
}
443-
else {
444-
false
439+
let has_ident = |local_expr: &hir::Expr<'_>| {
440+
if let hir::ExprKind::Path(hir::QPath::Resolved(_, arg_path)) = &local_expr.kind
441+
&& let [first_arg_ps, .. ] = arg_path.segments
442+
&& &first_arg_ps.ident == first_bind_ident
443+
{
444+
true
445+
}
446+
else {
447+
false
448+
}
449+
};
450+
if has_ident(first_arg) {
451+
return true;
452+
}
453+
if let hir::ExprKind::Tup(value) = &first_arg.kind && value.iter().any(has_ident) {
454+
return true;
455+
}
445456
}
457+
false
446458
}
447459

448460
fn is_inexpensive_expr(expr: &hir::Expr<'_>) -> bool {

tests/ui/significant_drop_tightening.fixed

+20
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,26 @@ pub fn issue_11160() -> bool {
5858
true
5959
}
6060

61+
pub fn issue_11189() {
62+
struct Number {
63+
pub value: u32,
64+
}
65+
66+
fn do_something() -> Result<(), ()> {
67+
let number = Mutex::new(Number { value: 1 });
68+
let number2 = Mutex::new(Number { value: 2 });
69+
let number3 = Mutex::new(Number { value: 3 });
70+
let mut lock = number.lock().unwrap();
71+
let mut lock2 = number2.lock().unwrap();
72+
let mut lock3 = number3.lock().unwrap();
73+
lock.value += 1;
74+
lock2.value += 1;
75+
lock3.value += 1;
76+
drop((lock, lock2, lock3));
77+
Ok(())
78+
}
79+
}
80+
6181
pub fn path_return_can_be_ignored() -> i32 {
6282
let mutex = Mutex::new(1);
6383
let lock = mutex.lock().unwrap();

tests/ui/significant_drop_tightening.rs

+20
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,26 @@ pub fn issue_11160() -> bool {
5757
true
5858
}
5959

60+
pub fn issue_11189() {
61+
struct Number {
62+
pub value: u32,
63+
}
64+
65+
fn do_something() -> Result<(), ()> {
66+
let number = Mutex::new(Number { value: 1 });
67+
let number2 = Mutex::new(Number { value: 2 });
68+
let number3 = Mutex::new(Number { value: 3 });
69+
let mut lock = number.lock().unwrap();
70+
let mut lock2 = number2.lock().unwrap();
71+
let mut lock3 = number3.lock().unwrap();
72+
lock.value += 1;
73+
lock2.value += 1;
74+
lock3.value += 1;
75+
drop((lock, lock2, lock3));
76+
Ok(())
77+
}
78+
}
79+
6080
pub fn path_return_can_be_ignored() -> i32 {
6181
let mutex = Mutex::new(1);
6282
let lock = mutex.lock().unwrap();

tests/ui/significant_drop_tightening.stderr

+3-3
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:86:13
26+
--> $DIR/significant_drop_tightening.rs:106: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:107:13
46+
--> $DIR/significant_drop_tightening.rs:127: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:113:17
70+
--> $DIR/significant_drop_tightening.rs:133:17
7171
|
7272
LL | / {
7373
LL | | let mutex = Mutex::new(vec![1i32]);

0 commit comments

Comments
 (0)