Skip to content

Commit a0fab5c

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

2 files changed

Lines changed: 37 additions & 1 deletion

File tree

‎clippy_lints/src/unused_io_amount.rs‎

Lines changed: 23 additions & 1 deletion
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
fncheck_block(&mutself,cx:&LateContext<'tcx>,block:&'tcx hir::Block<'tcx>){
86+
let fn_def_id = block.hir_id.owner.to_def_id();
87+
ifletSome(impl_id) = cx.tcx.impl_of_method(fn_def_id)
88+
&& letSome(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
iflet hir::StmtKind::Semi(exp) = stmt.kind{
88110
check_expr(cx, exp);

‎tests/ui/unused_io_amount.rs‎

Lines changed: 14 additions & 0 deletions
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+
structReader{}
281+
282+
implReadforReader{
283+
fnread(&mutself,buf:&mut[u8]) -> io::Result<usize>{
284+
todo!()
285+
}
286+
287+
fnread_exact(&mutself,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
fnmain(){}

0 commit comments

Comments
 (0)