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