add CR feedback

This commit is contained in:
Jacob Meyers 2020-03-10 05:41:24 -04:00
parent 0f7f30711e
commit a4ba1027fc
3 changed files with 20 additions and 21 deletions

View file

@ -8,19 +8,22 @@ declare_clippy_lint! {
/// **What it does:** Checks for use of File::read_to_end and File::read_to_string.
///
/// **Why is this bad?** `fs::{read, read_to_string}` provide the same functionality when `buf` is empty with fewer imports and no intermediate values.
///
/// 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)
/// **Known problems:** None.
///
/// **Example:**
///
/// ```rust, ignore
/// let mut f = File::open("foo.txt")?;
/// ```rust,no_run
/// # use std::io::Read;
/// # use std::fs::File;
/// let mut f = File::open("foo.txt").unwrap();
/// let mut bytes = Vec::new();
/// f.read_to_end(&mut bytes)?;
/// f.read_to_end(&mut bytes).unwrap();
/// ```
/// Can be written more concisely as
/// ```rust, ignore
/// let mut bytes = fs::read("foo.txt")?;
/// ```rust,no_run
/// # use std::fs;
/// let mut bytes = fs::read("foo.txt").unwrap();
/// ```
pub VERBOSE_FILE_READS,
complexity,
@ -36,19 +39,17 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for VerboseFileReads {
cx,
VERBOSE_FILE_READS,
expr.span,
"use of File::read_to_end",
"consider using fs::read instead",
"use of `File::read_to_end`",
"consider using `fs::read` instead",
);
} else if is_file_read_to_string(cx, expr) {
span_lint_and_help(
cx,
VERBOSE_FILE_READS,
expr.span,
"use of File::read_to_string",
"consider using fs::read_to_string instead",
"use of `File::read_to_string`",
"consider using `fs::read_to_string` instead",
)
} else {
// Don't care
}
}
}

View file

@ -12,9 +12,7 @@ impl Struct {
}
fn main() -> std::io::Result<()> {
let mut path = temp_dir();
path.push("test.txt");
let file = File::create(&path)?;
let path = "foo.txt";
// Lint shouldn't catch this
let s = Struct;
s.read_to_end();

View file

@ -1,19 +1,19 @@
error: use of File::read_to_end
--> $DIR/verbose_file_reads.rs:25:5
error: use of `File::read_to_end`
--> $DIR/verbose_file_reads.rs:23:5
|
LL | f.read_to_end(&mut buffer)?;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: `-D clippy::verbose-file-reads` implied by `-D warnings`
= help: consider using fs::read instead
= help: consider using `fs::read` instead
error: use of File::read_to_string
--> $DIR/verbose_file_reads.rs:28:5
error: use of `File::read_to_string`
--> $DIR/verbose_file_reads.rs:26:5
|
LL | f.read_to_string(&mut string_buffer)?;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: consider using fs::read_to_string instead
= help: consider using `fs::read_to_string` instead
error: aborting due to 2 previous errors