summaryrefslogtreecommitdiffstats
path: root/src/tools/clippy/clippy_lints/src/size_of_ref.rs
blob: 89ac8cd8ca97567e91018e3d732e7675135be7d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
use clippy_utils::diagnostics::span_lint_and_help;
use clippy_utils::path_def_id;
use clippy_utils::ty::peel_mid_ty_refs;
use rustc_hir::{Expr, ExprKind};
use rustc_lint::{LateContext, LateLintPass};
use rustc_session::{declare_lint_pass, declare_tool_lint};
use rustc_span::sym;

declare_clippy_lint! {
    /// ### What it does
    ///
    /// Checks for calls to `std::mem::size_of_val()` where the argument is
    /// a reference to a reference.
    ///
    /// ### Why is this bad?
    ///
    /// Calling `size_of_val()` with a reference to a reference as the argument
    /// yields the size of the reference-type, not the size of the value behind
    /// the reference.
    ///
    /// ### Example
    /// ```rust
    /// struct Foo {
    ///     buffer: [u8],
    /// }
    ///
    /// impl Foo {
    ///     fn size(&self) -> usize {
    ///         // Note that `&self` as an argument is a `&&Foo`: Because `self`
    ///         // is already a reference, `&self` is a double-reference.
    ///         // The return value of `size_of_val()` therefor is the
    ///         // size of the reference-type, not the size of `self`.
    ///         std::mem::size_of_val(&self)
    ///     }
    /// }
    /// ```
    /// Use instead:
    /// ```rust
    /// struct Foo {
    ///     buffer: [u8],
    /// }
    ///
    /// impl Foo {
    ///     fn size(&self) -> usize {
    ///         // Correct
    ///         std::mem::size_of_val(self)
    ///     }
    /// }
    /// ```
    #[clippy::version = "1.68.0"]
    pub SIZE_OF_REF,
    suspicious,
    "Argument to `std::mem::size_of_val()` is a double-reference, which is almost certainly unintended"
}
declare_lint_pass!(SizeOfRef => [SIZE_OF_REF]);

impl LateLintPass<'_> for SizeOfRef {
    fn check_expr(&mut self, cx: &LateContext<'_>, expr: &'_ Expr<'_>) {
        if let ExprKind::Call(path, [arg]) = expr.kind
            && let Some(def_id) = path_def_id(cx, path)
            && cx.tcx.is_diagnostic_item(sym::mem_size_of_val, def_id)
            && let arg_ty = cx.typeck_results().expr_ty(arg)
            && peel_mid_ty_refs(arg_ty).1 > 1
        {
            span_lint_and_help(
                cx,
                SIZE_OF_REF,
                expr.span,
                "argument to `std::mem::size_of_val()` is a reference to a reference",
                None,
                "dereference the argument to `std::mem::size_of_val()` to get the size of the value instead of the size of the reference-type",
            );
        }
    }
}