Skip to content

Commit d8808db

Browse files
committed
Move VerboseFileReads into Methods lint pass
1 parent 8acc4d2 commit d8808db

File tree

6 files changed

+65
-92
lines changed

6 files changed

+65
-92
lines changed

clippy_lints/src/lib.register_lints.rs

+1-1
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,
@@ -585,7 +586,6 @@ store.register_lints(&[
585586
useless_conversion::USELESS_CONVERSION,
586587
vec::USELESS_VEC,
587588
vec_init_then_push::VEC_INIT_THEN_PUSH,
588-
verbose_file_reads::VERBOSE_FILE_READS,
589589
wildcard_imports::ENUM_GLOB_USE,
590590
wildcard_imports::WILDCARD_IMPORTS,
591591
write::POSITIONAL_NAMED_FORMAT_PARAMETERS,

clippy_lints/src/lib.register_restriction.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ store.register_group(true, "clippy::restriction", Some("clippy_restriction"), ve
4040
LintId::of(methods::GET_UNWRAP),
4141
LintId::of(methods::MAP_ERR_IGNORE),
4242
LintId::of(methods::UNWRAP_USED),
43+
LintId::of(methods::VERBOSE_FILE_READS),
4344
LintId::of(misc_early::SEPARATED_LITERAL_SUFFIX),
4445
LintId::of(misc_early::UNNEEDED_FIELD_PATTERN),
4546
LintId::of(misc_early::UNSEPARATED_LITERAL_SUFFIX),
@@ -81,7 +82,6 @@ store.register_group(true, "clippy::restriction", Some("clippy_restriction"), ve
8182
LintId::of(unicode::NON_ASCII_LITERAL),
8283
LintId::of(unnecessary_self_imports::UNNECESSARY_SELF_IMPORTS),
8384
LintId::of(unwrap_in_result::UNWRAP_IN_RESULT),
84-
LintId::of(verbose_file_reads::VERBOSE_FILE_READS),
8585
LintId::of(write::PRINT_STDERR),
8686
LintId::of(write::PRINT_STDOUT),
8787
LintId::of(write::USE_DEBUG),

clippy_lints/src/lib.rs

-2
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,6 @@ mod use_self;
391391
mod useless_conversion;
392392
mod vec;
393393
mod vec_init_then_push;
394-
mod verbose_file_reads;
395394
mod wildcard_imports;
396395
mod write;
397396
mod zero_div_zero;
@@ -792,7 +791,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
792791
store.register_early_pass(|| Box::new(option_env_unwrap::OptionEnvUnwrap));
793792
let warn_on_all_wildcard_imports = conf.warn_on_all_wildcard_imports;
794793
store.register_late_pass(move || Box::new(wildcard_imports::WildcardImports::new(warn_on_all_wildcard_imports)));
795-
store.register_late_pass(|| Box::new(verbose_file_reads::VerboseFileReads));
796794
store.register_late_pass(|| Box::new(redundant_pub_crate::RedundantPubCrate::default()));
797795
store.register_late_pass(|| Box::new(unnamed_address::UnnamedAddress));
798796
store.register_late_pass(move || Box::new(dereference::Dereferencing::new(msrv)));

clippy_lints/src/methods/mod.rs

+35
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ mod unwrap_used;
9191
mod useless_asref;
9292
mod utils;
9393
mod vec_resize_to_zero;
94+
mod verbose_file_reads;
9495
mod wrong_self_convention;
9596
mod zst_offset;
9697

@@ -2930,6 +2931,33 @@ declare_clippy_lint! {
29302931
"emptying a vector with `resize(0, an_int)` instead of `clear()` is probably an argument inversion mistake"
29312932
}
29322933

2934+
declare_clippy_lint! {
2935+
/// ### What it does
2936+
/// Checks for use of File::read_to_end and File::read_to_string.
2937+
///
2938+
/// ### Why is this bad?
2939+
/// `fs::{read, read_to_string}` provide the same functionality when `buf` is empty with fewer imports and no intermediate values.
2940+
/// 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)
2941+
///
2942+
/// ### Example
2943+
/// ```rust,no_run
2944+
/// # use std::io::Read;
2945+
/// # use std::fs::File;
2946+
/// let mut f = File::open("foo.txt").unwrap();
2947+
/// let mut bytes = Vec::new();
2948+
/// f.read_to_end(&mut bytes).unwrap();
2949+
/// ```
2950+
/// Can be written more concisely as
2951+
/// ```rust,no_run
2952+
/// # use std::fs;
2953+
/// let mut bytes = fs::read("foo.txt").unwrap();
2954+
/// ```
2955+
#[clippy::version = "1.44.0"]
2956+
pub VERBOSE_FILE_READS,
2957+
restriction,
2958+
"use of `File::read_to_end` or `File::read_to_string`"
2959+
}
2960+
29332961
pub struct Methods {
29342962
avoid_breaking_exported_api: bool,
29352963
msrv: Option<RustcVersion>,
@@ -3050,6 +3078,7 @@ impl_lint_pass!(Methods => [
30503078
UNIT_HASH,
30513079
UNNECESSARY_SORT_BY,
30523080
VEC_RESIZE_TO_ZERO,
3081+
VERBOSE_FILE_READS,
30533082
]);
30543083

30553084
/// Extracts a method call name, args, and `Span` of the method name.
@@ -3441,6 +3470,12 @@ impl Methods {
34413470
("push", [arg]) => {
34423471
path_buf_push_overwrite::check(cx, expr, arg);
34433472
},
3473+
("read_to_end", [_]) => {
3474+
verbose_file_reads::check(cx, expr, recv, verbose_file_reads::READ_TO_END_MSG);
3475+
},
3476+
("read_to_string", [_]) => {
3477+
verbose_file_reads::check(cx, expr, recv, verbose_file_reads::READ_TO_STRING_MSG);
3478+
},
34443479
("repeat", [arg]) => {
34453480
repeat_once::check(cx, expr, recv, arg);
34463481
},
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

-88
This file was deleted.

0 commit comments

Comments
 (0)