Skip to content

Commit

Permalink
rc_mutex
Browse files Browse the repository at this point in the history
  • Loading branch information
lyj committed Jun 3, 2021
1 parent 5f746a1 commit 84c511f
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
11 changes: 10 additions & 1 deletion clippy_lints/src/types/mod.rs
Expand Up @@ -7,6 +7,7 @@ mod redundant_allocation;
mod type_complexity;
mod utils;
mod vec_box;
mod rc_mutex;

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

declare_clippy_lint! {
/// TODO
pub RC_MUTEX,
restriction,
"usage of Mutex inside Rc"
}

pub struct Types {
vec_box_size_threshold: u64,
type_complexity_threshold: u64,
}

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

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

if triggered {
return;
Expand Down
39 changes: 39 additions & 0 deletions clippy_lints/src/types/rc_mutex.rs
@@ -0,0 +1,39 @@
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::{ get_qpath_generic_tys,is_ty_param_diagnostic_item};
use clippy_utils::source::snippet_with_applicability;
use rustc_errors::Applicability;
use rustc_hir::{self as hir, def_id::DefId, QPath};
use rustc_lint::LateContext;
use rustc_span::symbol::sym;
// use rustc_middle::ty::Adt;

use super::RC_MUTEX;

pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, qpath: &QPath<'_>, def_id: DefId) -> bool {
if cx.tcx.is_diagnostic_item(sym::Rc, def_id) {
if let Some(ty) = is_ty_param_diagnostic_item(cx, qpath, sym!(mutex_type)) {
let mut applicability = Applicability::MachineApplicable;

let inner_span = match get_qpath_generic_tys(qpath).skip(1).next() {
Some(ty) => ty.span,
None => return false,
};

span_lint_and_sugg(
cx,
RC_MUTEX,
hir_ty.span,
"you seem to be trying to use `Rc<Mutex<T>>`. Consider using `Rc<RefCell<T>>`",
"try",
format!(
"Rc<RefCell<{}>>",
snippet_with_applicability(cx, inner_span, "..", &mut applicability)
),
applicability,
);
return true;
}
}

false
}

0 comments on commit 84c511f

Please sign in to comment.