1 use crate::utils::{has_drop, in_macro, snippet_opt, span_lint, span_lint_and_sugg};
2 use rustc::hir::def::Def;
3 use rustc::hir::{BinOpKind, BlockCheckMode, Expr, ExprKind, Stmt, StmtKind, UnsafeSource};
4 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
5 use rustc::{declare_tool_lint, lint_array};
6 use rustc_errors::Applicability;
10 /// **What it does:** Checks for statements which have no effect.
12 /// **Why is this bad?** Similar to dead code, these statements are actually
13 /// executed. However, as they have no effect, all they do is make the code less
16 /// **Known problems:** None.
24 "statements with no effect"
27 declare_clippy_lint! {
28 /// **What it does:** Checks for expression statements that can be reduced to a
31 /// **Why is this bad?** Expressions by themselves often have no side-effects.
32 /// Having such expressions reduces readability.
34 /// **Known problems:** None.
38 /// compute_array()[0];
40 pub UNNECESSARY_OPERATION,
42 "outer expressions with no effect"
45 fn has_no_effect(cx: &LateContext<'_, '_>, expr: &Expr) -> bool {
46 if in_macro(expr.span) {
50 ExprKind::Lit(..) | ExprKind::Closure(.., _) => true,
51 ExprKind::Path(..) => !has_drop(cx, cx.tables.expr_ty(expr)),
52 ExprKind::Index(ref a, ref b) | ExprKind::Binary(_, ref a, ref b) => {
53 has_no_effect(cx, a) && has_no_effect(cx, b)
55 ExprKind::Array(ref v) | ExprKind::Tup(ref v) => v.iter().all(|val| has_no_effect(cx, val)),
56 ExprKind::Repeat(ref inner, _)
57 | ExprKind::Cast(ref inner, _)
58 | ExprKind::Type(ref inner, _)
59 | ExprKind::Unary(_, ref inner)
60 | ExprKind::Field(ref inner, _)
61 | ExprKind::AddrOf(_, ref inner)
62 | ExprKind::Box(ref inner) => has_no_effect(cx, inner),
63 ExprKind::Struct(_, ref fields, ref base) => {
64 !has_drop(cx, cx.tables.expr_ty(expr))
65 && fields.iter().all(|field| has_no_effect(cx, &field.expr))
67 Some(ref base) => has_no_effect(cx, base),
71 ExprKind::Call(ref callee, ref args) => {
72 if let ExprKind::Path(ref qpath) = callee.node {
73 let def = cx.tables.qpath_def(qpath, callee.hir_id);
75 Def::Struct(..) | Def::Variant(..) | Def::StructCtor(..) | Def::VariantCtor(..) => {
76 !has_drop(cx, cx.tables.expr_ty(expr)) && args.iter().all(|arg| has_no_effect(cx, arg))
84 ExprKind::Block(ref block, _) => {
85 block.stmts.is_empty()
86 && if let Some(ref expr) = block.expr {
87 has_no_effect(cx, expr)
96 #[derive(Copy, Clone)]
99 impl LintPass for Pass {
100 fn get_lints(&self) -> LintArray {
101 lint_array!(NO_EFFECT, UNNECESSARY_OPERATION)
104 fn name(&self) -> &'static str {
109 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
110 fn check_stmt(&mut self, cx: &LateContext<'a, 'tcx>, stmt: &'tcx Stmt) {
111 if let StmtKind::Semi(ref expr) = stmt.node {
112 if has_no_effect(cx, expr) {
113 span_lint(cx, NO_EFFECT, stmt.span, "statement with no effect");
114 } else if let Some(reduced) = reduce_expression(cx, expr) {
115 let mut snippet = String::new();
117 if in_macro(e.span) {
120 if let Some(snip) = snippet_opt(cx, e.span) {
121 snippet.push_str(&snip);
129 UNNECESSARY_OPERATION,
131 "statement can be reduced",
134 Applicability::MachineApplicable,
141 fn reduce_expression<'a>(cx: &LateContext<'_, '_>, expr: &'a Expr) -> Option<Vec<&'a Expr>> {
142 if in_macro(expr.span) {
146 ExprKind::Index(ref a, ref b) => Some(vec![&**a, &**b]),
147 ExprKind::Binary(ref binop, ref a, ref b) if binop.node != BinOpKind::And && binop.node != BinOpKind::Or => {
148 Some(vec![&**a, &**b])
150 ExprKind::Array(ref v) | ExprKind::Tup(ref v) => Some(v.iter().collect()),
151 ExprKind::Repeat(ref inner, _)
152 | ExprKind::Cast(ref inner, _)
153 | ExprKind::Type(ref inner, _)
154 | ExprKind::Unary(_, ref inner)
155 | ExprKind::Field(ref inner, _)
156 | ExprKind::AddrOf(_, ref inner)
157 | ExprKind::Box(ref inner) => reduce_expression(cx, inner).or_else(|| Some(vec![inner])),
158 ExprKind::Struct(_, ref fields, ref base) => {
159 if has_drop(cx, cx.tables.expr_ty(expr)) {
162 Some(fields.iter().map(|f| &f.expr).chain(base).map(Deref::deref).collect())
165 ExprKind::Call(ref callee, ref args) => {
166 if let ExprKind::Path(ref qpath) = callee.node {
167 let def = cx.tables.qpath_def(qpath, callee.hir_id);
169 Def::Struct(..) | Def::Variant(..) | Def::StructCtor(..) | Def::VariantCtor(..)
170 if !has_drop(cx, cx.tables.expr_ty(expr)) =>
172 Some(args.iter().collect())
180 ExprKind::Block(ref block, _) => {
181 if block.stmts.is_empty() {
182 block.expr.as_ref().and_then(|e| {
184 BlockCheckMode::UnsafeBlock(UnsafeSource::UserProvided) => None,
185 BlockCheckMode::DefaultBlock => Some(vec![&**e]),
186 // in case of compiler-inserted signaling blocks
187 _ => reduce_expression(cx, e),