]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_lints/src/methods/verbose_file_reads.rs
Auto merge of #103828 - cassaundra:fix-format-args-span2, r=cjgillot
[rust.git] / src / tools / clippy / clippy_lints / src / methods / verbose_file_reads.rs
1 use clippy_utils::diagnostics::span_lint_and_help;
2 use clippy_utils::is_trait_method;
3 use clippy_utils::ty::is_type_diagnostic_item;
4 use rustc_hir::{Expr, ExprKind, QPath};
5 use rustc_lint::LateContext;
6 use rustc_span::sym;
7
8 use super::VERBOSE_FILE_READS;
9
10 pub(super) const READ_TO_END_MSG: (&str, &str) = ("use of `File::read_to_end`", "consider using `fs::read` instead");
11 pub(super) const READ_TO_STRING_MSG: (&str, &str) = (
12     "use of `File::read_to_string`",
13     "consider using `fs::read_to_string` instead",
14 );
15
16 pub(super) fn check<'tcx>(
17     cx: &LateContext<'tcx>,
18     expr: &'tcx Expr<'_>,
19     recv: &'tcx Expr<'_>,
20     (msg, help): (&str, &str),
21 ) {
22     if is_trait_method(cx, expr, sym::IoRead)
23         && matches!(recv.kind, ExprKind::Path(QPath::Resolved(None, _)))
24         && is_type_diagnostic_item(cx, cx.typeck_results().expr_ty_adjusted(recv).peel_refs(), sym::File)
25     {
26         span_lint_and_help(cx, VERBOSE_FILE_READS, expr.span, msg, None, help);
27     }
28 }