summaryrefslogtreecommitdiffstats
path: root/src/tools/clippy/clippy_lints/src/methods/verbose_file_reads.rs
blob: 2fe5ae9a9ad8fda3a9c80f8143ac603764d1ad8b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
use clippy_utils::diagnostics::span_lint_and_help;
use clippy_utils::is_trait_method;
use clippy_utils::ty::is_type_diagnostic_item;
use rustc_hir::{Expr, ExprKind, QPath};
use rustc_lint::LateContext;
use rustc_span::sym;

use super::VERBOSE_FILE_READS;

pub(super) const READ_TO_END_MSG: (&str, &str) = ("use of `File::read_to_end`", "consider using `fs::read` instead");
pub(super) const READ_TO_STRING_MSG: (&str, &str) = (
    "use of `File::read_to_string`",
    "consider using `fs::read_to_string` instead",
);

pub(super) fn check<'tcx>(
    cx: &LateContext<'tcx>,
    expr: &'tcx Expr<'_>,
    recv: &'tcx Expr<'_>,
    (msg, help): (&str, &str),
) {
    if is_trait_method(cx, expr, sym::IoRead)
        && matches!(recv.kind, ExprKind::Path(QPath::Resolved(None, _)))
        && is_type_diagnostic_item(cx, cx.typeck_results().expr_ty_adjusted(recv).peel_refs(), sym::File)
    {
        span_lint_and_help(cx, VERBOSE_FILE_READS, expr.span, msg, None, help);
    }
}