@@ -3,11 +3,11 @@ mod box_vec;
3
3
mod linked_list;
4
4
mod option_option;
5
5
mod rc_buffer;
6
+ mod rc_mutex;
6
7
mod redundant_allocation;
7
8
mod type_complexity;
8
9
mod utils;
9
10
mod vec_box;
10
- mod rc_mutex;
11
11
12
12
use rustc_hir as hir;
13
13
use rustc_hir:: intravisit:: FnKind ;
@@ -252,18 +252,50 @@ declare_clippy_lint! {
252
252
}
253
253
254
254
declare_clippy_lint ! {
255
- /// TODO
255
+ /// **What it does:** Checks for `Rc<Mutex<T>>`.
256
+ ///
257
+ /// **Why is this bad?** `Rc<Mutex<T>>` may introduce a deadlock in single thread. Consider
258
+ /// using `Rc<RefCell<T>>` instead.
259
+ /// ```rust
260
+ /// fn main() {
261
+ /// use std::rc::Rc;
262
+ /// use std::sync::Mutex;
263
+ ///
264
+ /// let a: Rc<Mutex<i32>> = Rc::new(Mutex::new(1));
265
+ /// let a_clone = a.clone();
266
+ /// let mut data = a.lock().unwrap();
267
+ /// println!("{:?}", *a_clone.lock().unwrap());
268
+ /// *data = 10;
269
+ /// }
270
+ /// ```
271
+ ///
272
+ /// **Known problems:** `Rc<RefCell<T>>` may panic in runtime.
273
+ ///
274
+ /// **Example:**
275
+ /// ```rust,ignore
276
+ /// use std::rc::Rc;
277
+ /// use std::sync::Mutex;
278
+ /// fn foo(interned: Rc<Mutex<i32>>) { ... }
279
+ /// ```
280
+ ///
281
+ /// Better:
282
+ ///
283
+ /// ```rust,ignore
284
+ /// use std::rc::Rc;
285
+ /// use std::cell::RefCell
286
+ /// fn foo(interned: Rc<RefCell<i32>>) { ... }
287
+ /// ```
256
288
pub RC_MUTEX ,
257
289
restriction,
258
- "usage of Mutex inside Rc "
290
+ "usage of `Rc<Mutex<T>>` "
259
291
}
260
292
261
293
pub struct Types {
262
294
vec_box_size_threshold : u64 ,
263
295
type_complexity_threshold : u64 ,
264
296
}
265
297
266
- impl_lint_pass ! ( Types => [ BOX_VEC , VEC_BOX , OPTION_OPTION , LINKEDLIST , BORROWED_BOX , REDUNDANT_ALLOCATION , RC_BUFFER , TYPE_COMPLEXITY , RC_MUTEX ] ) ;
298
+ impl_lint_pass ! ( Types => [ BOX_VEC , VEC_BOX , OPTION_OPTION , LINKEDLIST , BORROWED_BOX , REDUNDANT_ALLOCATION , RC_BUFFER , RC_MUTEX , TYPE_COMPLEXITY ] ) ;
267
299
268
300
impl < ' tcx > LateLintPass < ' tcx > for Types {
269
301
fn check_fn ( & mut self , cx : & LateContext < ' _ > , _: FnKind < ' _ > , decl : & FnDecl < ' _ > , _: & Body < ' _ > , _: Span , id : HirId ) {
0 commit comments