Skip to content

Commit

Permalink
check expr using a loop
Browse files Browse the repository at this point in the history
  • Loading branch information
ABouttefeux committed Apr 21, 2021
1 parent 350b117 commit 56bb890
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 16 deletions.
27 changes: 15 additions & 12 deletions clippy_lints/src/unused_io_amount.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,24 +47,15 @@ impl<'tcx> LateLintPass<'tcx> for UnusedIoAmount {
func.kind,
hir::ExprKind::Path(hir::QPath::LangItem(hir::LangItem::TryIntoResult, _))
) {
match args[0].kind {
hir::ExprKind::MethodCall(ref path, _, ref args, _) => {
if matches!(&*path.ident.as_str(), "or" | "or_else" | "ok") {
check_method_call(cx, &args[0], expr);
}
},
_ => {
check_method_call(cx, &args[0], expr);
},
}
check_map_error(cx, &args[0], expr);
}
} else {
check_method_call(cx, res, expr);
check_map_error(cx, res, expr);
}
},
hir::ExprKind::MethodCall(path, _, args, _) => match &*path.ident.as_str() {
"expect" | "unwrap" | "unwrap_or" | "unwrap_or_else" => {
check_method_call(cx, &args[0], expr);
check_map_error(cx, &args[0], expr);
},
_ => (),
},
Expand All @@ -73,6 +64,18 @@ impl<'tcx> LateLintPass<'tcx> for UnusedIoAmount {
}
}

fn check_map_error(cx: &LateContext<'_>, call: &hir::Expr<'_>, expr: &hir::Expr<'_>) {
let mut call = call;
while let hir::ExprKind::MethodCall(ref path, _, ref args, _) = call.kind {
if matches!(&*path.ident.as_str(), "or" | "or_else" | "ok") {
call = &args[0];
} else {
break;
}
}
check_method_call(cx, call, expr);
}

fn check_method_call(cx: &LateContext<'_>, call: &hir::Expr<'_>, expr: &hir::Expr<'_>) {
if let hir::ExprKind::MethodCall(path, _, _, _) = call.kind {
let symbol = &*path.ident.as_str();
Expand Down
12 changes: 12 additions & 0 deletions tests/ui/unused_io_amount.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ fn or_else(file: &str) -> io::Result<()> {
Ok(())
}

#[derive(Debug)]
enum Error {
Kind,
}
Expand All @@ -49,4 +50,15 @@ fn or(file: &str) -> Result<(), Error> {
Ok(())
}

fn combine_or(file: &str) -> Result<(), Error> {
let mut reader = std::fs::File::open(file).unwrap();
let mut result = [0u8; 0];
reader
.read(&mut result)
.or(Err(Error::Kind))
.or(Err(Error::Kind))
.expect("error");
Ok(())
}

fn main() {}
42 changes: 38 additions & 4 deletions tests/ui/unused_io_amount.stderr
Original file line number Diff line number Diff line change
@@ -1,17 +1,41 @@
error: written amount is not handled. Use `Write::write_all` instead
--> $DIR/unused_io_amount.rs:7:5
|
LL | s.write(b"test")?;
| ^^^^^^^^^^^^^^^^^
|
= note: `-D clippy::unused-io-amount` implied by `-D warnings`

error: read amount is not handled. Use `Read::read_exact` instead
--> $DIR/unused_io_amount.rs:9:5
|
LL | s.read(&mut buf)?;
| ^^^^^^^^^^^^^^^^^

error: written amount is not handled. Use `Write::write_all` instead
--> $DIR/unused_io_amount.rs:14:5
|
LL | s.write(b"test").unwrap();
| ^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: `-D clippy::unused-io-amount` implied by `-D warnings`

error: read amount is not handled. Use `Read::read_exact` instead
--> $DIR/unused_io_amount.rs:16:5
|
LL | s.read(&mut buf).unwrap();
| ^^^^^^^^^^^^^^^^^^^^^^^^^

error: read amount is not handled
--> $DIR/unused_io_amount.rs:20:5
|
LL | s.read_vectored(&mut [io::IoSliceMut::new(&mut [])])?;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: written amount is not handled
--> $DIR/unused_io_amount.rs:21:5
|
LL | s.write_vectored(&[io::IoSlice::new(&[])])?;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: read amount is not handled. Use `Read::read_exact` instead
--> $DIR/unused_io_amount.rs:28:5
|
Expand All @@ -25,10 +49,20 @@ LL | reader.read(&mut result).or_else(|err| Err(err))?;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: read amount is not handled. Use `Read::read_exact` instead
--> $DIR/unused_io_amount.rs:48:5
--> $DIR/unused_io_amount.rs:49:5
|
LL | reader.read(&mut result).or(Err(Error::Kind))?;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 5 previous errors
error: read amount is not handled. Use `Read::read_exact` instead
--> $DIR/unused_io_amount.rs:56:5
|
LL | / reader
LL | | .read(&mut result)
LL | | .or(Err(Error::Kind))
LL | | .or(Err(Error::Kind))
LL | | .expect("error");
| |________________________^

error: aborting due to 10 previous errors

0 comments on commit 56bb890

Please sign in to comment.