use rustc::lint::*;
use rustc::hir;
use utils::{span_lint, match_path, match_trait_method, is_try, paths};
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 {
hir::ExprMatch(ref res, _, _) if is_try(expr).is_some() => {
if let hir::ExprCall(ref func, ref args) = res.node {
if let hir::ExprPath(ref path) = func.node {
if match_path(path, &paths::CARRIER_TRANSLATE) && args.len() == 1 {
check_method_call(cx, &args[0], expr);
}
}
} else {
check_method_call(cx, res, expr);
}
},
hir::ExprMethodCall(ref symbol, _, ref args) => {
match &*symbol.node.as_str() {
"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) {
if let hir::ExprMethodCall(ref symbol, _, _) = call.node {
let symbol = &*symbol.node.as_str();
if match_trait_method(cx, call, &paths::IO_READ) && symbol == "read" {
span_lint(cx,
UNUSED_IO_AMOUNT,
expr.span,
"handle read amount returned or use `Read::read_exact` instead");
} else if match_trait_method(cx, call, &paths::IO_WRITE) && symbol == "write" {
span_lint(cx,
UNUSED_IO_AMOUNT,
expr.span,
"handle written amount returned or use `Write::write_all` instead");
}
}
}