-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
85bbba6
commit fecee63
Showing
8 changed files
with
127 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
use super::UNBUFFERED_BYTES; | ||
use clippy_utils::diagnostics::span_lint_and_help; | ||
use clippy_utils::ty::implements_trait; | ||
use clippy_utils::{get_trait_def_id, is_trait_method}; | ||
use rustc_hir as hir; | ||
use rustc_lint::LateContext; | ||
use rustc_span::sym; | ||
|
||
/// lint to detect `.bytes()` on an unbuffered type such as a `File` or `TcpStream` | ||
pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, recv: &hir::Expr<'_>) { | ||
// Retrieve the DefId of the BufRead trait | ||
// Sadly BufRead does not have a diagnostic item or a lang item | ||
let Some(buf_read) = get_trait_def_id(cx.tcx, &["std", "io", "BufRead"]) else { | ||
return; | ||
}; | ||
|
||
let typ = cx.typeck_results().expr_ty_adjusted(recv); | ||
if | ||
// The .bytes() call is a call from the given trait | ||
is_trait_method(cx, expr, sym::IoRead) | ||
// And the implementor of the trait is not buffered | ||
&& !implements_trait(cx, typ, buf_read, &[]) | ||
{ | ||
span_lint_and_help( | ||
cx, | ||
UNBUFFERED_BYTES, | ||
expr.span, | ||
"calling .bytes() is very inefficient when data is not in memory", | ||
None, | ||
"consider using `BufReader`", | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#![warn(clippy::unbuffered_bytes)] | ||
|
||
use std::fs::File; | ||
use std::io::{BufReader, Cursor, Read}; | ||
use std::net::TcpStream; | ||
|
||
fn main() { | ||
// File is not buffered, should complain | ||
let file = File::open("./bytes.txt").unwrap(); | ||
file.bytes(); | ||
|
||
// TcpStream is not buffered, should complain | ||
let tcp_stream: TcpStream = todo!(); | ||
tcp_stream.bytes(); | ||
|
||
// BufReader<File> is buffered, should not complain | ||
let file = BufReader::new(File::open("./bytes.txt").unwrap()); | ||
file.bytes(); | ||
|
||
// Cursor is buffered, should not complain | ||
let cursor = Cursor::new(Vec::new()); | ||
cursor.bytes(); | ||
} | ||
|
||
fn use_read<R: Read>(r: R) { | ||
// Callers of `use_read` may choose a `R` that is not buffered | ||
r.bytes(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
error: calling .bytes() is very inefficient when data is not in memory | ||
--> tests/ui/unbuffered_bytes.rs:10:5 | ||
| | ||
LL | file.bytes(); | ||
| ^^^^^^^^^^^^ | ||
| | ||
= help: consider using `BufReader` | ||
= note: `-D clippy::unbuffered-bytes` implied by `-D warnings` | ||
= help: to override `-D warnings` add `#[allow(clippy::unbuffered_bytes)]` | ||
|
||
error: calling .bytes() is very inefficient when data is not in memory | ||
--> tests/ui/unbuffered_bytes.rs:14:5 | ||
| | ||
LL | tcp_stream.bytes(); | ||
| ^^^^^^^^^^^^^^^^^^ | ||
| | ||
= help: consider using `BufReader` | ||
|
||
error: calling .bytes() is very inefficient when data is not in memory | ||
--> tests/ui/unbuffered_bytes.rs:27:5 | ||
| | ||
LL | r.bytes(); | ||
| ^^^^^^^^^ | ||
| | ||
= help: consider using `BufReader` | ||
|
||
error: aborting due to 3 previous errors | ||
|