|
| 1 | +use clippy_config::Conf; |
| 2 | +use rustc_hir::{FieldDef, LetStmt, LocalSource}; |
| 3 | +use rustc_lint::{LateContext, LateLintPass}; |
| 4 | +use rustc_middle::ty; |
| 5 | +use rustc_middle::ty::layout::TyAndLayout; |
| 6 | +use rustc_session::impl_lint_pass; |
| 7 | +use rustc_span::symbol::sym; |
| 8 | +use rustc_span::Span; |
| 9 | + |
| 10 | +use clippy_utils::diagnostics::span_lint_and_help; |
| 11 | +use clippy_utils::ty::implements_trait; |
| 12 | + |
| 13 | +declare_clippy_lint! { |
| 14 | + /// ### What it does |
| 15 | + /// |
| 16 | + /// Detects crate-local usage of `RefCell<T>` where `T` implements `Copy` |
| 17 | + /// |
| 18 | + /// ### Why is this bad? |
| 19 | + /// |
| 20 | + /// `RefCell` has to perform extra book-keeping in order to support references, whereas `Cell` does not. |
| 21 | + /// |
| 22 | + /// ### Example |
| 23 | + /// ```no_run |
| 24 | + /// let interior_mutable = std::cell::RefCell::new(0_u8); |
| 25 | + /// |
| 26 | + /// *interior_mutable.borrow_mut() = 1; |
| 27 | + /// ``` |
| 28 | + /// Use instead: |
| 29 | + /// ```no_run |
| 30 | + /// let interior_mutable = std::cell::Cell::new(0_u8); |
| 31 | + /// |
| 32 | + /// interior_mutable.set(1); |
| 33 | + /// ``` |
| 34 | + #[clippy::version = "1.83.0"] |
| 35 | + pub COPY_REFCELL, |
| 36 | + perf, |
| 37 | + "usage of `RefCell<T>` where `T` implements `Copy`" |
| 38 | +} |
| 39 | + |
| 40 | +pub struct CopyRefcell { |
| 41 | + maximum_size: u64, |
| 42 | +} |
| 43 | + |
| 44 | +impl CopyRefcell { |
| 45 | + pub fn new(conf: &'static Conf) -> Self { |
| 46 | + Self { |
| 47 | + maximum_size: conf.large_cell_limit, |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + fn check_copy_refcell<'tcx>(&self, cx: &LateContext<'tcx>, ty: ty::Ty<'tcx>, span: Span) { |
| 52 | + let Some(copy_def_id) = cx.tcx.get_diagnostic_item(sym::Copy) else { |
| 53 | + return; |
| 54 | + }; |
| 55 | + |
| 56 | + let ty::Adt(adt, generics) = ty.kind() else { |
| 57 | + return; |
| 58 | + }; |
| 59 | + |
| 60 | + if !cx.tcx.is_diagnostic_item(sym::RefCell, adt.did()) { |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + let inner_ty = generics.type_at(0); |
| 65 | + let Ok(TyAndLayout { layout, .. }) = cx.tcx.layout_of(cx.param_env.and(inner_ty)) else { |
| 66 | + return; |
| 67 | + }; |
| 68 | + |
| 69 | + if layout.size().bytes() >= self.maximum_size { |
| 70 | + return; |
| 71 | + } |
| 72 | + |
| 73 | + if implements_trait(cx, inner_ty, copy_def_id, &[]) { |
| 74 | + span_lint_and_help( |
| 75 | + cx, |
| 76 | + COPY_REFCELL, |
| 77 | + span, |
| 78 | + "std::cell::RefCell used with a type that implements Copy", |
| 79 | + None, |
| 80 | + "replace the usage of RefCell with Cell, which is simpler and zero-cost", |
| 81 | + ); |
| 82 | + } |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +impl_lint_pass!(CopyRefcell => [COPY_REFCELL]); |
| 87 | + |
| 88 | +impl<'tcx> LateLintPass<'tcx> for CopyRefcell { |
| 89 | + fn check_field_def(&mut self, cx: &LateContext<'tcx>, field_def: &'tcx FieldDef<'tcx>) { |
| 90 | + // Don't want to lint macro expansions. |
| 91 | + if field_def.span.from_expansion() { |
| 92 | + return; |
| 93 | + } |
| 94 | + |
| 95 | + let field_ty = rustc_hir_analysis::lower_ty(cx.tcx, field_def.ty); |
| 96 | + self.check_copy_refcell(cx, field_ty, field_def.ty.span); |
| 97 | + } |
| 98 | + |
| 99 | + fn check_local(&mut self, cx: &LateContext<'tcx>, local_def: &'tcx LetStmt<'tcx>) { |
| 100 | + // Don't want to lint macro expansions or desugaring. |
| 101 | + if local_def.span.from_expansion() || !matches!(local_def.source, LocalSource::Normal) { |
| 102 | + return; |
| 103 | + } |
| 104 | + |
| 105 | + let Some(init_expr) = local_def.init else { |
| 106 | + return; |
| 107 | + }; |
| 108 | + |
| 109 | + let init_ty = cx.typeck_results().expr_ty(init_expr); |
| 110 | + self.check_copy_refcell(cx, init_ty, init_expr.span); |
| 111 | + } |
| 112 | +} |
0 commit comments