3 use utils::{span_lint, match_path, match_trait_method, is_try, paths};
5 /// **What it does:** Checks for unused written/read amount.
7 /// **Why is this bad?** `io::Write::write` and `io::Read::read` are not guaranteed to
8 /// process the entire buffer. They return how many bytes were processed, which might be smaller
9 /// than a given buffer's length. If you don't need to deal with partial-write/read, use
10 /// `write_all`/`read_exact` instead.
12 /// **Known problems:** Detects only common patterns.
17 /// fn foo<W: io::Write>(w: &mut W) -> io::Result<()> {
18 /// // must be `w.write_all(b"foo")?;`
26 "unused written/read amount"
29 pub struct UnusedIoAmount;
31 impl LintPass for UnusedIoAmount {
32 fn get_lints(&self) -> LintArray {
33 lint_array!(UNUSED_IO_AMOUNT)
37 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedIoAmount {
38 fn check_stmt(&mut self, cx: &LateContext, s: &hir::Stmt) {
39 let expr = match s.node {
40 hir::StmtSemi(ref expr, _) |
41 hir::StmtExpr(ref expr, _) => &**expr,
46 hir::ExprMatch(ref res, _, _) if is_try(expr).is_some() => {
47 if let hir::ExprCall(ref func, ref args) = res.node {
48 if let hir::ExprPath(ref path) = func.node {
49 if match_path(path, &paths::CARRIER_TRANSLATE) && args.len() == 1 {
50 check_method_call(cx, &args[0], expr);
54 check_method_call(cx, res, expr);
58 hir::ExprMethodCall(ref symbol, _, ref args) => {
59 let symbol = &*symbol.node.as_str();
61 "expect" | "unwrap" | "unwrap_or" | "unwrap_or_else" => {
62 check_method_call(cx, &args[0], expr);
73 fn check_method_call(cx: &LateContext, call: &hir::Expr, expr: &hir::Expr) {
74 if let hir::ExprMethodCall(ref symbol, _, _) = call.node {
75 let symbol = &*symbol.node.as_str();
76 if match_trait_method(cx, call, &paths::IO_READ) && symbol == "read" {
80 "handle read amount returned or use `Read::read_exact` instead");
81 } else if match_trait_method(cx, call, &paths::IO_WRITE) && symbol == "write" {
85 "handle written amount returned or use `Write::write_all` instead");