]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/verbose_file_reads.rs
Merge remote-tracking branch 'upstream/beta' into backport_remerge
[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     ///
13     /// **Known problems:** None.
14     ///
15     /// **Example:**
16     ///
17     /// ```rust,no_run
18     /// # use std::io::Read;
19     /// # use std::fs::File;
20     /// let mut f = File::open("foo.txt").unwrap();
21     /// let mut bytes = Vec::new();
22     /// f.read_to_end(&mut bytes).unwrap();
23     /// ```
24     /// Can be written more concisely as
25     /// ```rust,no_run
26     /// # use std::fs;
27     /// let mut bytes = fs::read("foo.txt").unwrap();
28     /// ```
29     pub VERBOSE_FILE_READS,
30     restriction,
31     "use of `File::read_to_end` or `File::read_to_string`"
32 }
33
34 declare_lint_pass!(VerboseFileReads => [VERBOSE_FILE_READS]);
35
36 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for VerboseFileReads {
37     fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr<'tcx>) {
38         if is_file_read_to_end(cx, expr) {
39             span_lint_and_help(
40                 cx,
41                 VERBOSE_FILE_READS,
42                 expr.span,
43                 "use of `File::read_to_end`",
44                 None,
45                 "consider using `fs::read` instead",
46             );
47         } else if is_file_read_to_string(cx, expr) {
48             span_lint_and_help(
49                 cx,
50                 VERBOSE_FILE_READS,
51                 expr.span,
52                 "use of `File::read_to_string`",
53                 None,
54                 "consider using `fs::read_to_string` instead",
55             )
56         }
57     }
58 }
59
60 fn is_file_read_to_end<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr<'tcx>) -> bool {
61     if_chain! {
62         if let ExprKind::MethodCall(method_name, _, exprs) = expr.kind;
63         if method_name.ident.as_str() == "read_to_end";
64         if let ExprKind::Path(QPath::Resolved(None, _)) = &exprs[0].kind;
65         let ty = cx.tables.expr_ty(&exprs[0]);
66         if match_type(cx, ty, &paths::FILE);
67         then {
68             return true
69         }
70     }
71     false
72 }
73
74 fn is_file_read_to_string<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr<'tcx>) -> bool {
75     if_chain! {
76         if let ExprKind::MethodCall(method_name, _, exprs) = expr.kind;
77         if method_name.ident.as_str() == "read_to_string";
78         if let ExprKind::Path(QPath::Resolved(None, _)) = &exprs[0].kind;
79         let ty = cx.tables.expr_ty(&exprs[0]);
80         if match_type(cx, ty, &paths::FILE);
81         then {
82             return true
83         }
84     }
85     false
86 }