diff --git a/clippy_lints/src/verbose_file_reads.rs b/clippy_lints/src/verbose_file_reads.rs index 93256790211a..37885317c58e 100644 --- a/clippy_lints/src/verbose_file_reads.rs +++ b/clippy_lints/src/verbose_file_reads.rs @@ -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 } } } diff --git a/tests/ui/verbose_file_reads.rs b/tests/ui/verbose_file_reads.rs index 3c7c4be84b0c..e0065e05ade6 100644 --- a/tests/ui/verbose_file_reads.rs +++ b/tests/ui/verbose_file_reads.rs @@ -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(); diff --git a/tests/ui/verbose_file_reads.stderr b/tests/ui/verbose_file_reads.stderr index 73dc22fd4dbc..550b6ab679f1 100644 --- a/tests/ui/verbose_file_reads.stderr +++ b/tests/ui/verbose_file_reads.stderr @@ -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