-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #7316 - lengyijun:rc_mutex, r=llogiq
Add new lint: `rc_mutex` changelog: Add new lint `rc_mutex`. It lints on `Rc<Mutex<T>>`. `Rc<Mutex<T>>` should be corrected to `Rc<RefCell<T>>`
- Loading branch information
Showing
6 changed files
with
124 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
use clippy_utils::diagnostics::span_lint; | ||
use clippy_utils::is_ty_param_diagnostic_item; | ||
use if_chain::if_chain; | ||
use rustc_hir::{self as hir, def_id::DefId, QPath}; | ||
use rustc_lint::LateContext; | ||
use rustc_span::symbol::sym; | ||
|
||
use super::RC_MUTEX; | ||
|
||
pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, qpath: &QPath<'_>, def_id: DefId) -> bool { | ||
if_chain! { | ||
if cx.tcx.is_diagnostic_item(sym::Rc, def_id) ; | ||
if let Some(_) = is_ty_param_diagnostic_item(cx, qpath, sym!(mutex_type)) ; | ||
|
||
then{ | ||
span_lint( | ||
cx, | ||
RC_MUTEX, | ||
hir_ty.span, | ||
"found `Rc<Mutex<_>>`. Consider using `Rc<RefCell<_>>` or `Arc<Mutex<_>>` instead", | ||
); | ||
return true; | ||
} | ||
} | ||
|
||
false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#![warn(clippy::rc_mutex)] | ||
#![allow(clippy::blacklisted_name)] | ||
|
||
use std::rc::Rc; | ||
use std::sync::Mutex; | ||
|
||
pub struct MyStruct { | ||
foo: Rc<Mutex<i32>>, | ||
} | ||
|
||
pub struct SubT<T> { | ||
foo: T, | ||
} | ||
|
||
pub enum MyEnum { | ||
One, | ||
Two, | ||
} | ||
|
||
pub fn test1<T>(foo: Rc<Mutex<T>>) {} | ||
|
||
pub fn test2(foo: Rc<Mutex<MyEnum>>) {} | ||
|
||
pub fn test3(foo: Rc<Mutex<SubT<usize>>>) {} | ||
|
||
fn main() { | ||
test1(Rc::new(Mutex::new(1))); | ||
test2(Rc::new(Mutex::new(MyEnum::One))); | ||
test3(Rc::new(Mutex::new(SubT { foo: 1 }))); | ||
|
||
let _my_struct = MyStruct { | ||
foo: Rc::new(Mutex::new(1)), | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
error: found `Rc<Mutex<_>>`. Consider using `Rc<RefCell<_>>` or `Arc<Mutex<_>>` instead | ||
--> $DIR/rc_mutex.rs:8:10 | ||
| | ||
LL | foo: Rc<Mutex<i32>>, | ||
| ^^^^^^^^^^^^^^ | ||
| | ||
= note: `-D clippy::rc-mutex` implied by `-D warnings` | ||
|
||
error: found `Rc<Mutex<_>>`. Consider using `Rc<RefCell<_>>` or `Arc<Mutex<_>>` instead | ||
--> $DIR/rc_mutex.rs:20:22 | ||
| | ||
LL | pub fn test1<T>(foo: Rc<Mutex<T>>) {} | ||
| ^^^^^^^^^^^^ | ||
|
||
error: found `Rc<Mutex<_>>`. Consider using `Rc<RefCell<_>>` or `Arc<Mutex<_>>` instead | ||
--> $DIR/rc_mutex.rs:22:19 | ||
| | ||
LL | pub fn test2(foo: Rc<Mutex<MyEnum>>) {} | ||
| ^^^^^^^^^^^^^^^^^ | ||
|
||
error: found `Rc<Mutex<_>>`. Consider using `Rc<RefCell<_>>` or `Arc<Mutex<_>>` instead | ||
--> $DIR/rc_mutex.rs:24:19 | ||
| | ||
LL | pub fn test3(foo: Rc<Mutex<SubT<usize>>>) {} | ||
| ^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: aborting due to 4 previous errors | ||
|