1 use crate::utils::{has_drop, qpath_res, snippet_opt, span_lint, span_lint_and_sugg};
2 use rustc_errors::Applicability;
3 use rustc_hir::def::{DefKind, Res};
4 use rustc_hir::{BinOpKind, BlockCheckMode, Expr, ExprKind, Stmt, StmtKind, UnsafeSource};
5 use rustc_lint::{LateContext, LateLintPass};
6 use rustc_session::{declare_lint_pass, declare_tool_lint};
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 expr.span.from_expansion() {
50 ExprKind::Lit(..) | ExprKind::Closure(..) => true,
51 ExprKind::Path(..) => !has_drop(cx, cx.typeck_results().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.typeck_results().expr_ty(expr))
65 && fields.iter().all(|field| has_no_effect(cx, &field.expr))
66 && base.as_ref().map_or(true, |base| has_no_effect(cx, base))
68 ExprKind::Call(ref callee, ref args) => {
69 if let ExprKind::Path(ref qpath) = callee.kind {
70 let res = qpath_res(cx, qpath, callee.hir_id);
72 Res::Def(DefKind::Struct | DefKind::Variant | DefKind::Ctor(..), ..) => {
73 !has_drop(cx, cx.typeck_results().expr_ty(expr))
74 && args.iter().all(|arg| has_no_effect(cx, arg))
82 ExprKind::Block(ref block, _) => {
83 block.stmts.is_empty() && block.expr.as_ref().map_or(false, |expr| has_no_effect(cx, expr))
89 declare_lint_pass!(NoEffect => [NO_EFFECT, UNNECESSARY_OPERATION]);
91 impl<'tcx> LateLintPass<'tcx> for NoEffect {
92 fn check_stmt(&mut self, cx: &LateContext<'tcx>, stmt: &'tcx Stmt<'_>) {
93 if let StmtKind::Semi(ref expr) = stmt.kind {
94 if has_no_effect(cx, expr) {
95 span_lint(cx, NO_EFFECT, stmt.span, "statement with no effect");
96 } else if let Some(reduced) = reduce_expression(cx, expr) {
97 let mut snippet = String::new();
99 if e.span.from_expansion() {
102 if let Some(snip) = snippet_opt(cx, e.span) {
103 snippet.push_str(&snip);
111 UNNECESSARY_OPERATION,
113 "statement can be reduced",
116 Applicability::MachineApplicable,
123 fn reduce_expression<'a>(cx: &LateContext<'_>, expr: &'a Expr<'a>) -> Option<Vec<&'a Expr<'a>>> {
124 if expr.span.from_expansion() {
128 ExprKind::Index(ref a, ref b) => Some(vec![&**a, &**b]),
129 ExprKind::Binary(ref binop, ref a, ref b) if binop.node != BinOpKind::And && binop.node != BinOpKind::Or => {
130 Some(vec![&**a, &**b])
132 ExprKind::Array(ref v) | ExprKind::Tup(ref v) => Some(v.iter().collect()),
133 ExprKind::Repeat(ref inner, _)
134 | ExprKind::Cast(ref inner, _)
135 | ExprKind::Type(ref inner, _)
136 | ExprKind::Unary(_, ref inner)
137 | ExprKind::Field(ref inner, _)
138 | ExprKind::AddrOf(_, _, ref inner)
139 | ExprKind::Box(ref inner) => reduce_expression(cx, inner).or_else(|| Some(vec![inner])),
140 ExprKind::Struct(_, ref fields, ref base) => {
141 if has_drop(cx, cx.typeck_results().expr_ty(expr)) {
144 Some(fields.iter().map(|f| &f.expr).chain(base).map(Deref::deref).collect())
147 ExprKind::Call(ref callee, ref args) => {
148 if let ExprKind::Path(ref qpath) = callee.kind {
149 let res = qpath_res(cx, qpath, callee.hir_id);
151 Res::Def(DefKind::Struct | DefKind::Variant | DefKind::Ctor(..), ..)
152 if !has_drop(cx, cx.typeck_results().expr_ty(expr)) =>
154 Some(args.iter().collect())
162 ExprKind::Block(ref block, _) => {
163 if block.stmts.is_empty() {
164 block.expr.as_ref().and_then(|e| {
166 BlockCheckMode::UnsafeBlock(UnsafeSource::UserProvided) => None,
167 BlockCheckMode::DefaultBlock => Some(vec![&**e]),
168 // in case of compiler-inserted signaling blocks
169 _ => reduce_expression(cx, e),