Skip to content

Commit 0f6ad32

Browse files
committed
Do not trigger if_let_mutex strating from Edition 2024
1 parent b829d53 commit 0f6ad32

File tree

3 files changed

+29
-7
lines changed

3 files changed

+29
-7
lines changed

clippy_lints/src/if_let_mutex.rs

+11
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,20 @@ use rustc_errors::Diag;
77
use rustc_hir::{Expr, ExprKind};
88
use rustc_lint::{LateContext, LateLintPass};
99
use rustc_session::declare_lint_pass;
10+
use rustc_span::edition::Edition::Edition2024;
1011
use rustc_span::sym;
1112

1213
declare_clippy_lint! {
1314
/// ### What it does
1415
/// Checks for `Mutex::lock` calls in `if let` expression
1516
/// with lock calls in any of the else blocks.
1617
///
18+
/// ### Disabled starting in Edition 2024
19+
/// `Mutex::lock` is effectively disabled starting in
20+
/// Edition 2024 as `if let ... else` scoping was reworked
21+
/// such that this is no longer an issue. See
22+
/// [Proposal: stabilize if_let_rescope for Edition 2024](https://github.com/rust-lang/rust/issues/131154)
23+
///
1724
/// ### Why is this bad?
1825
/// The Mutex lock remains held for the whole
1926
/// `if let ... else` block and deadlocks.
@@ -45,6 +52,10 @@ declare_lint_pass!(IfLetMutex => [IF_LET_MUTEX]);
4552

4653
impl<'tcx> LateLintPass<'tcx> for IfLetMutex {
4754
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
55+
if cx.tcx.sess.edition() >= Edition2024 {
56+
return;
57+
}
58+
4859
if let Some(higher::IfLet {
4960
let_expr,
5061
if_then,

tests/ui/if_let_mutex.stderr renamed to tests/ui/if_let_mutex.edition2021.stderr

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
error: calling `Mutex::lock` inside the scope of another `Mutex::lock` causes a deadlock
2-
--> tests/ui/if_let_mutex.rs:11:5
2+
--> tests/ui/if_let_mutex.rs:16:5
33
|
44
LL | if let Err(locked) = m.lock() {
55
| ^ - this Mutex will remain locked for the entire `if let`-block...
66
| _____|
77
| |
88
LL | |
9+
LL | | // ERROR: calling `Mutex::lock` inside the scope of another `Mutex::lock` causes a d
910
LL | | do_stuff(locked);
1011
LL | | } else {
1112
LL | | let lock = m.lock().unwrap();
@@ -19,13 +20,14 @@ LL | | };
1920
= help: to override `-D warnings` add `#[allow(clippy::if_let_mutex)]`
2021

2122
error: calling `Mutex::lock` inside the scope of another `Mutex::lock` causes a deadlock
22-
--> tests/ui/if_let_mutex.rs:24:5
23+
--> tests/ui/if_let_mutex.rs:30:5
2324
|
2425
LL | if let Some(locked) = m.lock().unwrap().deref() {
2526
| ^ - this Mutex will remain locked for the entire `if let`-block...
2627
| _____|
2728
| |
2829
LL | |
30+
LL | | // ERROR: calling `Mutex::lock` inside the scope of another `Mutex::lock` causes a d
2931
LL | | do_stuff(locked);
3032
LL | | } else {
3133
LL | | let lock = m.lock().unwrap();
@@ -37,13 +39,14 @@ LL | | };
3739
= help: move the lock call outside of the `if let ...` expression
3840

3941
error: calling `Mutex::lock` inside the scope of another `Mutex::lock` causes a deadlock
40-
--> tests/ui/if_let_mutex.rs:46:5
42+
--> tests/ui/if_let_mutex.rs:53:5
4143
|
4244
LL | if let Ok(i) = mutex.lock() {
4345
| ^ ----- this Mutex will remain locked for the entire `if let`-block...
4446
| _____|
4547
| |
4648
LL | |
49+
LL | | // ERROR: calling `Mutex::lock` inside the scope of another `Mutex::lock` causes a d
4750
LL | | do_stuff(i);
4851
LL | | } else {
4952
LL | | let _x = mutex.lock();
@@ -54,7 +57,7 @@ LL | | };
5457
= help: move the lock call outside of the `if let ...` expression
5558

5659
error: calling `Mutex::lock` inside the scope of another `Mutex::lock` causes a deadlock
57-
--> tests/ui/if_let_mutex.rs:55:5
60+
--> tests/ui/if_let_mutex.rs:63:5
5861
|
5962
LL | if let Ok(_) = m1.lock() {
6063
| ^ -- this Mutex will remain locked for the entire `if let`-block...

tests/ui/if_let_mutex.rs

+11-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
//@ compile-flags: -Zunstable-options
2+
3+
//@revisions: edition2021 edition2024
4+
//@[edition2021] edition:2021
5+
//@[edition2024] edition:2024
16
#![warn(clippy::if_let_mutex)]
27
#![allow(clippy::redundant_pattern_matching)]
38

@@ -9,7 +14,8 @@ fn do_stuff<T>(_: T) {}
914
fn if_let() {
1015
let m = Mutex::new(1_u8);
1116
if let Err(locked) = m.lock() {
12-
//~^ ERROR: calling `Mutex::lock` inside the scope of another `Mutex::lock` causes a d
17+
//~[edition2021]^ if_let_mutex
18+
// ERROR: calling `Mutex::lock` inside the scope of another `Mutex::lock` causes a d
1319
do_stuff(locked);
1420
} else {
1521
let lock = m.lock().unwrap();
@@ -22,7 +28,8 @@ fn if_let() {
2228
fn if_let_option() {
2329
let m = Mutex::new(Some(0_u8));
2430
if let Some(locked) = m.lock().unwrap().deref() {
25-
//~^ ERROR: calling `Mutex::lock` inside the scope of another `Mutex::lock` causes a d
31+
//~[edition2021]^ if_let_mutex
32+
// ERROR: calling `Mutex::lock` inside the scope of another `Mutex::lock` causes a d
2633
do_stuff(locked);
2734
} else {
2835
let lock = m.lock().unwrap();
@@ -44,7 +51,8 @@ fn if_let_different_mutex() {
4451

4552
fn mutex_ref(mutex: &Mutex<i32>) {
4653
if let Ok(i) = mutex.lock() {
47-
//~^ ERROR: calling `Mutex::lock` inside the scope of another `Mutex::lock` causes a d
54+
//~[edition2021]^ if_let_mutex
55+
// ERROR: calling `Mutex::lock` inside the scope of another `Mutex::lock` causes a d
4856
do_stuff(i);
4957
} else {
5058
let _x = mutex.lock();

0 commit comments

Comments
 (0)