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