Skip to content

Commit a0fab5c

Browse files
committed
Auto merge of rust-lang#13605 - GnomedDev:read-exact-in-read-exact, r=llogiq
Stop linting unused_io_amount in io traits Closes rust-lang#4836 changelog: [`unused_io_amount`] No longer lints inside `io::Read`/`io::Write`, and async variants.
2 parents 3caff99 + 560353c commit a0fab5c

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

clippy_lints/src/unused_io_amount.rs

+23-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use clippy_utils::diagnostics::span_lint_hir_and_then;
22
use clippy_utils::macros::{is_panic, root_macro_call_first_node};
3-
use clippy_utils::{is_res_lang_ctor, is_trait_method, match_trait_method, paths, peel_blocks};
3+
use clippy_utils::{is_res_lang_ctor, is_trait_method, match_def_path, match_trait_method, paths, peel_blocks};
44
use hir::{ExprKind, HirId, PatKind};
55
use rustc_hir as hir;
66
use rustc_lint::{LateContext, LateLintPass};
@@ -83,6 +83,28 @@ impl<'tcx> LateLintPass<'tcx> for UnusedIoAmount {
8383
/// to consider the arms, and we want to avoid breaking the logic for situations where things
8484
/// get desugared to match.
8585
fn check_block(&mut self, cx: &LateContext<'tcx>, block: &'tcx hir::Block<'tcx>) {
86+
let fn_def_id = block.hir_id.owner.to_def_id();
87+
if let Some(impl_id) = cx.tcx.impl_of_method(fn_def_id)
88+
&& let Some(trait_id) = cx.tcx.trait_id_of_impl(impl_id)
89+
{
90+
// We don't want to lint inside io::Read or io::Write implementations, as the author has more
91+
// information about their trait implementation than our lint, see https://github.com/rust-lang/rust-clippy/issues/4836
92+
if cx.tcx.is_diagnostic_item(sym::IoRead, trait_id) || cx.tcx.is_diagnostic_item(sym::IoWrite, trait_id) {
93+
return;
94+
}
95+
96+
let async_paths: [&[&str]; 4] = [
97+
&paths::TOKIO_IO_ASYNCREADEXT,
98+
&paths::TOKIO_IO_ASYNCWRITEEXT,
99+
&paths::FUTURES_IO_ASYNCREADEXT,
100+
&paths::FUTURES_IO_ASYNCWRITEEXT,
101+
];
102+
103+
if async_paths.into_iter().any(|path| match_def_path(cx, trait_id, path)) {
104+
return;
105+
}
106+
}
107+
86108
for stmt in block.stmts {
87109
if let hir::StmtKind::Semi(exp) = stmt.kind {
88110
check_expr(cx, exp);

tests/ui/unused_io_amount.rs

+14
Original file line numberDiff line numberDiff line change
@@ -277,4 +277,18 @@ fn allow_works<F: std::io::Read>(mut f: F) {
277277
f.read(&mut data).unwrap();
278278
}
279279

280+
struct Reader {}
281+
282+
impl Read for Reader {
283+
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
284+
todo!()
285+
}
286+
287+
fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> {
288+
// We shouldn't recommend using Read::read_exact inside Read::read_exact!
289+
self.read(buf).unwrap();
290+
Ok(())
291+
}
292+
}
293+
280294
fn main() {}

0 commit comments

Comments
 (0)