-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add new lint: rc_mutex
#7316
Add new lint: rc_mutex
#7316
Changes from 6 commits
84c511f
c0f3c2f
a5ced1f
e2ec85c
896c19e
f877f54
683c557
b9cc2fe
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
} |
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>>) {} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd like to see a test that actually creates an instance. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you please check if I added correct test in my latest commit? |
||
|
||
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)), | ||
}; | ||
} |
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 | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think there may be instances where combining generics and traits could require this actual type combination, and following the suggestion would make it impossible to implement the traits.
We should consider those cases false positives and make a note of a possible problem. I don't think this problem is worrisome for the lint (it's unlikely to happen in practise), but we should note it nonetheless.