Skip to content

Commit b696631

Browse files
committed
Move VerboseFileReads into Methods lint pass
1 parent 69203c0 commit b696631

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,
@@ -574,7 +575,6 @@ store.register_lints(&[
574575
useless_conversion::USELESS_CONVERSION,
575576
vec::USELESS_VEC,
576577
vec_init_then_push::VEC_INIT_THEN_PUSH,
577-
verbose_file_reads::VERBOSE_FILE_READS,
578578
wildcard_imports::ENUM_GLOB_USE,
579579
wildcard_imports::WILDCARD_IMPORTS,
580580
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
@@ -38,6 +38,7 @@ store.register_group(true, "clippy::restriction", Some("clippy_restriction"), ve
3838
LintId::of(methods::GET_UNWRAP),
3939
LintId::of(methods::MAP_ERR_IGNORE),
4040
LintId::of(methods::UNWRAP_USED),
41+
LintId::of(methods::VERBOSE_FILE_READS),
4142
LintId::of(misc_early::SEPARATED_LITERAL_SUFFIX),
4243
LintId::of(misc_early::UNNEEDED_FIELD_PATTERN),
4344
LintId::of(misc_early::UNSEPARATED_LITERAL_SUFFIX),
@@ -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
@@ -387,7 +387,6 @@ mod use_self;
387387
mod useless_conversion;
388388
mod vec;
389389
mod vec_init_then_push;
390-
mod verbose_file_reads;
391390
mod wildcard_imports;
392391
mod write;
393392
mod zero_div_zero;
@@ -776,7 +775,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
776775
store.register_early_pass(|| Box::new(option_env_unwrap::OptionEnvUnwrap));
777776
let warn_on_all_wildcard_imports = conf.warn_on_all_wildcard_imports;
778777
store.register_late_pass(move || Box::new(wildcard_imports::WildcardImports::new(warn_on_all_wildcard_imports)));
779-
store.register_late_pass(|| Box::new(verbose_file_reads::VerboseFileReads));
780778
store.register_late_pass(|| Box::new(redundant_pub_crate::RedundantPubCrate::default()));
781779
store.register_late_pass(|| Box::new(unnamed_address::UnnamedAddress));
782780
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

@@ -2819,6 +2820,33 @@ declare_clippy_lint! {
28192820
"emptying a vector with `resize(0, an_int)` instead of `clear()` is probably an argument inversion mistake"
28202821
}
28212822

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

29412970
/// Extracts a method call name, args, and `Span` of the method name.
@@ -3323,6 +3352,12 @@ impl Methods {
33233352
("push", [arg]) => {
33243353
path_buf_push_overwrite::check(cx, expr, arg);
33253354
},
3355+
("read_to_end", [_]) => {
3356+
verbose_file_reads::check(cx, expr, recv, verbose_file_reads::READ_TO_END_MSG);
3357+
},
3358+
("read_to_string", [_]) => {
3359+
verbose_file_reads::check(cx, expr, recv, verbose_file_reads::READ_TO_STRING_MSG);
3360+
},
33263361
("repeat", [arg]) => {
33273362
repeat_once::check(cx, expr, recv, arg);
33283363
},
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)