summaryrefslogtreecommitdiffstats
path: root/src/tools/clippy/clippy_lints/src/read_zero_byte_vec.rs
blob: 2882ba033032285141e00a40620e02bb7717a344 (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
use clippy_utils::{
    diagnostics::{span_lint, span_lint_and_sugg},
    higher::{get_vec_init_kind, VecInitKind},
    source::snippet,
    visitors::expr_visitor_no_bodies,
};
use hir::{intravisit::Visitor, ExprKind, Local, PatKind, PathSegment, QPath, StmtKind};
use rustc_errors::Applicability;
use rustc_hir as hir;
use rustc_lint::{LateContext, LateLintPass};
use rustc_session::{declare_lint_pass, declare_tool_lint};

declare_clippy_lint! {
    /// ### What it does
    /// This lint catches reads into a zero-length `Vec`.
    /// Especially in the case of a call to `with_capacity`, this lint warns that read
    /// gets the number of bytes from the `Vec`'s length, not its capacity.
    ///
    /// ### Why is this bad?
    /// Reading zero bytes is almost certainly not the intended behavior.
    ///
    /// ### Known problems
    /// In theory, a very unusual read implementation could assign some semantic meaning
    /// to zero-byte reads. But it seems exceptionally unlikely that code intending to do
    /// a zero-byte read would allocate a `Vec` for it.
    ///
    /// ### Example
    /// ```rust
    /// use std::io;
    /// fn foo<F: io::Read>(mut f: F) {
    ///     let mut data = Vec::with_capacity(100);
    ///     f.read(&mut data).unwrap();
    /// }
    /// ```
    /// Use instead:
    /// ```rust
    /// use std::io;
    /// fn foo<F: io::Read>(mut f: F) {
    ///     let mut data = Vec::with_capacity(100);
    ///     data.resize(100, 0);
    ///     f.read(&mut data).unwrap();
    /// }
    /// ```
    #[clippy::version = "1.63.0"]
    pub READ_ZERO_BYTE_VEC,
    nursery,
    "checks for reads into a zero-length `Vec`"
}
declare_lint_pass!(ReadZeroByteVec => [READ_ZERO_BYTE_VEC]);

impl<'tcx> LateLintPass<'tcx> for ReadZeroByteVec {
    fn check_block(&mut self, cx: &LateContext<'tcx>, block: &hir::Block<'tcx>) {
        for (idx, stmt) in block.stmts.iter().enumerate() {
            if !stmt.span.from_expansion()
                // matches `let v = Vec::new();`
                && let StmtKind::Local(local) = stmt.kind
                && let Local { pat, init: Some(init), .. } = local
                && let PatKind::Binding(_, _, ident, _) = pat.kind
                && let Some(vec_init_kind) = get_vec_init_kind(cx, init)
            {
                // finds use of `_.read(&mut v)`
                let mut read_found = false;
                let mut visitor = expr_visitor_no_bodies(|expr| {
                    if let ExprKind::MethodCall(path, _self, [arg], _) = expr.kind
                        && let PathSegment { ident: read_or_read_exact, .. } = *path
                        && matches!(read_or_read_exact.as_str(), "read" | "read_exact")
                        && let ExprKind::AddrOf(_, hir::Mutability::Mut, inner) = arg.kind
                        && let ExprKind::Path(QPath::Resolved(None, inner_path)) = inner.kind
                        && let [inner_seg] = inner_path.segments
                        && ident.name == inner_seg.ident.name
                    {
                        read_found = true;
                    }
                    !read_found
                });

                let next_stmt_span;
                if idx == block.stmts.len() - 1 {
                    // case { .. stmt; expr }
                    if let Some(e) = block.expr {
                        visitor.visit_expr(e);
                        next_stmt_span = e.span;
                    } else {
                        return;
                    }
                } else {
                    // case { .. stmt; stmt; .. }
                    let next_stmt = &block.stmts[idx + 1];
                    visitor.visit_stmt(next_stmt);
                    next_stmt_span = next_stmt.span;
                }
                drop(visitor);

                if read_found && !next_stmt_span.from_expansion() {
                    let applicability = Applicability::MaybeIncorrect;
                    match vec_init_kind {
                        VecInitKind::WithConstCapacity(len) => {
                            span_lint_and_sugg(
                                cx,
                                READ_ZERO_BYTE_VEC,
                                next_stmt_span,
                                "reading zero byte data to `Vec`",
                                "try",
                                format!("{}.resize({}, 0); {}",
                                    ident.as_str(),
                                    len,
                                    snippet(cx, next_stmt_span, "..")
                                ),
                                applicability,
                            );
                        }
                        VecInitKind::WithExprCapacity(hir_id) => {
                            let e = cx.tcx.hir().expect_expr(hir_id);
                            span_lint_and_sugg(
                                cx,
                                READ_ZERO_BYTE_VEC,
                                next_stmt_span,
                                "reading zero byte data to `Vec`",
                                "try",
                                format!("{}.resize({}, 0); {}",
                                    ident.as_str(),
                                    snippet(cx, e.span, ".."),
                                    snippet(cx, next_stmt_span, "..")
                                ),
                                applicability,
                            );
                        }
                        _ => {
                            span_lint(
                                cx,
                                READ_ZERO_BYTE_VEC,
                                next_stmt_span,
                                "reading zero byte data to `Vec`",
                            );

                        }
                    }
                }
            }
        }
    }
}