Skip to content

Commit 1f82f6d

Browse files
committed
[significant_drop_tightening] Fix rust-lang#11160
1 parent 1d33469 commit 1f82f6d

4 files changed

+24
-7
lines changed

clippy_lints/src/significant_drop_tightening.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use clippy_utils::diagnostics::span_lint_and_then;
22
use clippy_utils::source::{indent_of, snippet};
3-
use clippy_utils::{expr_or_init, get_attr, path_to_local};
3+
use clippy_utils::{expr_or_init, get_attr, path_to_local, peel_hir_expr_unary};
44
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap};
55
use rustc_errors::Applicability;
66
use rustc_hir::def::{DefKind, Res};
@@ -236,7 +236,7 @@ impl<'ap, 'lc, 'others, 'stmt, 'tcx> StmtsChecker<'ap, 'lc, 'others, 'stmt, 'tcx
236236

237237
fn manage_has_expensive_expr_after_last_attr(&mut self) {
238238
let has_expensive_stmt = match self.ap.curr_stmt.kind {
239-
hir::StmtKind::Expr(expr) if !is_expensive_expr(expr) => false,
239+
hir::StmtKind::Expr(expr) if is_inexpensive_expr(expr) => false,
240240
hir::StmtKind::Local(local) if let Some(expr) = local.init
241241
&& let hir::ExprKind::Path(_) = expr.kind => false,
242242
_ => true
@@ -445,6 +445,9 @@ fn has_drop(expr: &hir::Expr<'_>, first_bind_ident: &Ident, lcx: &LateContext<'_
445445
}
446446
}
447447

448-
fn is_expensive_expr(expr: &hir::Expr<'_>) -> bool {
449-
!matches!(expr.kind, hir::ExprKind::Path(_))
448+
fn is_inexpensive_expr(expr: &hir::Expr<'_>) -> bool {
449+
let actual = peel_hir_expr_unary(expr).0;
450+
let is_path = matches!(actual.kind, hir::ExprKind::Path(_));
451+
let is_lit = matches!(actual.kind, hir::ExprKind::Lit(_));
452+
is_path || is_lit
450453
}

tests/ui/significant_drop_tightening.fixed

+7
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,13 @@ pub fn issue_11128() {
5151
}
5252
}
5353

54+
pub fn issue_11160() -> bool {
55+
let mutex = Mutex::new(1i32);
56+
let lock = mutex.lock().unwrap();
57+
let _ = lock.abs();
58+
true
59+
}
60+
5461
pub fn path_return_can_be_ignored() -> i32 {
5562
let mutex = Mutex::new(1);
5663
let lock = mutex.lock().unwrap();

tests/ui/significant_drop_tightening.rs

+7
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ pub fn issue_11128() {
5050
}
5151
}
5252

53+
pub fn issue_11160() -> bool {
54+
let mutex = Mutex::new(1i32);
55+
let lock = mutex.lock().unwrap();
56+
let _ = lock.abs();
57+
true
58+
}
59+
5360
pub fn path_return_can_be_ignored() -> i32 {
5461
let mutex = Mutex::new(1);
5562
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:79:13
26+
--> $DIR/significant_drop_tightening.rs:86: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:100:13
46+
--> $DIR/significant_drop_tightening.rs:107: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:106:17
70+
--> $DIR/significant_drop_tightening.rs:113:17
7171
|
7272
LL | / {
7373
LL | | let mutex = Mutex::new(vec![1i32]);

0 commit comments

Comments
 (0)