|
| 1 | +use clippy_utils::diagnostics::span_lint_and_sugg; |
| 2 | +use clippy_utils::source::snippet_opt; |
| 3 | +use clippy_utils::{match_def_path, meets_msrv, msrvs, paths}; |
| 4 | +use rustc_ast::ast::LitKind; |
| 5 | +use rustc_errors::Applicability; |
| 6 | +use rustc_hir::{BinOpKind, Expr, ExprKind, GenericArg, QPath}; |
| 7 | +use rustc_lint::{LateContext, LateLintPass}; |
| 8 | +use rustc_middle::ty::{self, Ty}; |
| 9 | +use rustc_semver::RustcVersion; |
| 10 | +use rustc_session::{declare_tool_lint, impl_lint_pass}; |
| 11 | + |
| 12 | +declare_clippy_lint! { |
| 13 | + /// ### What it does |
| 14 | + /// Checks for uses of `std::mem::size_of::<T>() * 8` when |
| 15 | + /// `T::BITS` is available. |
| 16 | + /// |
| 17 | + /// ### Why is this bad? |
| 18 | + /// Can be written as the shorter `T::BITS`. |
| 19 | + /// |
| 20 | + /// ### Example |
| 21 | + /// ```rust |
| 22 | + /// std::mem::size_of::<usize>() * 8; |
| 23 | + /// ``` |
| 24 | + /// Use instead: |
| 25 | + /// ```rust |
| 26 | + /// usize::BITS; |
| 27 | + /// ``` |
| 28 | + #[clippy::version = "1.60.0"] |
| 29 | + pub MANUAL_BITS, |
| 30 | + style, |
| 31 | + "manual implementation of `size_of::<T>() * 8` can be simplified with `T::BITS`" |
| 32 | +} |
| 33 | + |
| 34 | +#[derive(Clone)] |
| 35 | +pub struct ManualBits { |
| 36 | + msrv: Option<RustcVersion>, |
| 37 | +} |
| 38 | + |
| 39 | +impl ManualBits { |
| 40 | + #[must_use] |
| 41 | + pub fn new(msrv: Option<RustcVersion>) -> Self { |
| 42 | + Self { msrv } |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +impl_lint_pass!(ManualBits => [MANUAL_BITS]); |
| 47 | + |
| 48 | +impl<'tcx> LateLintPass<'tcx> for ManualBits { |
| 49 | + fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) { |
| 50 | + if !meets_msrv(self.msrv.as_ref(), &msrvs::MANUAL_BITS) { |
| 51 | + return; |
| 52 | + } |
| 53 | + |
| 54 | + if_chain! { |
| 55 | + if let ExprKind::Binary(bin_op, left_expr, right_expr) = expr.kind; |
| 56 | + if let BinOpKind::Mul = &bin_op.node; |
| 57 | + if let Some((real_ty, resolved_ty, other_expr)) = get_one_size_of_ty(cx, left_expr, right_expr); |
| 58 | + if matches!(resolved_ty.kind(), ty::Int(_) | ty::Uint(_)); |
| 59 | + if let ExprKind::Lit(lit) = &other_expr.kind; |
| 60 | + if let LitKind::Int(8, _) = lit.node; |
| 61 | + |
| 62 | + then { |
| 63 | + span_lint_and_sugg( |
| 64 | + cx, |
| 65 | + MANUAL_BITS, |
| 66 | + expr.span, |
| 67 | + "usage of `mem::size_of::<T>()` to obtain the size of `T` in bits", |
| 68 | + "consider using", |
| 69 | + format!("{}::BITS", snippet_opt(cx, real_ty.span).unwrap()), |
| 70 | + Applicability::MachineApplicable, |
| 71 | + ); |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +fn get_one_size_of_ty<'tcx>( |
| 78 | + cx: &LateContext<'tcx>, |
| 79 | + expr1: &'tcx Expr<'_>, |
| 80 | + expr2: &'tcx Expr<'_>, |
| 81 | +) -> Option<(&'tcx rustc_hir::Ty<'tcx>, Ty<'tcx>, &'tcx Expr<'tcx>)> { |
| 82 | + match (get_size_of_ty(cx, expr1), get_size_of_ty(cx, expr2)) { |
| 83 | + (Some((real_ty, resolved_ty)), None) => Some((real_ty, resolved_ty, expr2)), |
| 84 | + (None, Some((real_ty, resolved_ty))) => Some((real_ty, resolved_ty, expr1)), |
| 85 | + _ => None, |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +fn get_size_of_ty<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) -> Option<(&'tcx rustc_hir::Ty<'tcx>, Ty<'tcx>)> { |
| 90 | + if_chain! { |
| 91 | + if let ExprKind::Call(count_func, _func_args) = expr.kind; |
| 92 | + if let ExprKind::Path(ref count_func_qpath) = count_func.kind; |
| 93 | + |
| 94 | + if let QPath::Resolved(_, count_func_path) = count_func_qpath; |
| 95 | + if let Some(segment_zero) = count_func_path.segments.get(0); |
| 96 | + if let Some(args) = segment_zero.args; |
| 97 | + if let Some(GenericArg::Type(real_ty)) = args.args.get(0); |
| 98 | + |
| 99 | + if let Some(def_id) = cx.qpath_res(count_func_qpath, count_func.hir_id).opt_def_id(); |
| 100 | + if match_def_path(cx, def_id, &paths::MEM_SIZE_OF); |
| 101 | + then { |
| 102 | + cx.typeck_results().node_substs(count_func.hir_id).types().next().map(|resolved_ty| (real_ty, resolved_ty)) |
| 103 | + } else { |
| 104 | + None |
| 105 | + } |
| 106 | + } |
| 107 | +} |
0 commit comments