Skip to content

[significant_drop_tightening] Fix #11189 #11196

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 1 commit into from
Jul 22, 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
30 changes: 21 additions & 9 deletions clippy_lints/src/significant_drop_tightening.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,13 +331,13 @@ impl<'ap, 'lc, 'others, 'stmt, 'tcx> Visitor<'tcx> for StmtsChecker<'ap, 'lc, 'o
apa.last_method_span = span;
}
},
hir::StmtKind::Semi(expr) => {
if has_drop(expr, &apa.first_bind_ident, self.cx) {
hir::StmtKind::Semi(semi_expr) => {
if has_drop(semi_expr, &apa.first_bind_ident, self.cx) {
apa.has_expensive_expr_after_last_attr = false;
apa.last_stmt_span = DUMMY_SP;
return;
}
if let hir::ExprKind::MethodCall(_, _, _, span) = expr.kind {
if let hir::ExprKind::MethodCall(_, _, _, span) = semi_expr.kind {
apa.last_method_span = span;
}
},
Expand Down Expand Up @@ -435,14 +435,26 @@ fn has_drop(expr: &hir::Expr<'_>, first_bind_ident: &Ident, lcx: &LateContext<'_
&& 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
{
&first_arg_ps.ident == first_bind_ident
}
else {
false
let has_ident = |local_expr: &hir::Expr<'_>| {
if let hir::ExprKind::Path(hir::QPath::Resolved(_, arg_path)) = &local_expr.kind
&& let [first_arg_ps, .. ] = arg_path.segments
&& &first_arg_ps.ident == first_bind_ident
{
true
}
else {
false
}
};
if has_ident(first_arg) {
return true;
}
if let hir::ExprKind::Tup(value) = &first_arg.kind && value.iter().any(has_ident) {
return true;
}
}
false
}

fn is_inexpensive_expr(expr: &hir::Expr<'_>) -> bool {
Expand Down
20 changes: 20 additions & 0 deletions tests/ui/significant_drop_tightening.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,26 @@ pub fn issue_11160() -> bool {
true
}

pub fn issue_11189() {
struct Number {
pub value: u32,
}

fn do_something() -> Result<(), ()> {
let number = Mutex::new(Number { value: 1 });
let number2 = Mutex::new(Number { value: 2 });
let number3 = Mutex::new(Number { value: 3 });
let mut lock = number.lock().unwrap();
let mut lock2 = number2.lock().unwrap();
let mut lock3 = number3.lock().unwrap();
lock.value += 1;
lock2.value += 1;
lock3.value += 1;
drop((lock, lock2, lock3));
Ok(())
}
}

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

pub fn issue_11189() {
struct Number {
pub value: u32,
}

fn do_something() -> Result<(), ()> {
let number = Mutex::new(Number { value: 1 });
let number2 = Mutex::new(Number { value: 2 });
let number3 = Mutex::new(Number { value: 3 });
let mut lock = number.lock().unwrap();
let mut lock2 = number2.lock().unwrap();
let mut lock3 = number3.lock().unwrap();
lock.value += 1;
lock2.value += 1;
lock3.value += 1;
drop((lock, lock2, lock3));
Ok(())
}
}

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:86:13
--> $DIR/significant_drop_tightening.rs:106: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:107:13
--> $DIR/significant_drop_tightening.rs:127: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:113:17
--> $DIR/significant_drop_tightening.rs:133:17
|
LL | / {
LL | | let mutex = Mutex::new(vec![1i32]);
Expand Down