Skip to content

Commit f132cca

Browse files
committed
Move VerboseFileReads into Methods lint pass
1 parent d995730 commit f132cca

File tree

6 files changed

+65
-92
lines changed

6 files changed

+65
-92
lines changed

clippy_lints/src/lib.register_lints.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,7 @@ store.register_lints(&[
370370
methods::UNWRAP_USED,
371371
methods::USELESS_ASREF,
372372
methods::VEC_RESIZE_TO_ZERO,
373+
methods::VERBOSE_FILE_READS,
373374
methods::WRONG_SELF_CONVENTION,
374375
methods::ZST_OFFSET,
375376
minmax::MIN_MAX,
@@ -564,7 +565,6 @@ store.register_lints(&[
564565
useless_conversion::USELESS_CONVERSION,
565566
vec::USELESS_VEC,
566567
vec_init_then_push::VEC_INIT_THEN_PUSH,
567-
verbose_file_reads::VERBOSE_FILE_READS,
568568
wildcard_imports::ENUM_GLOB_USE,
569569
wildcard_imports::WILDCARD_IMPORTS,
570570
write::PRINTLN_EMPTY_STRING,

clippy_lints/src/lib.register_restriction.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ store.register_group(true, "clippy::restriction", Some("clippy_restriction"), ve
3939
LintId::of(methods::GET_UNWRAP),
4040
LintId::of(methods::MAP_ERR_IGNORE),
4141
LintId::of(methods::UNWRAP_USED),
42+
LintId::of(methods::VERBOSE_FILE_READS),
4243
LintId::of(misc::FLOAT_CMP_CONST),
4344
LintId::of(misc_early::SEPARATED_LITERAL_SUFFIX),
4445
LintId::of(misc_early::UNNEEDED_FIELD_PATTERN),
@@ -75,7 +76,6 @@ store.register_group(true, "clippy::restriction", Some("clippy_restriction"), ve
7576
LintId::of(unicode::NON_ASCII_LITERAL),
7677
LintId::of(unnecessary_self_imports::UNNECESSARY_SELF_IMPORTS),
7778
LintId::of(unwrap_in_result::UNWRAP_IN_RESULT),
78-
LintId::of(verbose_file_reads::VERBOSE_FILE_READS),
7979
LintId::of(write::PRINT_STDERR),
8080
LintId::of(write::PRINT_STDOUT),
8181
LintId::of(write::USE_DEBUG),

clippy_lints/src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,6 @@ mod use_self;
394394
mod useless_conversion;
395395
mod vec;
396396
mod vec_init_then_push;
397-
mod verbose_file_reads;
398397
mod wildcard_imports;
399398
mod write;
400399
mod zero_div_zero;
@@ -773,7 +772,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
773772
store.register_early_pass(|| Box::new(option_env_unwrap::OptionEnvUnwrap));
774773
let warn_on_all_wildcard_imports = conf.warn_on_all_wildcard_imports;
775774
store.register_late_pass(move || Box::new(wildcard_imports::WildcardImports::new(warn_on_all_wildcard_imports)));
776-
store.register_late_pass(|| Box::new(verbose_file_reads::VerboseFileReads));
777775
store.register_late_pass(|| Box::new(redundant_pub_crate::RedundantPubCrate::default()));
778776
store.register_late_pass(|| Box::new(unnamed_address::UnnamedAddress));
779777
store.register_late_pass(|| Box::new(dereference::Dereferencing::default()));

clippy_lints/src/methods/mod.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ mod unwrap_used;
8989
mod useless_asref;
9090
mod utils;
9191
mod vec_resize_to_zero;
92+
mod verbose_file_reads;
9293
mod wrong_self_convention;
9394
mod zst_offset;
9495

@@ -2783,6 +2784,33 @@ declare_clippy_lint! {
27832784
"emptying a vector with `resize(0, an_int)` instead of `clear()` is probably an argument inversion mistake"
27842785
}
27852786

2787+
declare_clippy_lint! {
2788+
/// ### What it does
2789+
/// Checks for use of File::read_to_end and File::read_to_string.
2790+
///
2791+
/// ### Why is this bad?
2792+
/// `fs::{read, read_to_string}` provide the same functionality when `buf` is empty with fewer imports and no intermediate values.
2793+
/// 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)
2794+
///
2795+
/// ### Example
2796+
/// ```rust,no_run
2797+
/// # use std::io::Read;
2798+
/// # use std::fs::File;
2799+
/// let mut f = File::open("foo.txt").unwrap();
2800+
/// let mut bytes = Vec::new();
2801+
/// f.read_to_end(&mut bytes).unwrap();
2802+
/// ```
2803+
/// Can be written more concisely as
2804+
/// ```rust,no_run
2805+
/// # use std::fs;
2806+
/// let mut bytes = fs::read("foo.txt").unwrap();
2807+
/// ```
2808+
#[clippy::version = "1.44.0"]
2809+
pub VERBOSE_FILE_READS,
2810+
restriction,
2811+
"use of `File::read_to_end` or `File::read_to_string`"
2812+
}
2813+
27862814
pub struct Methods {
27872815
avoid_breaking_exported_api: bool,
27882816
msrv: Option<RustcVersion>,
@@ -2900,6 +2928,7 @@ impl_lint_pass!(Methods => [
29002928
UNIT_HASH,
29012929
UNNECESSARY_SORT_BY,
29022930
VEC_RESIZE_TO_ZERO,
2931+
VERBOSE_FILE_READS,
29032932
]);
29042933

29052934
/// Extracts a method call name, args, and `Span` of the method name.
@@ -3287,6 +3316,12 @@ impl Methods {
32873316
("push", [arg]) => {
32883317
path_buf_push_overwrite::check(cx, expr, arg);
32893318
},
3319+
("read_to_end", []) => {
3320+
verbose_file_reads::check(cx, expr, recv, verbose_file_reads::READ_TO_END_MSG);
3321+
},
3322+
("read_to_string", []) => {
3323+
verbose_file_reads::check(cx, expr, recv, verbose_file_reads::READ_TO_STRING_MSG);
3324+
},
32903325
("repeat", [arg]) => {
32913326
repeat_once::check(cx, expr, recv, arg);
32923327
},
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
}

clippy_lints/src/verbose_file_reads.rs

Lines changed: 0 additions & 88 deletions
This file was deleted.

0 commit comments

Comments
 (0)