2022-01-13 06:18:19 -06:00
|
|
|
use clippy_utils::diagnostics::{span_lint, span_lint_and_help};
|
2021-03-25 13:29:11 -05:00
|
|
|
use clippy_utils::{is_try, match_trait_method, paths};
|
2020-01-06 10:39:50 -06:00
|
|
|
use rustc_hir as hir;
|
2020-01-12 00:08:41 -06:00
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
2020-01-11 05:37:08 -06:00
|
|
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
2017-01-07 05:35:45 -06:00
|
|
|
|
2018-03-28 08:24:26 -05:00
|
|
|
declare_clippy_lint! {
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### What it does
|
|
|
|
/// Checks for unused written/read amount.
|
2019-03-05 10:50:33 -06:00
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Why is this bad?
|
|
|
|
/// `io::Write::write(_vectored)` and
|
2020-01-09 08:46:55 -06:00
|
|
|
/// `io::Read::read(_vectored)` are not guaranteed to
|
2019-03-05 10:50:33 -06: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.
|
|
|
|
///
|
2022-01-13 06:18:19 -06:00
|
|
|
/// When working with asynchronous code (either with the `futures`
|
|
|
|
/// crate or with `tokio`), a similar issue exists for
|
|
|
|
/// `AsyncWriteExt::write()` and `AsyncReadExt::read()` : these
|
|
|
|
/// functions are also not guaranteed to process the entire
|
|
|
|
/// buffer. Your code should either handle partial-writes/reads, or
|
|
|
|
/// call the `write_all`/`read_exact` methods on those traits instead.
|
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Known problems
|
|
|
|
/// Detects only common patterns.
|
2019-03-05 10:50:33 -06:00
|
|
|
///
|
2022-01-13 06:18:19 -06:00
|
|
|
/// ### Examples
|
2019-03-05 10:50:33 -06:00
|
|
|
/// ```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(())
|
|
|
|
/// }
|
|
|
|
/// ```
|
2021-12-06 05:33:31 -06:00
|
|
|
#[clippy::version = "pre 1.29.0"]
|
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"
|
|
|
|
}
|
|
|
|
|
2019-04-08 15:43:55 -05:00
|
|
|
declare_lint_pass!(UnusedIoAmount => [UNUSED_IO_AMOUNT]);
|
2017-01-07 05:35:45 -06:00
|
|
|
|
2020-06-25 15:41:36 -05:00
|
|
|
impl<'tcx> LateLintPass<'tcx> for UnusedIoAmount {
|
|
|
|
fn check_stmt(&mut self, cx: &LateContext<'_>, s: &hir::Stmt<'_>) {
|
2019-09-27 10:16:06 -05:00
|
|
|
let expr = match s.kind {
|
2021-04-08 10:50:13 -05:00
|
|
|
hir::StmtKind::Semi(expr) | hir::StmtKind::Expr(expr) => expr,
|
2017-01-07 05:35:45 -06:00
|
|
|
_ => return,
|
|
|
|
};
|
|
|
|
|
2019-09-27 10:16:06 -05:00
|
|
|
match expr.kind {
|
2021-04-22 04:31:13 -05:00
|
|
|
hir::ExprKind::Match(res, _, _) if is_try(cx, expr).is_some() => {
|
2021-09-08 09:31:47 -05:00
|
|
|
if let hir::ExprKind::Call(func, [ref arg_0, ..]) = res.kind {
|
2020-08-04 08:24:13 -05:00
|
|
|
if matches!(
|
|
|
|
func.kind,
|
2021-11-16 14:44:25 -06:00
|
|
|
hir::ExprKind::Path(hir::QPath::LangItem(hir::LangItem::TryTraitBranch, ..))
|
2020-08-04 08:24:13 -05:00
|
|
|
) {
|
2021-09-08 09:31:47 -05:00
|
|
|
check_map_error(cx, arg_0, expr);
|
2017-01-07 05:35:45 -06:00
|
|
|
}
|
|
|
|
} else {
|
2021-04-27 09:55:11 -05:00
|
|
|
check_map_error(cx, res, expr);
|
2017-01-07 05:35:45 -06:00
|
|
|
}
|
|
|
|
},
|
2021-12-01 11:17:50 -06:00
|
|
|
hir::ExprKind::MethodCall(path, [ref arg_0, ..], _) => match path.ident.as_str() {
|
2017-09-05 04:33:04 -05:00
|
|
|
"expect" | "unwrap" | "unwrap_or" | "unwrap_or_else" => {
|
2021-09-08 09:31:47 -05:00
|
|
|
check_map_error(cx, arg_0, expr);
|
2017-09-05 04:33:04 -05:00
|
|
|
},
|
|
|
|
_ => (),
|
2017-01-07 05:35:45 -06:00
|
|
|
},
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-13 06:18:19 -06:00
|
|
|
/// If `expr` is an (e).await, return the inner expression "e" that's being
|
|
|
|
/// waited on. Otherwise return None.
|
|
|
|
fn try_remove_await<'a>(expr: &'a hir::Expr<'a>) -> Option<&hir::Expr<'a>> {
|
|
|
|
if let hir::ExprKind::Match(expr, _, hir::MatchSource::AwaitDesugar) = expr.kind {
|
|
|
|
if let hir::ExprKind::Call(func, [ref arg_0, ..]) = expr.kind {
|
|
|
|
if matches!(
|
|
|
|
func.kind,
|
|
|
|
hir::ExprKind::Path(hir::QPath::LangItem(hir::LangItem::IntoFutureIntoFuture, ..))
|
|
|
|
) {
|
|
|
|
return Some(arg_0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2021-04-27 09:55:11 -05:00
|
|
|
fn check_map_error(cx: &LateContext<'_>, call: &hir::Expr<'_>, expr: &hir::Expr<'_>) {
|
|
|
|
let mut call = call;
|
2021-12-01 11:17:50 -06:00
|
|
|
while let hir::ExprKind::MethodCall(path, args, _) = call.kind {
|
2021-12-14 23:13:11 -06:00
|
|
|
if matches!(path.ident.as_str(), "or" | "or_else" | "ok") {
|
2021-04-27 09:55:11 -05:00
|
|
|
call = &args[0];
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2022-01-13 06:18:19 -06:00
|
|
|
|
|
|
|
if let Some(call) = try_remove_await(call) {
|
|
|
|
check_method_call(cx, call, expr, true);
|
|
|
|
} else {
|
|
|
|
check_method_call(cx, call, expr, false);
|
|
|
|
}
|
2021-04-27 09:55:11 -05:00
|
|
|
}
|
|
|
|
|
2022-01-13 06:18:19 -06:00
|
|
|
fn check_method_call(cx: &LateContext<'_>, call: &hir::Expr<'_>, expr: &hir::Expr<'_>, is_await: bool) {
|
2021-12-01 11:17:50 -06:00
|
|
|
if let hir::ExprKind::MethodCall(path, _, _) = call.kind {
|
2021-12-14 23:13:11 -06:00
|
|
|
let symbol = path.ident.as_str();
|
2022-01-13 06:18:19 -06:00
|
|
|
let read_trait = if is_await {
|
|
|
|
match_trait_method(cx, call, &paths::FUTURES_IO_ASYNCREADEXT)
|
|
|
|
|| match_trait_method(cx, call, &paths::TOKIO_IO_ASYNCREADEXT)
|
|
|
|
} else {
|
|
|
|
match_trait_method(cx, call, &paths::IO_READ)
|
|
|
|
};
|
|
|
|
let write_trait = if is_await {
|
|
|
|
match_trait_method(cx, call, &paths::FUTURES_IO_ASYNCWRITEEXT)
|
|
|
|
|| match_trait_method(cx, call, &paths::TOKIO_IO_ASYNCWRITEEXT)
|
|
|
|
} else {
|
|
|
|
match_trait_method(cx, call, &paths::IO_WRITE)
|
|
|
|
};
|
2020-01-09 08:46:55 -06:00
|
|
|
|
2022-01-13 06:18:19 -06:00
|
|
|
match (read_trait, write_trait, symbol, is_await) {
|
|
|
|
(true, _, "read", false) => span_lint_and_help(
|
|
|
|
cx,
|
|
|
|
UNUSED_IO_AMOUNT,
|
|
|
|
expr.span,
|
|
|
|
"read amount is not handled",
|
|
|
|
None,
|
|
|
|
"use `Read::read_exact` instead, or handle partial reads",
|
|
|
|
),
|
|
|
|
(true, _, "read", true) => span_lint_and_help(
|
2017-08-09 02:30:56 -05:00
|
|
|
cx,
|
|
|
|
UNUSED_IO_AMOUNT,
|
|
|
|
expr.span,
|
2022-01-13 06:18:19 -06:00
|
|
|
"read amount is not handled",
|
|
|
|
None,
|
|
|
|
"use `AsyncReadExt::read_exact` instead, or handle partial reads",
|
2020-01-09 08:46:55 -06:00
|
|
|
),
|
2022-01-13 06:18:19 -06:00
|
|
|
(true, _, "read_vectored", _) => {
|
|
|
|
span_lint(cx, UNUSED_IO_AMOUNT, expr.span, "read amount is not handled");
|
|
|
|
},
|
|
|
|
(_, true, "write", false) => span_lint_and_help(
|
2017-08-09 02:30:56 -05:00
|
|
|
cx,
|
|
|
|
UNUSED_IO_AMOUNT,
|
|
|
|
expr.span,
|
2022-01-13 06:18:19 -06:00
|
|
|
"written amount is not handled",
|
|
|
|
None,
|
|
|
|
"use `Write::write_all` instead, or handle partial writes",
|
2020-01-09 08:46:55 -06:00
|
|
|
),
|
2022-01-13 06:18:19 -06:00
|
|
|
(_, true, "write", true) => span_lint_and_help(
|
|
|
|
cx,
|
|
|
|
UNUSED_IO_AMOUNT,
|
|
|
|
expr.span,
|
|
|
|
"written amount is not handled",
|
|
|
|
None,
|
|
|
|
"use `AsyncWriteExt::write_all` instead, or handle partial writes",
|
|
|
|
),
|
|
|
|
(_, true, "write_vectored", _) => {
|
|
|
|
span_lint(cx, UNUSED_IO_AMOUNT, expr.span, "written amount is not handled");
|
|
|
|
},
|
2020-01-09 08:46:55 -06:00
|
|
|
_ => (),
|
2017-01-07 05:35:45 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|