Skip to content

Commit 6e779a8

Browse files
committed
Move VerboseFileReads into Methods lint pass
1 parent e8665ed commit 6e779a8

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,
@@ -565,7 +566,6 @@ store.register_lints(&[
565566
useless_conversion::USELESS_CONVERSION,
566567
vec::USELESS_VEC,
567568
vec_init_then_push::VEC_INIT_THEN_PUSH,
568-
verbose_file_reads::VERBOSE_FILE_READS,
569569
wildcard_imports::ENUM_GLOB_USE,
570570
wildcard_imports::WILDCARD_IMPORTS,
571571
write::PRINTLN_EMPTY_STRING,

clippy_lints/src/lib.register_restriction.rs

+1-1
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

-2
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,6 @@ mod use_self;
396396
mod useless_conversion;
397397
mod vec;
398398
mod vec_init_then_push;
399-
mod verbose_file_reads;
400399
mod wildcard_imports;
401400
mod write;
402401
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

+35
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

@@ -2817,6 +2818,33 @@ declare_clippy_lint! {
28172818
"emptying a vector with `resize(0, an_int)` instead of `clear()` is probably an argument inversion mistake"
28182819
}
28192820

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

29392968
/// Extracts a method call name, args, and `Span` of the method name.
@@ -3321,6 +3350,12 @@ impl Methods {
33213350
("push", [arg]) => {
33223351
path_buf_push_overwrite::check(cx, expr, arg);
33233352
},
3353+
("read_to_end", [_]) => {
3354+
verbose_file_reads::check(cx, expr, recv, verbose_file_reads::READ_TO_END_MSG);
3355+
},
3356+
("read_to_string", [_]) => {
3357+
verbose_file_reads::check(cx, expr, recv, verbose_file_reads::READ_TO_STRING_MSG);
3358+
},
33243359
("repeat", [arg]) => {
33253360
repeat_once::check(cx, expr, recv, arg);
33263361
},
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)