2017-01-07 05:35:45 -06:00
|
|
|
use rustc::lint::*;
|
|
|
|
use rustc::hir;
|
2017-01-07 08:39:50 -06:00
|
|
|
use utils::{span_lint, match_path, match_trait_method, is_try, paths};
|
2017-01-07 05:35:45 -06:00
|
|
|
|
|
|
|
/// **What it does:** Checks for unused written/read amount.
|
|
|
|
///
|
2017-08-09 02:30:56 -05:00
|
|
|
/// **Why is this bad?** `io::Write::write` and `io::Read::read` are not
|
|
|
|
/// guaranteed to
|
|
|
|
/// process the entire buffer. They return how many bytes were processed, which
|
|
|
|
/// might be smaller
|
|
|
|
/// than a given buffer's length. If you don't need to deal with
|
|
|
|
/// partial-write/read, use
|
2017-01-07 05:35:45 -06:00
|
|
|
/// `write_all`/`read_exact` instead.
|
|
|
|
///
|
|
|
|
/// **Known problems:** Detects only common patterns.
|
|
|
|
///
|
|
|
|
/// **Example:**
|
|
|
|
/// ```rust,ignore
|
|
|
|
/// use std::io;
|
|
|
|
/// fn foo<W: io::Write>(w: &mut W) -> io::Result<()> {
|
|
|
|
/// // must be `w.write_all(b"foo")?;`
|
|
|
|
/// w.write(b"foo")?;
|
|
|
|
/// Ok(())
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
declare_lint! {
|
|
|
|
pub UNUSED_IO_AMOUNT,
|
|
|
|
Deny,
|
|
|
|
"unused written/read amount"
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct UnusedIoAmount;
|
|
|
|
|
|
|
|
impl LintPass for UnusedIoAmount {
|
|
|
|
fn get_lints(&self) -> LintArray {
|
|
|
|
lint_array!(UNUSED_IO_AMOUNT)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedIoAmount {
|
|
|
|
fn check_stmt(&mut self, cx: &LateContext, s: &hir::Stmt) {
|
|
|
|
let expr = match s.node {
|
|
|
|
hir::StmtSemi(ref expr, _) |
|
|
|
|
hir::StmtExpr(ref expr, _) => &**expr,
|
|
|
|
_ => return,
|
|
|
|
};
|
|
|
|
|
|
|
|
match expr.node {
|
2017-01-07 08:39:50 -06:00
|
|
|
hir::ExprMatch(ref res, _, _) if is_try(expr).is_some() => {
|
|
|
|
if let hir::ExprCall(ref func, ref args) = res.node {
|
2017-01-07 05:35:45 -06:00
|
|
|
if let hir::ExprPath(ref path) = func.node {
|
2017-06-04 16:28:01 -05:00
|
|
|
if match_path(path, &paths::TRY_INTO_RESULT) && args.len() == 1 {
|
2017-01-07 05:35:45 -06:00
|
|
|
check_method_call(cx, &args[0], expr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2017-01-07 08:39:50 -06:00
|
|
|
check_method_call(cx, res, expr);
|
2017-01-07 05:35:45 -06:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-07-10 03:17:40 -05:00
|
|
|
hir::ExprMethodCall(ref path, _, ref args) => {
|
|
|
|
match &*path.name.as_str() {
|
2017-01-07 05:35:45 -06:00
|
|
|
"expect" | "unwrap" | "unwrap_or" | "unwrap_or_else" => {
|
|
|
|
check_method_call(cx, &args[0], expr);
|
|
|
|
},
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_method_call(cx: &LateContext, call: &hir::Expr, expr: &hir::Expr) {
|
2017-07-10 03:17:40 -05:00
|
|
|
if let hir::ExprMethodCall(ref path, _, _) = call.node {
|
|
|
|
let symbol = &*path.name.as_str();
|
2017-01-07 05:35:45 -06:00
|
|
|
if match_trait_method(cx, call, &paths::IO_READ) && symbol == "read" {
|
2017-08-09 02:30:56 -05:00
|
|
|
span_lint(
|
|
|
|
cx,
|
|
|
|
UNUSED_IO_AMOUNT,
|
|
|
|
expr.span,
|
|
|
|
"handle read amount returned or use `Read::read_exact` instead",
|
|
|
|
);
|
2017-01-07 05:35:45 -06:00
|
|
|
} else if match_trait_method(cx, call, &paths::IO_WRITE) && symbol == "write" {
|
2017-08-09 02:30:56 -05:00
|
|
|
span_lint(
|
|
|
|
cx,
|
|
|
|
UNUSED_IO_AMOUNT,
|
|
|
|
expr.span,
|
|
|
|
"handle written amount returned or use `Write::write_all` instead",
|
|
|
|
);
|
2017-01-07 05:35:45 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|