Skip to content

Commit 0ab7acc

Browse files
committed
Auto merge of #7100 - ABouttefeux:unused_io_amount, r=camsteffen
Unused io amount detects `.read().ok()?` fixes #7096 changelog: unused_io_amount now detect expertion like `.read().ok()?`, `.read().or_else(|err| ...)?` and similar expressions.
2 parents 98e2b9f + 5625d58 commit 0ab7acc

File tree

3 files changed

+84
-7
lines changed

3 files changed

+84
-7
lines changed

clippy_lints/src/unused_io_amount.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,25 +47,35 @@ impl<'tcx> LateLintPass<'tcx> for UnusedIoAmount {
4747
func.kind,
4848
hir::ExprKind::Path(hir::QPath::LangItem(hir::LangItem::TryIntoResult, _))
4949
) {
50-
check_method_call(cx, &args[0], expr);
50+
check_map_error(cx, &args[0], expr);
5151
}
5252
} else {
53-
check_method_call(cx, res, expr);
53+
check_map_error(cx, res, expr);
5454
}
5555
},
56-
5756
hir::ExprKind::MethodCall(path, _, args, _) => match &*path.ident.as_str() {
5857
"expect" | "unwrap" | "unwrap_or" | "unwrap_or_else" => {
59-
check_method_call(cx, &args[0], expr);
58+
check_map_error(cx, &args[0], expr);
6059
},
6160
_ => (),
6261
},
63-
6462
_ => (),
6563
}
6664
}
6765
}
6866

67+
fn check_map_error(cx: &LateContext<'_>, call: &hir::Expr<'_>, expr: &hir::Expr<'_>) {
68+
let mut call = call;
69+
while let hir::ExprKind::MethodCall(ref path, _, ref args, _) = call.kind {
70+
if matches!(&*path.ident.as_str(), "or" | "or_else" | "ok") {
71+
call = &args[0];
72+
} else {
73+
break;
74+
}
75+
}
76+
check_method_call(cx, call, expr);
77+
}
78+
6979
fn check_method_call(cx: &LateContext<'_>, call: &hir::Expr<'_>, expr: &hir::Expr<'_>) {
7080
if let hir::ExprKind::MethodCall(path, _, _, _) = call.kind {
7181
let symbol = &*path.ident.as_str();

tests/ui/unused_io_amount.rs

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#![allow(dead_code)]
22
#![warn(clippy::unused_io_amount)]
33

4-
use std::io;
4+
use std::io::{self, Read};
55

66
fn question_mark<T: io::Read + io::Write>(s: &mut T) -> io::Result<()> {
77
s.write(b"test")?;
@@ -22,4 +22,43 @@ fn vectored<T: io::Read + io::Write>(s: &mut T) -> io::Result<()> {
2222
Ok(())
2323
}
2424

25+
fn ok(file: &str) -> Option<()> {
26+
let mut reader = std::fs::File::open(file).ok()?;
27+
let mut result = [0u8; 0];
28+
reader.read(&mut result).ok()?;
29+
Some(())
30+
}
31+
32+
#[allow(clippy::redundant_closure)]
33+
#[allow(clippy::bind_instead_of_map)]
34+
fn or_else(file: &str) -> io::Result<()> {
35+
let mut reader = std::fs::File::open(file)?;
36+
let mut result = [0u8; 0];
37+
reader.read(&mut result).or_else(|err| Err(err))?;
38+
Ok(())
39+
}
40+
41+
#[derive(Debug)]
42+
enum Error {
43+
Kind,
44+
}
45+
46+
fn or(file: &str) -> Result<(), Error> {
47+
let mut reader = std::fs::File::open(file).unwrap();
48+
let mut result = [0u8; 0];
49+
reader.read(&mut result).or(Err(Error::Kind))?;
50+
Ok(())
51+
}
52+
53+
fn combine_or(file: &str) -> Result<(), Error> {
54+
let mut reader = std::fs::File::open(file).unwrap();
55+
let mut result = [0u8; 0];
56+
reader
57+
.read(&mut result)
58+
.or(Err(Error::Kind))
59+
.or(Err(Error::Kind))
60+
.expect("error");
61+
Ok(())
62+
}
63+
2564
fn main() {}

tests/ui/unused_io_amount.stderr

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,33 @@ error: written amount is not handled
3636
LL | s.write_vectored(&[io::IoSlice::new(&[])])?;
3737
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3838

39-
error: aborting due to 6 previous errors
39+
error: read amount is not handled. Use `Read::read_exact` instead
40+
--> $DIR/unused_io_amount.rs:28:5
41+
|
42+
LL | reader.read(&mut result).ok()?;
43+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
44+
45+
error: read amount is not handled. Use `Read::read_exact` instead
46+
--> $DIR/unused_io_amount.rs:37:5
47+
|
48+
LL | reader.read(&mut result).or_else(|err| Err(err))?;
49+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
50+
51+
error: read amount is not handled. Use `Read::read_exact` instead
52+
--> $DIR/unused_io_amount.rs:49:5
53+
|
54+
LL | reader.read(&mut result).or(Err(Error::Kind))?;
55+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
56+
57+
error: read amount is not handled. Use `Read::read_exact` instead
58+
--> $DIR/unused_io_amount.rs:56:5
59+
|
60+
LL | / reader
61+
LL | | .read(&mut result)
62+
LL | | .or(Err(Error::Kind))
63+
LL | | .or(Err(Error::Kind))
64+
LL | | .expect("error");
65+
| |________________________^
66+
67+
error: aborting due to 10 previous errors
4068

0 commit comments

Comments
 (0)