@@ -3,6 +3,7 @@ mod box_vec;
33mod linked_list;
44mod option_option;
55mod rc_buffer;
6+ mod rc_mutex;
67mod redundant_allocation;
78mod type_complexity;
89mod utils;
@@ -250,12 +251,41 @@ declare_clippy_lint! {
250251 "usage of very complex types that might be better factored into `type` definitions"
251252}
252253
254+ declare_clippy_lint ! {
255+ /// **What it does:** Checks for `Rc<Mutex<T>>`.
256+ ///
257+ /// **Why is this bad?** `Rc` is used in single thread and `Mutex` is used in multi thread.
258+ /// Consider using `Rc<RefCell<T>>` in single thread or `Arc<Mutex<T>>` in multi thread.
259+ ///
260+ /// **Known problems:** Sometimes combining generic types can lead to the requirement that a
261+ /// type use Rc in conjunction with Mutex. We must consider those cases false positives, but
262+ /// alas they are quite hard to rule out. Luckily they are also rare.
263+ ///
264+ /// **Example:**
265+ /// ```rust,ignore
266+ /// use std::rc::Rc;
267+ /// use std::sync::Mutex;
268+ /// fn foo(interned: Rc<Mutex<i32>>) { ... }
269+ /// ```
270+ ///
271+ /// Better:
272+ ///
273+ /// ```rust,ignore
274+ /// use std::rc::Rc;
275+ /// use std::cell::RefCell
276+ /// fn foo(interned: Rc<RefCell<i32>>) { ... }
277+ /// ```
278+ pub RC_MUTEX ,
279+ restriction,
280+ "usage of `Rc<Mutex<T>>`"
281+ }
282+
253283pub struct Types {
254284 vec_box_size_threshold : u64 ,
255285 type_complexity_threshold : u64 ,
256286}
257287
258- impl_lint_pass ! ( Types => [ BOX_VEC , VEC_BOX , OPTION_OPTION , LINKEDLIST , BORROWED_BOX , REDUNDANT_ALLOCATION , RC_BUFFER , TYPE_COMPLEXITY ] ) ;
288+ impl_lint_pass ! ( Types => [ BOX_VEC , VEC_BOX , OPTION_OPTION , LINKEDLIST , BORROWED_BOX , REDUNDANT_ALLOCATION , RC_BUFFER , RC_MUTEX , TYPE_COMPLEXITY ] ) ;
259289
260290impl < ' tcx > LateLintPass < ' tcx > for Types {
261291 fn check_fn ( & mut self , cx : & LateContext < ' _ > , _: FnKind < ' _ > , decl : & FnDecl < ' _ > , _: & Body < ' _ > , _: Span , id : HirId ) {
@@ -375,6 +405,7 @@ impl Types {
375405 triggered |= vec_box:: check ( cx, hir_ty, qpath, def_id, self . vec_box_size_threshold ) ;
376406 triggered |= option_option:: check ( cx, hir_ty, qpath, def_id) ;
377407 triggered |= linked_list:: check ( cx, hir_ty, def_id) ;
408+ triggered |= rc_mutex:: check ( cx, hir_ty, qpath, def_id) ;
378409
379410 if triggered {
380411 return ;
0 commit comments