]> git.proxmox.com Git - rustc.git/blame - src/tools/clippy/clippy_lints/src/verbose_file_reads.rs
New upstream version 1.56.0~beta.4+dfsg1
[rustc.git] / src / tools / clippy / clippy_lints / src / verbose_file_reads.rs
CommitLineData
cdc7bbd5
XL
1use clippy_utils::diagnostics::span_lint_and_help;
2use clippy_utils::paths;
3use clippy_utils::ty::match_type;
f20569fa
XL
4use if_chain::if_chain;
5use rustc_hir::{Expr, ExprKind, QPath};
6use rustc_lint::{LateContext, LateLintPass};
7use rustc_session::{declare_lint_pass, declare_tool_lint};
8
9declare_clippy_lint! {
94222f64
XL
10 /// ### What it does
11 /// Checks for use of File::read_to_end and File::read_to_string.
f20569fa 12 ///
94222f64
XL
13 /// ### Why is this bad?
14 /// `fs::{read, read_to_string}` provide the same functionality when `buf` is empty with fewer imports and no intermediate values.
f20569fa
XL
15 /// 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)
16 ///
94222f64 17 /// ### Example
f20569fa
XL
18 /// ```rust,no_run
19 /// # use std::io::Read;
20 /// # use std::fs::File;
21 /// let mut f = File::open("foo.txt").unwrap();
22 /// let mut bytes = Vec::new();
23 /// f.read_to_end(&mut bytes).unwrap();
24 /// ```
25 /// Can be written more concisely as
26 /// ```rust,no_run
27 /// # use std::fs;
28 /// let mut bytes = fs::read("foo.txt").unwrap();
29 /// ```
30 pub VERBOSE_FILE_READS,
31 restriction,
32 "use of `File::read_to_end` or `File::read_to_string`"
33}
34
35declare_lint_pass!(VerboseFileReads => [VERBOSE_FILE_READS]);
36
37impl<'tcx> LateLintPass<'tcx> for VerboseFileReads {
38 fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
39 if is_file_read_to_end(cx, expr) {
40 span_lint_and_help(
41 cx,
42 VERBOSE_FILE_READS,
43 expr.span,
44 "use of `File::read_to_end`",
45 None,
46 "consider using `fs::read` instead",
47 );
48 } else if is_file_read_to_string(cx, expr) {
49 span_lint_and_help(
50 cx,
51 VERBOSE_FILE_READS,
52 expr.span,
53 "use of `File::read_to_string`",
54 None,
55 "consider using `fs::read_to_string` instead",
17df50a5 56 );
f20569fa
XL
57 }
58 }
59}
60
61fn is_file_read_to_end<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) -> bool {
62 if_chain! {
63 if let ExprKind::MethodCall(method_name, _, exprs, _) = expr.kind;
64 if method_name.ident.as_str() == "read_to_end";
65 if let ExprKind::Path(QPath::Resolved(None, _)) = &exprs[0].kind;
66 let ty = cx.typeck_results().expr_ty(&exprs[0]);
67 if match_type(cx, ty, &paths::FILE);
68 then {
69 return true
70 }
71 }
72 false
73}
74
75fn is_file_read_to_string<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) -> bool {
76 if_chain! {
77 if let ExprKind::MethodCall(method_name, _, exprs, _) = expr.kind;
78 if method_name.ident.as_str() == "read_to_string";
79 if let ExprKind::Path(QPath::Resolved(None, _)) = &exprs[0].kind;
80 let ty = cx.typeck_results().expr_ty(&exprs[0]);
81 if match_type(cx, ty, &paths::FILE);
82 then {
83 return true
84 }
85 }
86 false
87}