Skip to content

Commit adaa140

Browse files
Fix review comments
1 parent f7f450e commit adaa140

File tree

2 files changed

+19
-20
lines changed

2 files changed

+19
-20
lines changed

clippy_lints/src/methods/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4395,7 +4395,7 @@ declare_clippy_lint! {
43954395

43964396
declare_clippy_lint! {
43974397
/// ### What it does
4398-
/// Checks for `Read::bytes()` on an unbuffered Read type
4398+
/// Checks for calls to `Read::bytes` on types which don't implement `BufRead`.
43994399
///
44004400
/// ### Why is this bad?
44014401
/// The default implementation calls `read` for each byte, which can be very inefficient for data that’s not in memory, such as `File`.

clippy_lints/src/methods/unbuffered_bytes.rs

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,25 @@ use rustc_lint::LateContext;
77
use rustc_span::sym;
88

99
pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, recv: &hir::Expr<'_>) {
10-
// Retrieve the DefId of the BufRead trait
11-
// FIXME: add a diagnostic item for `BufRead`
12-
let Some(buf_read) = get_trait_def_id(cx.tcx, &paths::BUF_READ) else {
13-
return;
14-
};
10+
let ty = cx.typeck_results().expr_ty_adjusted(recv);
1511

16-
let typ = cx.typeck_results().expr_ty_adjusted(recv);
17-
if
18-
// The .bytes() call is a call from the given trait
19-
is_trait_method(cx, expr, sym::IoRead)
12+
// If the .bytes() call is a call from the Read trait
13+
if is_trait_method(cx, expr, sym::IoRead) {
14+
// Retrieve the DefId of the BufRead trait
15+
// FIXME: add a diagnostic item for `BufRead`
16+
let Some(buf_read) = get_trait_def_id(cx.tcx, &paths::BUF_READ) else {
17+
return;
18+
};
2019
// And the implementor of the trait is not buffered
21-
&& !implements_trait(cx, typ, buf_read, &[])
22-
{
23-
span_lint_and_help(
24-
cx,
25-
UNBUFFERED_BYTES,
26-
expr.span,
27-
"calling .bytes() is very inefficient when data is not in memory",
28-
None,
29-
"consider using `BufReader`",
30-
);
20+
if !implements_trait(cx, ty, buf_read, &[]) {
21+
span_lint_and_help(
22+
cx,
23+
UNBUFFERED_BYTES,
24+
expr.span,
25+
"calling .bytes() is very inefficient when data is not in memory",
26+
None,
27+
"consider using `BufReader`",
28+
);
29+
}
3130
}
3231
}

0 commit comments

Comments
 (0)