]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/verbose_file_reads.rs
Auto merge of #5272 - jmeyers35:file_read_lint, r=flip1995
[rust.git] / clippy_lints / src / verbose_file_reads.rs
1 use crate::utils::{match_type, paths, span_lint_and_help};
2 use if_chain::if_chain;
3 use rustc_hir::{Expr, ExprKind, QPath};
4 use rustc_lint::{LateContext, LateLintPass};
5 use rustc_session::{declare_lint_pass, declare_tool_lint};
6
7 declare_clippy_lint! {
8     /// **What it does:** Checks for use of File::read_to_end and File::read_to_string.
9     ///
10     /// **Why is this bad?** `fs::{read, read_to_string}` provide the same functionality when `buf` is empty with fewer imports and no intermediate values.
11     /// See also: [fs::read docs](https://doc.rust-lang.org/std/fs/fn.read.html), [fs::read_to_string docs](https://doc.rust-lang.org/std/fs/fn.read_to_string.html)
12     /// **Known problems:** None.
13     ///
14     /// **Example:**
15     ///
16     /// ```rust,no_run
17     /// # use std::io::Read;
18     /// # use std::fs::File;
19     /// let mut f = File::open("foo.txt").unwrap();
20     /// let mut bytes = Vec::new();
21     /// f.read_to_end(&mut bytes).unwrap();
22     /// ```
23     /// Can be written more concisely as
24     /// ```rust,no_run
25     /// # use std::fs;
26     /// let mut bytes = fs::read("foo.txt").unwrap();
27     /// ```
28     pub VERBOSE_FILE_READS,
29     complexity,
30     "use of `File::read_to_end` or `File::read_to_string`"
31 }
32
33 declare_lint_pass!(VerboseFileReads => [VERBOSE_FILE_READS]);
34
35 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for VerboseFileReads {
36     fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr<'tcx>) {
37         if is_file_read_to_end(cx, expr) {
38             span_lint_and_help(
39                 cx,
40                 VERBOSE_FILE_READS,
41                 expr.span,
42                 "use of `File::read_to_end`",
43                 "consider using `fs::read` instead",
44             );
45         } else if is_file_read_to_string(cx, expr) {
46             span_lint_and_help(
47                 cx,
48                 VERBOSE_FILE_READS,
49                 expr.span,
50                 "use of `File::read_to_string`",
51                 "consider using `fs::read_to_string` instead",
52             )
53         }
54     }
55 }
56
57 fn is_file_read_to_end<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr<'tcx>) -> bool {
58     if_chain! {
59         if let ExprKind::MethodCall(method_name, _, exprs) = expr.kind;
60         if method_name.ident.as_str() == "read_to_end";
61         if let ExprKind::Path(QPath::Resolved(None, _)) = &exprs[0].kind;
62         let ty = cx.tables.expr_ty(&exprs[0]);
63         if match_type(cx, ty, &paths::FILE);
64         then {
65             return true
66         }
67     }
68     false
69 }
70
71 fn is_file_read_to_string<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr<'tcx>) -> bool {
72     if_chain! {
73         if let ExprKind::MethodCall(method_name, _, exprs) = expr.kind;
74         if method_name.ident.as_str() == "read_to_string";
75         if let ExprKind::Path(QPath::Resolved(None, _)) = &exprs[0].kind;
76         let ty = cx.tables.expr_ty(&exprs[0]);
77         if match_type(cx, ty, &paths::FILE);
78         then {
79             return true
80         }
81     }
82     false
83 }