Skip to content

Commit 97b9e4a

Browse files
authored
Do not trigger if_let_mutex starting from Edition 2024 (#13695)
Close #13679 changelog: [`if_let_mutex`]: disable lint from Edition 2024 since [stabilized if_let_rescope ](rust-lang/rust#131154)
2 parents 8698c31 + ef42a66 commit 97b9e4a

File tree

3 files changed

+23
-7
lines changed

3 files changed

+23
-7
lines changed

Diff for: 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+
/// This lint 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,

Diff for: tests/ui/if_let_mutex.stderr renamed to tests/ui/if_let_mutex.edition2021.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
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...
@@ -19,7 +19,7 @@ LL | | };
1919
= help: to override `-D warnings` add `#[allow(clippy::if_let_mutex)]`
2020

2121
error: calling `Mutex::lock` inside the scope of another `Mutex::lock` causes a deadlock
22-
--> tests/ui/if_let_mutex.rs:24:5
22+
--> tests/ui/if_let_mutex.rs:29:5
2323
|
2424
LL | if let Some(locked) = m.lock().unwrap().deref() {
2525
| ^ - this Mutex will remain locked for the entire `if let`-block...
@@ -37,7 +37,7 @@ LL | | };
3737
= help: move the lock call outside of the `if let ...` expression
3838

3939
error: calling `Mutex::lock` inside the scope of another `Mutex::lock` causes a deadlock
40-
--> tests/ui/if_let_mutex.rs:46:5
40+
--> tests/ui/if_let_mutex.rs:51:5
4141
|
4242
LL | if let Ok(i) = mutex.lock() {
4343
| ^ ----- this Mutex will remain locked for the entire `if let`-block...
@@ -54,7 +54,7 @@ LL | | };
5454
= help: move the lock call outside of the `if let ...` expression
5555

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

Diff for: tests/ui/if_let_mutex.rs

+8-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,7 @@ 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
1318
do_stuff(locked);
1419
} else {
1520
let lock = m.lock().unwrap();
@@ -22,7 +27,7 @@ fn if_let() {
2227
fn if_let_option() {
2328
let m = Mutex::new(Some(0_u8));
2429
if let Some(locked) = m.lock().unwrap().deref() {
25-
//~^ ERROR: calling `Mutex::lock` inside the scope of another `Mutex::lock` causes a d
30+
//~[edition2021]^ if_let_mutex
2631
do_stuff(locked);
2732
} else {
2833
let lock = m.lock().unwrap();
@@ -44,7 +49,7 @@ fn if_let_different_mutex() {
4449

4550
fn mutex_ref(mutex: &Mutex<i32>) {
4651
if let Ok(i) = mutex.lock() {
47-
//~^ ERROR: calling `Mutex::lock` inside the scope of another `Mutex::lock` causes a d
52+
//~[edition2021]^ if_let_mutex
4853
do_stuff(i);
4954
} else {
5055
let _x = mutex.lock();

0 commit comments

Comments
 (0)