-use clippy_utils::diagnostics::{span_lint, span_lint_and_sugg};
+use clippy_utils::diagnostics::{span_lint_hir, span_lint_hir_and_then};
use clippy_utils::source::snippet_opt;
use clippy_utils::ty::has_drop;
use rustc_errors::Applicability;
use std::ops::Deref;
declare_clippy_lint! {
- /// **What it does:** Checks for statements which have no effect.
+ /// ### What it does
+ /// Checks for statements which have no effect.
///
- /// **Why is this bad?** Similar to dead code, these statements are actually
+ /// ### Why is this bad?
+ /// Similar to dead code, these statements are actually
/// executed. However, as they have no effect, all they do is make the code less
/// readable.
///
- /// **Known problems:** None.
- ///
- /// **Example:**
+ /// ### Example
/// ```rust
/// 0;
/// ```
}
declare_clippy_lint! {
- /// **What it does:** Checks for expression statements that can be reduced to a
+ /// ### What it does
+ /// Checks for expression statements that can be reduced to a
/// sub-expression.
///
- /// **Why is this bad?** Expressions by themselves often have no side-effects.
+ /// ### Why is this bad?
+ /// Expressions by themselves often have no side-effects.
/// Having such expressions reduces readability.
///
- /// **Known problems:** None.
- ///
- /// **Example:**
+ /// ### Example
/// ```rust,ignore
/// compute_array()[0];
/// ```
fn check_stmt(&mut self, cx: &LateContext<'tcx>, stmt: &'tcx Stmt<'_>) {
if let StmtKind::Semi(expr) = stmt.kind {
if has_no_effect(cx, expr) {
- span_lint(cx, NO_EFFECT, stmt.span, "statement with no effect");
+ span_lint_hir(cx, NO_EFFECT, expr.hir_id, stmt.span, "statement with no effect");
} else if let Some(reduced) = reduce_expression(cx, expr) {
let mut snippet = String::new();
for e in reduced {
return;
}
}
- span_lint_and_sugg(
+ span_lint_hir_and_then(
cx,
UNNECESSARY_OPERATION,
+ expr.hir_id,
stmt.span,
"statement can be reduced",
- "replace it with",
- snippet,
- Applicability::MachineApplicable,
+ |diag| {
+ diag.span_suggestion(stmt.span, "replace it with", snippet, Applicability::MachineApplicable);
+ },
);
}
}
BlockCheckMode::UnsafeBlock(UnsafeSource::UserProvided) => None,
BlockCheckMode::DefaultBlock => Some(vec![&**e]),
// in case of compiler-inserted signaling blocks
- _ => reduce_expression(cx, e),
+ BlockCheckMode::UnsafeBlock(_) => reduce_expression(cx, e),
}
})
} else {