rust-lang / rust-lang/rust-clippy

verbose_file_reads suggestion isn't applicable

Open
#9,780 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug I-false-positive
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

Summary

The provided reproducer has two issues that prevent the suggestion from being applicable:

  1. The file is opened in a different function, meaning we don't have the filename in a local scope, and so can't pass it to fs::read.
  2. We are calling seek on the file before reading, using fs::read would make this impossible.
Lint Name

verbose_file_reads

Reproducer

I tried this code:

use std::fs::File;
use std::io::Read;
use std::io::Seek;
use std::io::SeekFrom;

fn foo(mut file: &File) {
    let mut file_contents = Vec::new();
    file.seek(SeekFrom::Start(10)).unwrap();
    file.read_to_end(&mut file_contents).unwrap();
}

fn main() {
    foo(&File::open("foo.txt").unwrap());
}

I saw this happen:

warning: use of `File::read_to_end`
 --> src\main.rs:9:5
  |
9 |     file.read_to_end(&mut file_contents).unwrap();
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: requested on the command line with `-W clippy::verbose-file-reads`
  = help: consider using `fs::read` instead
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#verbose_file_reads

I expected to see this happen:
No warnings, or at least a usable suggestion.

Version
rustc 1.65.0 (897e37553 2022-11-02)
binary: rustc
commit-hash: 897e37553bba8b42751c67658967889d11ecd120
commit-date: 2022-11-02
host: x86_64-pc-windows-msvc
release: 1.65.0
LLVM version: 15.0.0
Additional Labels

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the verbose_file_reads warning with the supplied Rust example, focusing on the separate file-opening function and the seek before reading. Inspect how this lint decides when to suggest fs::read; done means the example produces no warning or receives a usable suggestion.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.