Skip to content

Commit f7f4bd5

Browse files
committed
Move VerboseFileReads into Methods lint pass
1 parent f190e95 commit f7f4bd5

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
@@ -364,6 +364,7 @@ store.register_lints(&[
364364
methods::UNWRAP_USED,
365365
methods::USELESS_ASREF,
366366
methods::VEC_RESIZE_TO_ZERO,
367+
methods::VERBOSE_FILE_READS,
367368
methods::WRONG_SELF_CONVENTION,
368369
methods::ZST_OFFSET,
369370
minmax::MIN_MAX,
@@ -577,7 +578,6 @@ store.register_lints(&[
577578
useless_conversion::USELESS_CONVERSION,
578579
vec::USELESS_VEC,
579580
vec_init_then_push::VEC_INIT_THEN_PUSH,
580-
verbose_file_reads::VERBOSE_FILE_READS,
581581
wildcard_imports::ENUM_GLOB_USE,
582582
wildcard_imports::WILDCARD_IMPORTS,
583583
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_early::SEPARATED_LITERAL_SUFFIX),
4344
LintId::of(misc_early::UNNEEDED_FIELD_PATTERN),
4445
LintId::of(misc_early::UNSEPARATED_LITERAL_SUFFIX),
@@ -79,7 +80,6 @@ store.register_group(true, "clippy::restriction", Some("clippy_restriction"), ve
7980
LintId::of(unicode::NON_ASCII_LITERAL),
8081
LintId::of(unnecessary_self_imports::UNNECESSARY_SELF_IMPORTS),
8182
LintId::of(unwrap_in_result::UNWRAP_IN_RESULT),
82-
LintId::of(verbose_file_reads::VERBOSE_FILE_READS),
8383
LintId::of(write::PRINT_STDERR),
8484
LintId::of(write::PRINT_STDOUT),
8585
LintId::of(write::USE_DEBUG),

clippy_lints/src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,6 @@ mod use_self;
388388
mod useless_conversion;
389389
mod vec;
390390
mod vec_init_then_push;
391-
mod verbose_file_reads;
392391
mod wildcard_imports;
393392
mod write;
394393
mod zero_div_zero;
@@ -777,7 +776,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
777776
store.register_early_pass(|| Box::new(option_env_unwrap::OptionEnvUnwrap));
778777
let warn_on_all_wildcard_imports = conf.warn_on_all_wildcard_imports;
779778
store.register_late_pass(move || Box::new(wildcard_imports::WildcardImports::new(warn_on_all_wildcard_imports)));
780-
store.register_late_pass(|| Box::new(verbose_file_reads::VerboseFileReads));
781779
store.register_late_pass(|| Box::new(redundant_pub_crate::RedundantPubCrate::default()));
782780
store.register_late_pass(|| Box::new(unnamed_address::UnnamedAddress));
783781
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

@@ -2823,6 +2824,33 @@ declare_clippy_lint! {
28232824
"emptying a vector with `resize(0, an_int)` instead of `clear()` is probably an argument inversion mistake"
28242825
}
28252826

2827+
declare_clippy_lint! {
2828+
/// ### What it does
2829+
/// Checks for use of File::read_to_end and File::read_to_string.
2830+
///
2831+
/// ### Why is this bad?
2832+
/// `fs::{read, read_to_string}` provide the same functionality when `buf` is empty with fewer imports and no intermediate values.
2833+
/// 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)
2834+
///
2835+
/// ### Example
2836+
/// ```rust,no_run
2837+
/// # use std::io::Read;
2838+
/// # use std::fs::File;
2839+
/// let mut f = File::open("foo.txt").unwrap();
2840+
/// let mut bytes = Vec::new();
2841+
/// f.read_to_end(&mut bytes).unwrap();
2842+
/// ```
2843+
/// Can be written more concisely as
2844+
/// ```rust,no_run
2845+
/// # use std::fs;
2846+
/// let mut bytes = fs::read("foo.txt").unwrap();
2847+
/// ```
2848+
#[clippy::version = "1.44.0"]
2849+
pub VERBOSE_FILE_READS,
2850+
restriction,
2851+
"use of `File::read_to_end` or `File::read_to_string`"
2852+
}
2853+
28262854
pub struct Methods {
28272855
avoid_breaking_exported_api: bool,
28282856
msrv: Option<RustcVersion>,
@@ -2940,6 +2968,7 @@ impl_lint_pass!(Methods => [
29402968
UNIT_HASH,
29412969
UNNECESSARY_SORT_BY,
29422970
VEC_RESIZE_TO_ZERO,
2971+
VERBOSE_FILE_READS,
29432972
]);
29442973

29452974
/// Extracts a method call name, args, and `Span` of the method name.
@@ -3327,6 +3356,12 @@ impl Methods {
33273356
("push", [arg]) => {
33283357
path_buf_push_overwrite::check(cx, expr, arg);
33293358
},
3359+
("read_to_end", [_]) => {
3360+
verbose_file_reads::check(cx, expr, recv, verbose_file_reads::READ_TO_END_MSG);
3361+
},
3362+
("read_to_string", [_]) => {
3363+
verbose_file_reads::check(cx, expr, recv, verbose_file_reads::READ_TO_STRING_MSG);
3364+
},
33303365
("repeat", [arg]) => {
33313366
repeat_once::check(cx, expr, recv, arg);
33323367
},
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)