1 //! Lint on use of `size_of` or `size_of_val` of T in an expression
2 //! expecting a count of T
4 use clippy_utils::diagnostics::span_lint_and_help;
5 use clippy_utils::{match_def_path, paths};
6 use if_chain::if_chain;
7 use rustc_hir::BinOpKind;
8 use rustc_hir::{Expr, ExprKind};
9 use rustc_lint::{LateContext, LateLintPass};
10 use rustc_middle::ty::{self, Ty, TyS, TypeAndMut};
11 use rustc_session::{declare_lint_pass, declare_tool_lint};
13 declare_clippy_lint! {
15 /// Detects expressions where
16 /// `size_of::<T>` or `size_of_val::<T>` is used as a
17 /// count of elements of type `T`
19 /// ### Why is this bad?
20 /// These functions expect a count
21 /// of `T` and not a number of bytes
25 /// # use std::ptr::copy_nonoverlapping;
26 /// # use std::mem::size_of;
27 /// const SIZE: usize = 128;
28 /// let x = [2u8; SIZE];
29 /// let mut y = [2u8; SIZE];
30 /// unsafe { copy_nonoverlapping(x.as_ptr(), y.as_mut_ptr(), size_of::<u8>() * SIZE) };
32 #[clippy::version = "1.50.0"]
33 pub SIZE_OF_IN_ELEMENT_COUNT,
35 "using `size_of::<T>` or `size_of_val::<T>` where a count of elements of `T` is expected"
38 declare_lint_pass!(SizeOfInElementCount => [SIZE_OF_IN_ELEMENT_COUNT]);
40 fn get_size_of_ty(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, inverted: bool) -> Option<Ty<'tcx>> {
42 ExprKind::Call(count_func, _func_args) => {
45 if let ExprKind::Path(ref count_func_qpath) = count_func.kind;
46 if let Some(def_id) = cx.qpath_res(count_func_qpath, count_func.hir_id).opt_def_id();
47 if match_def_path(cx, def_id, &paths::MEM_SIZE_OF)
48 || match_def_path(cx, def_id, &paths::MEM_SIZE_OF_VAL);
50 cx.typeck_results().node_substs(count_func.hir_id).types().next()
56 ExprKind::Binary(op, left, right) if BinOpKind::Mul == op.node => {
57 get_size_of_ty(cx, left, inverted).or_else(|| get_size_of_ty(cx, right, inverted))
59 ExprKind::Binary(op, left, right) if BinOpKind::Div == op.node => {
60 get_size_of_ty(cx, left, inverted).or_else(|| get_size_of_ty(cx, right, !inverted))
62 ExprKind::Cast(expr, _) => get_size_of_ty(cx, expr, inverted),
67 fn get_pointee_ty_and_count_expr(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) -> Option<(Ty<'tcx>, &'tcx Expr<'tcx>)> {
68 const FUNCTIONS: [&[&str]; 8] = [
69 &paths::PTR_COPY_NONOVERLAPPING,
71 &paths::PTR_WRITE_BYTES,
72 &paths::PTR_SWAP_NONOVERLAPPING,
73 &paths::PTR_SLICE_FROM_RAW_PARTS,
74 &paths::PTR_SLICE_FROM_RAW_PARTS_MUT,
75 &paths::SLICE_FROM_RAW_PARTS,
76 &paths::SLICE_FROM_RAW_PARTS_MUT,
78 const METHODS: [&str; 11] = [
82 "copy_to_nonoverlapping",
83 "copy_from_nonoverlapping",
93 // Find calls to ptr::{copy, copy_nonoverlapping}
94 // and ptr::{swap_nonoverlapping, write_bytes},
95 if let ExprKind::Call(func, [.., count]) = expr.kind;
96 if let ExprKind::Path(ref func_qpath) = func.kind;
97 if let Some(def_id) = cx.qpath_res(func_qpath, func.hir_id).opt_def_id();
98 if FUNCTIONS.iter().any(|func_path| match_def_path(cx, def_id, func_path));
100 // Get the pointee type
101 if let Some(pointee_ty) = cx.typeck_results().node_substs(func.hir_id).types().next();
103 return Some((pointee_ty, count));
107 // Find calls to copy_{from,to}{,_nonoverlapping} and write_bytes methods
108 if let ExprKind::MethodCall(method_path, _, [ptr_self, .., count], _) = expr.kind;
109 let method_ident = method_path.ident.as_str();
110 if METHODS.iter().any(|m| *m == &*method_ident);
112 // Get the pointee type
113 if let ty::RawPtr(TypeAndMut { ty: pointee_ty, .. }) =
114 cx.typeck_results().expr_ty(ptr_self).kind();
116 return Some((pointee_ty, count));
122 impl<'tcx> LateLintPass<'tcx> for SizeOfInElementCount {
123 fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
124 const HELP_MSG: &str = "use a count of elements instead of a count of bytes\
125 , it already gets multiplied by the size of the type";
127 const LINT_MSG: &str = "found a count of bytes \
128 instead of a count of elements of `T`";
131 // Find calls to functions with an element count parameter and get
132 // the pointee type and count parameter expression
133 if let Some((pointee_ty, count_expr)) = get_pointee_ty_and_count_expr(cx, expr);
135 // Find a size_of call in the count parameter expression and
136 // check that it's the same type
137 if let Some(ty_used_for_size_of) = get_size_of_ty(cx, count_expr, false);
138 if TyS::same_type(pointee_ty, ty_used_for_size_of);
142 SIZE_OF_IN_ELEMENT_COUNT,