2018-05-30 03:15:50 -05:00
|
|
|
use crate::utils::{is_try, match_qpath, match_trait_method, paths, span_lint};
|
2018-12-29 09:04:45 -06:00
|
|
|
use rustc::hir;
|
|
|
|
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
|
|
|
|
use rustc::{declare_tool_lint, lint_array};
|
2017-01-07 05:35:45 -06:00
|
|
|
|
2018-03-28 08:24:26 -05:00
|
|
|
declare_clippy_lint! {
|
2019-03-05 10:50:33 -06:00
|
|
|
/// **What it does:** Checks for unused written/read amount.
|
|
|
|
///
|
|
|
|
/// **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
|
|
|
|
/// `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(())
|
|
|
|
/// }
|
|
|
|
/// ```
|
2017-01-07 05:35:45 -06:00
|
|
|
pub UNUSED_IO_AMOUNT,
|
2018-03-28 08:24:26 -05:00
|
|
|
correctness,
|
2017-01-07 05:35:45 -06:00
|
|
|
"unused written/read amount"
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct UnusedIoAmount;
|
|
|
|
|
|
|
|
impl LintPass for UnusedIoAmount {
|
|
|
|
fn get_lints(&self) -> LintArray {
|
|
|
|
lint_array!(UNUSED_IO_AMOUNT)
|
|
|
|
}
|
2019-01-26 13:40:55 -06:00
|
|
|
|
|
|
|
fn name(&self) -> &'static str {
|
|
|
|
"UnusedIoAmount"
|
|
|
|
}
|
2017-01-07 05:35:45 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedIoAmount {
|
2018-07-23 06:01:12 -05:00
|
|
|
fn check_stmt(&mut self, cx: &LateContext<'_, '_>, s: &hir::Stmt) {
|
2017-01-07 05:35:45 -06:00
|
|
|
let expr = match s.node {
|
2019-01-20 04:21:30 -06:00
|
|
|
hir::StmtKind::Semi(ref expr) | hir::StmtKind::Expr(ref expr) => &**expr,
|
2017-01-07 05:35:45 -06:00
|
|
|
_ => return,
|
|
|
|
};
|
|
|
|
|
|
|
|
match expr.node {
|
2019-03-07 14:51:05 -06:00
|
|
|
hir::ExprKind::Match(ref res, _, _) if is_try(cx, expr).is_some() => {
|
2018-07-12 02:30:57 -05:00
|
|
|
if let hir::ExprKind::Call(ref func, ref args) = res.node {
|
|
|
|
if let hir::ExprKind::Path(ref path) = func.node {
|
2017-08-24 17:21:46 -05:00
|
|
|
if match_qpath(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
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-07-12 02:30:57 -05:00
|
|
|
hir::ExprKind::MethodCall(ref path, _, ref args) => match &*path.ident.as_str() {
|
2017-09-05 04:33:04 -05:00
|
|
|
"expect" | "unwrap" | "unwrap_or" | "unwrap_or_else" => {
|
|
|
|
check_method_call(cx, &args[0], expr);
|
|
|
|
},
|
|
|
|
_ => (),
|
2017-01-07 05:35:45 -06:00
|
|
|
},
|
|
|
|
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-23 06:01:12 -05:00
|
|
|
fn check_method_call(cx: &LateContext<'_, '_>, call: &hir::Expr, expr: &hir::Expr) {
|
2018-07-12 02:30:57 -05:00
|
|
|
if let hir::ExprKind::MethodCall(ref path, _, _) = call.node {
|
2018-06-28 08:46:58 -05:00
|
|
|
let symbol = &*path.ident.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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|