Skip to content

Commit 1b4e626

Browse files
committed
WIP: don't lint statics
1 parent e6755c5 commit 1b4e626

File tree

4 files changed

+41
-15
lines changed

4 files changed

+41
-15
lines changed

clippy_lints/src/methods/mut_mutex_lock.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,22 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, recv: &'tcx Expr<'tcx>, name_s
7373
// that the field type is not a `&T`, and we'll do that in the next iteration of the
7474
// loop, during adjustment checking
7575
ExprKind::Field(base, _) => r = base,
76-
// No more projections to check
77-
_ => break,
76+
// We arrived at the innermost receiver
77+
_ => {
78+
if let ExprKind::Path(ref p) = r.kind
79+
&& cx
80+
.qpath_res(p, r.hir_id)
81+
.opt_def_id()
82+
.and_then(|id| cx.tcx.static_mutability(id))
83+
== Some(Mutability::Not)
84+
{
85+
// The mutex is stored in a `static`, and we don't want to suggest making that
86+
// mutable
87+
return;
88+
}
89+
// No more projections to check
90+
break;
91+
},
7892
}
7993
}
8094

tests/ui/mut_mutex_lock.fixed

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ fn mut_ref_ref_mutex_lock() {
3737
*guard += 1;
3838
}
3939

40+
static MTX: Mutex<i32> = Mutex::new(0);
41+
42+
fn dont_suggest_on_statics() {
43+
MTX.lock().unwrap();
44+
}
45+
4046
mod issue16253 {
4147
use std::sync::{Arc, Mutex};
4248

tests/ui/mut_mutex_lock.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ fn mut_ref_ref_mutex_lock() {
3737
*guard += 1;
3838
}
3939

40+
static MTX: Mutex<i32> = Mutex::new(0);
41+
42+
fn dont_suggest_on_statics() {
43+
MTX.lock().unwrap();
44+
}
45+
4046
mod issue16253 {
4147
use std::sync::{Arc, Mutex};
4248

tests/ui/mut_mutex_lock.stderr

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,79 +14,79 @@ LL | let mut value = mut_ref_mut_ref_mutex.lock().unwrap();
1414
| ^^^^ help: change this to: `get_mut`
1515

1616
error: calling `&mut Mutex::lock` unnecessarily locks an exclusive (mutable) reference
17-
--> tests/ui/mut_mutex_lock.rs:68:26
17+
--> tests/ui/mut_mutex_lock.rs:74:26
1818
|
1919
LL | refmutself.0.lock().unwrap();
2020
| ^^^^ help: change this to: `get_mut`
2121

2222
error: calling `&mut Mutex::lock` unnecessarily locks an exclusive (mutable) reference
23-
--> tests/ui/mut_mutex_lock.rs:70:20
23+
--> tests/ui/mut_mutex_lock.rs:76:20
2424
|
2525
LL | self.0.lock().unwrap();
2626
| ^^^^ help: change this to: `get_mut`
2727

2828
error: calling `&mut Mutex::lock` unnecessarily locks an exclusive (mutable) reference
29-
--> tests/ui/mut_mutex_lock.rs:80:26
29+
--> tests/ui/mut_mutex_lock.rs:86:26
3030
|
3131
LL | refmutself.0.lock().unwrap();
3232
| ^^^^ help: change this to: `get_mut`
3333

3434
error: calling `&mut Mutex::lock` unnecessarily locks an exclusive (mutable) reference
35-
--> tests/ui/mut_mutex_lock.rs:82:20
35+
--> tests/ui/mut_mutex_lock.rs:88:20
3636
|
3737
LL | self.0.lock().unwrap();
3838
| ^^^^ help: change this to: `get_mut`
3939

4040
error: calling `&mut Mutex::lock` unnecessarily locks an exclusive (mutable) reference
41-
--> tests/ui/mut_mutex_lock.rs:203:28
41+
--> tests/ui/mut_mutex_lock.rs:209:28
4242
|
4343
LL | refmutself.0.0.lock().unwrap();
4444
| ^^^^ help: change this to: `get_mut`
4545

4646
error: calling `&mut Mutex::lock` unnecessarily locks an exclusive (mutable) reference
47-
--> tests/ui/mut_mutex_lock.rs:205:22
47+
--> tests/ui/mut_mutex_lock.rs:211:22
4848
|
4949
LL | self.0.0.lock().unwrap();
5050
| ^^^^ help: change this to: `get_mut`
5151

5252
error: calling `&mut Mutex::lock` unnecessarily locks an exclusive (mutable) reference
53-
--> tests/ui/mut_mutex_lock.rs:216:28
53+
--> tests/ui/mut_mutex_lock.rs:222:28
5454
|
5555
LL | refmutself.0.0.lock().unwrap();
5656
| ^^^^ help: change this to: `get_mut`
5757

5858
error: calling `&mut Mutex::lock` unnecessarily locks an exclusive (mutable) reference
59-
--> tests/ui/mut_mutex_lock.rs:218:22
59+
--> tests/ui/mut_mutex_lock.rs:224:22
6060
|
6161
LL | self.0.0.lock().unwrap();
6262
| ^^^^ help: change this to: `get_mut`
6363

6464
error: calling `&mut Mutex::lock` unnecessarily locks an exclusive (mutable) reference
65-
--> tests/ui/mut_mutex_lock.rs:251:28
65+
--> tests/ui/mut_mutex_lock.rs:257:28
6666
|
6767
LL | refmutself.0.0.lock().unwrap();
6868
| ^^^^ help: change this to: `get_mut`
6969

7070
error: calling `&mut Mutex::lock` unnecessarily locks an exclusive (mutable) reference
71-
--> tests/ui/mut_mutex_lock.rs:253:22
71+
--> tests/ui/mut_mutex_lock.rs:259:22
7272
|
7373
LL | self.0.0.lock().unwrap();
7474
| ^^^^ help: change this to: `get_mut`
7575

7676
error: calling `&mut Mutex::lock` unnecessarily locks an exclusive (mutable) reference
77-
--> tests/ui/mut_mutex_lock.rs:264:28
77+
--> tests/ui/mut_mutex_lock.rs:270:28
7878
|
7979
LL | refmutself.0.0.lock().unwrap();
8080
| ^^^^ help: change this to: `get_mut`
8181

8282
error: calling `&mut Mutex::lock` unnecessarily locks an exclusive (mutable) reference
83-
--> tests/ui/mut_mutex_lock.rs:266:22
83+
--> tests/ui/mut_mutex_lock.rs:272:22
8484
|
8585
LL | self.0.0.lock().unwrap();
8686
| ^^^^ help: change this to: `get_mut`
8787

8888
error: calling `&mut Mutex::lock` unnecessarily locks an exclusive (mutable) reference
89-
--> tests/ui/mut_mutex_lock.rs:274:20
89+
--> tests/ui/mut_mutex_lock.rs:280:20
9090
|
9191
LL | mutexes[0].lock().unwrap();
9292
| ^^^^ help: change this to: `get_mut`

0 commit comments

Comments
 (0)