Skip to content

Commit 84c511f

Browse files
author
lyj
committed
rc_mutex
1 parent 5f746a1 commit 84c511f

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

clippy_lints/src/types/mod.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ mod redundant_allocation;
77
mod type_complexity;
88
mod utils;
99
mod vec_box;
10+
mod rc_mutex;
1011

1112
use rustc_hir as hir;
1213
use rustc_hir::intravisit::FnKind;
@@ -250,12 +251,19 @@ declare_clippy_lint! {
250251
"usage of very complex types that might be better factored into `type` definitions"
251252
}
252253

254+
declare_clippy_lint! {
255+
/// TODO
256+
pub RC_MUTEX,
257+
restriction,
258+
"usage of Mutex inside Rc"
259+
}
260+
253261
pub struct Types {
254262
vec_box_size_threshold: u64,
255263
type_complexity_threshold: u64,
256264
}
257265

258-
impl_lint_pass!(Types => [BOX_VEC, VEC_BOX, OPTION_OPTION, LINKEDLIST, BORROWED_BOX, REDUNDANT_ALLOCATION, RC_BUFFER, TYPE_COMPLEXITY]);
266+
impl_lint_pass!(Types => [BOX_VEC, VEC_BOX, OPTION_OPTION, LINKEDLIST, BORROWED_BOX, REDUNDANT_ALLOCATION, RC_BUFFER, TYPE_COMPLEXITY,RC_MUTEX]);
259267

260268
impl<'tcx> LateLintPass<'tcx> for Types {
261269
fn check_fn(&mut self, cx: &LateContext<'_>, _: FnKind<'_>, decl: &FnDecl<'_>, _: &Body<'_>, _: Span, id: HirId) {
@@ -375,6 +383,7 @@ impl Types {
375383
triggered |= vec_box::check(cx, hir_ty, qpath, def_id, self.vec_box_size_threshold);
376384
triggered |= option_option::check(cx, hir_ty, qpath, def_id);
377385
triggered |= linked_list::check(cx, hir_ty, def_id);
386+
triggered |= rc_mutex::check(cx, hir_ty, qpath, def_id);
378387

379388
if triggered {
380389
return;

clippy_lints/src/types/rc_mutex.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
use clippy_utils::diagnostics::span_lint_and_sugg;
2+
use clippy_utils::{ get_qpath_generic_tys,is_ty_param_diagnostic_item};
3+
use clippy_utils::source::snippet_with_applicability;
4+
use rustc_errors::Applicability;
5+
use rustc_hir::{self as hir, def_id::DefId, QPath};
6+
use rustc_lint::LateContext;
7+
use rustc_span::symbol::sym;
8+
// use rustc_middle::ty::Adt;
9+
10+
use super::RC_MUTEX;
11+
12+
pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, qpath: &QPath<'_>, def_id: DefId) -> bool {
13+
if cx.tcx.is_diagnostic_item(sym::Rc, def_id) {
14+
if let Some(ty) = is_ty_param_diagnostic_item(cx, qpath, sym!(mutex_type)) {
15+
let mut applicability = Applicability::MachineApplicable;
16+
17+
let inner_span = match get_qpath_generic_tys(qpath).skip(1).next() {
18+
Some(ty) => ty.span,
19+
None => return false,
20+
};
21+
22+
span_lint_and_sugg(
23+
cx,
24+
RC_MUTEX,
25+
hir_ty.span,
26+
"you seem to be trying to use `Rc<Mutex<T>>`. Consider using `Rc<RefCell<T>>`",
27+
"try",
28+
format!(
29+
"Rc<RefCell<{}>>",
30+
snippet_with_applicability(cx, inner_span, "..", &mut applicability)
31+
),
32+
applicability,
33+
);
34+
return true;
35+
}
36+
}
37+
38+
false
39+
}

0 commit comments

Comments
 (0)