2021-03-25 18:29:11 +00:00
|
|
|
use clippy_utils::diagnostics::span_lint;
|
|
|
|
use clippy_utils::{is_try, match_trait_method, paths};
|
2020-01-06 16:39:50 +00:00
|
|
|
use rustc_hir as hir;
|
2020-01-12 06:08:41 +00:00
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
2020-01-11 11:37:08 +00:00
|
|
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
2017-01-07 11:35:45 +00:00
|
|
|
|
2018-03-28 13:24:26 +00:00
|
|
|
declare_clippy_lint! {
|
2019-03-05 16:50:33 +00:00
|
|
|
/// **What it does:** Checks for unused written/read amount.
|
|
|
|
///
|
2020-01-09 14:46:55 +00:00
|
|
|
/// **Why is this bad?** `io::Write::write(_vectored)` and
|
|
|
|
/// `io::Read::read(_vectored)` are not guaranteed to
|
2019-03-05 16:50:33 +00:00
|
|
|
/// 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 11:35:45 +00:00
|
|
|
pub UNUSED_IO_AMOUNT,
|
2018-03-28 13:24:26 +00:00
|
|
|
correctness,
|
2017-01-07 11:35:45 +00:00
|
|
|
"unused written/read amount"
|
|
|
|
}
|
|
|
|
|
2019-04-08 20:43:55 +00:00
|
|
|
declare_lint_pass!(UnusedIoAmount => [UNUSED_IO_AMOUNT]);
|
2017-01-07 11:35:45 +00:00
|
|
|
|
2020-06-25 20:41:36 +00:00
|
|
|
impl<'tcx> LateLintPass<'tcx> for UnusedIoAmount {
|
|
|
|
fn check_stmt(&mut self, cx: &LateContext<'_>, s: &hir::Stmt<'_>) {
|
2019-09-27 15:16:06 +00:00
|
|
|
let expr = match s.kind {
|
2021-04-08 15:50:13 +00:00
|
|
|
hir::StmtKind::Semi(expr) | hir::StmtKind::Expr(expr) => expr,
|
2017-01-07 11:35:45 +00:00
|
|
|
_ => return,
|
|
|
|
};
|
|
|
|
|
2019-09-27 15:16:06 +00:00
|
|
|
match expr.kind {
|
2021-04-22 09:31:13 +00:00
|
|
|
hir::ExprKind::Match(res, _, _) if is_try(cx, expr).is_some() => {
|
2021-04-08 15:50:13 +00:00
|
|
|
if let hir::ExprKind::Call(func, args) = res.kind {
|
2020-08-04 13:24:13 +00:00
|
|
|
if matches!(
|
|
|
|
func.kind,
|
|
|
|
hir::ExprKind::Path(hir::QPath::LangItem(hir::LangItem::TryIntoResult, _))
|
|
|
|
) {
|
2021-04-27 14:55:11 +00:00
|
|
|
check_map_error(cx, &args[0], expr);
|
2017-01-07 11:35:45 +00:00
|
|
|
}
|
|
|
|
} else {
|
2021-04-27 14:55:11 +00:00
|
|
|
check_map_error(cx, res, expr);
|
2017-01-07 11:35:45 +00:00
|
|
|
}
|
|
|
|
},
|
2021-04-08 15:50:13 +00:00
|
|
|
hir::ExprKind::MethodCall(path, _, args, _) => match &*path.ident.as_str() {
|
2017-09-05 09:33:04 +00:00
|
|
|
"expect" | "unwrap" | "unwrap_or" | "unwrap_or_else" => {
|
2021-04-27 14:55:11 +00:00
|
|
|
check_map_error(cx, &args[0], expr);
|
2017-09-05 09:33:04 +00:00
|
|
|
},
|
|
|
|
_ => (),
|
2017-01-07 11:35:45 +00:00
|
|
|
},
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-27 14:55:11 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2020-06-25 20:41:36 +00:00
|
|
|
fn check_method_call(cx: &LateContext<'_>, call: &hir::Expr<'_>, expr: &hir::Expr<'_>) {
|
2021-04-08 15:50:13 +00:00
|
|
|
if let hir::ExprKind::MethodCall(path, _, _, _) = call.kind {
|
2018-06-28 13:46:58 +00:00
|
|
|
let symbol = &*path.ident.as_str();
|
2020-01-09 14:46:55 +00:00
|
|
|
let read_trait = match_trait_method(cx, call, &paths::IO_READ);
|
|
|
|
let write_trait = match_trait_method(cx, call, &paths::IO_WRITE);
|
|
|
|
|
|
|
|
match (read_trait, write_trait, symbol) {
|
|
|
|
(true, _, "read") => span_lint(
|
2017-08-09 07:30:56 +00:00
|
|
|
cx,
|
|
|
|
UNUSED_IO_AMOUNT,
|
|
|
|
expr.span,
|
2020-01-09 14:46:55 +00:00
|
|
|
"read amount is not handled. Use `Read::read_exact` instead",
|
|
|
|
),
|
|
|
|
(true, _, "read_vectored") => span_lint(cx, UNUSED_IO_AMOUNT, expr.span, "read amount is not handled"),
|
|
|
|
(_, true, "write") => span_lint(
|
2017-08-09 07:30:56 +00:00
|
|
|
cx,
|
|
|
|
UNUSED_IO_AMOUNT,
|
|
|
|
expr.span,
|
2020-01-09 14:46:55 +00:00
|
|
|
"written amount is not handled. Use `Write::write_all` instead",
|
|
|
|
),
|
|
|
|
(_, true, "write_vectored") => span_lint(cx, UNUSED_IO_AMOUNT, expr.span, "written amount is not handled"),
|
|
|
|
_ => (),
|
2017-01-07 11:35:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|