2019-11-16 14:55:00 +00:00
|
|
|
use if_chain::if_chain;
|
2020-01-11 11:37:08 +00:00
|
|
|
use rustc::lint::{LateContext, LateLintPass};
|
2020-01-06 16:39:50 +00:00
|
|
|
use rustc_hir::*;
|
2020-01-11 11:37:08 +00:00
|
|
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
2019-11-16 14:55:00 +00:00
|
|
|
|
|
|
|
use crate::utils::{is_must_use_func_call, is_must_use_ty, span_help_and_lint};
|
|
|
|
|
|
|
|
declare_clippy_lint! {
|
|
|
|
/// **What it does:** Checks for `let _ = <expr>`
|
|
|
|
/// where expr is #[must_use]
|
|
|
|
///
|
|
|
|
/// **Why is this bad?** It's better to explicitly
|
|
|
|
/// handle the value of a #[must_use] expr
|
|
|
|
///
|
|
|
|
/// **Known problems:** None.
|
|
|
|
///
|
|
|
|
/// **Example:**
|
|
|
|
/// ```rust
|
|
|
|
/// fn f() -> Result<u32, u32> {
|
|
|
|
/// Ok(0)
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// let _ = f();
|
|
|
|
/// // is_ok() is marked #[must_use]
|
|
|
|
/// let _ = f().is_ok();
|
|
|
|
/// ```
|
|
|
|
pub LET_UNDERSCORE_MUST_USE,
|
|
|
|
restriction,
|
2020-01-06 06:30:43 +00:00
|
|
|
"non-binding let on a `#[must_use]` expression"
|
2019-11-16 14:55:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
declare_lint_pass!(LetUnderscore => [LET_UNDERSCORE_MUST_USE]);
|
|
|
|
|
|
|
|
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for LetUnderscore {
|
2019-12-27 07:12:26 +00:00
|
|
|
fn check_stmt(&mut self, cx: &LateContext<'_, '_>, stmt: &Stmt<'_>) {
|
2019-11-16 14:55:00 +00:00
|
|
|
if_chain! {
|
|
|
|
if let StmtKind::Local(ref local) = stmt.kind;
|
|
|
|
if let PatKind::Wild = local.pat.kind;
|
|
|
|
if let Some(ref init) = local.init;
|
|
|
|
then {
|
|
|
|
if is_must_use_ty(cx, cx.tables.expr_ty(init)) {
|
|
|
|
span_help_and_lint(
|
|
|
|
cx,
|
|
|
|
LET_UNDERSCORE_MUST_USE,
|
|
|
|
stmt.span,
|
2020-01-06 06:30:43 +00:00
|
|
|
"non-binding let on an expression with `#[must_use]` type",
|
2019-11-16 14:55:00 +00:00
|
|
|
"consider explicitly using expression value"
|
|
|
|
)
|
|
|
|
} else if is_must_use_func_call(cx, init) {
|
|
|
|
span_help_and_lint(
|
|
|
|
cx,
|
|
|
|
LET_UNDERSCORE_MUST_USE,
|
|
|
|
stmt.span,
|
2020-01-06 06:30:43 +00:00
|
|
|
"non-binding let on a result of a `#[must_use]` function",
|
2019-11-16 14:55:00 +00:00
|
|
|
"consider explicitly using function result"
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|