|
| 1 | +use clippy_utils::diagnostics::span_lint_and_help; |
| 2 | +use clippy_utils::{expr_or_init, in_constant}; |
| 3 | +use rustc_hir::{BinOpKind, Expr, ExprKind}; |
| 4 | +use rustc_lint::{LateContext, LateLintPass}; |
| 5 | +use rustc_middle::ty; |
| 6 | +use rustc_session::{declare_lint_pass, declare_tool_lint}; |
| 7 | +use rustc_span::symbol::sym; |
| 8 | + |
| 9 | +declare_clippy_lint! { |
| 10 | + /// ### What it does |
| 11 | + /// When `a` is `&[T]`, detect `a.len() * size_of::<T>()` and suggest `size_of_val(a)` |
| 12 | + /// instead. |
| 13 | + /// |
| 14 | + /// ### Why is this better? |
| 15 | + /// * Shorter to write |
| 16 | + /// * Removes the need for the human and the compiler to worry about overflow in the |
| 17 | + /// multiplication |
| 18 | + /// * Potentially faster at runtime as rust emits special no-wrapping flags when it |
| 19 | + /// calculates the byte length |
| 20 | + /// * Less turbofishing |
| 21 | + /// |
| 22 | + /// ### Example |
| 23 | + /// ```rust |
| 24 | + /// # let data : &[i32] = &[1, 2, 3]; |
| 25 | + /// let newlen = data.len() * std::mem::size_of::<i32>(); |
| 26 | + /// ``` |
| 27 | + /// Use instead: |
| 28 | + /// ```rust |
| 29 | + /// # let data : &[i32] = &[1, 2, 3]; |
| 30 | + /// let newlen = std::mem::size_of_val(data); |
| 31 | + /// ``` |
| 32 | + #[clippy::version = "1.70.0"] |
| 33 | + pub MANUAL_SLICE_SIZE_CALCULATION, |
| 34 | + complexity, |
| 35 | + "manual slice size calculation" |
| 36 | +} |
| 37 | +declare_lint_pass!(ManualSliceSizeCalculation => [MANUAL_SLICE_SIZE_CALCULATION]); |
| 38 | + |
| 39 | +impl<'tcx> LateLintPass<'tcx> for ManualSliceSizeCalculation { |
| 40 | + fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) { |
| 41 | + // Does not apply inside const because size_of_value is not cost in stable. |
| 42 | + if !in_constant(cx, expr.hir_id) |
| 43 | + && let ExprKind::Binary(ref op, left, right) = expr.kind |
| 44 | + && BinOpKind::Mul == op.node |
| 45 | + && let Some(_receiver) = simplify(cx, left, right) |
| 46 | + { |
| 47 | + span_lint_and_help( |
| 48 | + cx, |
| 49 | + MANUAL_SLICE_SIZE_CALCULATION, |
| 50 | + expr.span, |
| 51 | + "manual slice size calculation", |
| 52 | + None, |
| 53 | + "consider using std::mem::size_of_value instead"); |
| 54 | + } |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +fn simplify<'tcx>( |
| 59 | + cx: &LateContext<'tcx>, |
| 60 | + expr1: &'tcx Expr<'tcx>, |
| 61 | + expr2: &'tcx Expr<'tcx>, |
| 62 | +) -> Option<&'tcx Expr<'tcx>> { |
| 63 | + let expr1 = expr_or_init(cx, expr1); |
| 64 | + let expr2 = expr_or_init(cx, expr2); |
| 65 | + |
| 66 | + simplify_half(cx, expr1, expr2).or_else(|| simplify_half(cx, expr2, expr1)) |
| 67 | +} |
| 68 | + |
| 69 | +fn simplify_half<'tcx>( |
| 70 | + cx: &LateContext<'tcx>, |
| 71 | + expr1: &'tcx Expr<'tcx>, |
| 72 | + expr2: &'tcx Expr<'tcx>, |
| 73 | +) -> Option<&'tcx Expr<'tcx>> { |
| 74 | + if |
| 75 | + // expr1 is `[T1].len()`? |
| 76 | + let ExprKind::MethodCall(method_path, receiver, _, _) = expr1.kind |
| 77 | + && method_path.ident.name == sym::len |
| 78 | + && let receiver_ty = cx.typeck_results().expr_ty(receiver) |
| 79 | + && let ty::Slice(ty1) = receiver_ty.peel_refs().kind() |
| 80 | + // expr2 is `size_of::<T2>()`? |
| 81 | + && let ExprKind::Call(func, _) = expr2.kind |
| 82 | + && let ExprKind::Path(ref func_qpath) = func.kind |
| 83 | + && let Some(def_id) = cx.qpath_res(func_qpath, func.hir_id).opt_def_id() |
| 84 | + && cx.tcx.is_diagnostic_item(sym::mem_size_of, def_id) |
| 85 | + && let Some(ty2) = cx.typeck_results().node_substs(func.hir_id).types().next() |
| 86 | + // T1 == T2? |
| 87 | + && *ty1 == ty2 |
| 88 | + { |
| 89 | + Some(receiver) |
| 90 | + } else { |
| 91 | + None |
| 92 | + } |
| 93 | +} |
0 commit comments