1 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
2 use rustc::hir::def::Def;
3 use rustc::hir::{BiAnd, BiOr, BlockCheckMode, Expr, Expr_, Stmt, StmtSemi, UnsafeSource};
4 use utils::{in_macro, snippet_opt, span_lint, span_lint_and_sugg};
7 /// **What it does:** Checks for statements which have no effect.
9 /// **Why is this bad?** Similar to dead code, these statements are actually
10 /// executed. However, as they have no effect, all they do is make the code less
13 /// **Known problems:** None.
22 "statements with no effect"
25 /// **What it does:** Checks for expression statements that can be reduced to a
28 /// **Why is this bad?** Expressions by themselves often have no side-effects.
29 /// Having such expressions reduces readability.
31 /// **Known problems:** None.
35 /// compute_array()[0];
38 pub UNNECESSARY_OPERATION,
40 "outer expressions with no effect"
43 fn has_no_effect(cx: &LateContext, expr: &Expr) -> bool {
44 if in_macro(expr.span) {
48 Expr_::ExprLit(..) | Expr_::ExprClosure(.., _) | Expr_::ExprPath(..) => true,
49 Expr_::ExprIndex(ref a, ref b) | Expr_::ExprBinary(_, ref a, ref b) => {
50 has_no_effect(cx, a) && has_no_effect(cx, b)
52 Expr_::ExprArray(ref v) | Expr_::ExprTup(ref v) => v.iter().all(|val| has_no_effect(cx, val)),
53 Expr_::ExprRepeat(ref inner, _) |
54 Expr_::ExprCast(ref inner, _) |
55 Expr_::ExprType(ref inner, _) |
56 Expr_::ExprUnary(_, ref inner) |
57 Expr_::ExprField(ref inner, _) |
58 Expr_::ExprTupField(ref inner, _) |
59 Expr_::ExprAddrOf(_, ref inner) |
60 Expr_::ExprBox(ref inner) => has_no_effect(cx, inner),
61 Expr_::ExprStruct(_, ref fields, ref base) => {
62 fields.iter().all(|field| has_no_effect(cx, &field.expr)) && match *base {
63 Some(ref base) => has_no_effect(cx, base),
67 Expr_::ExprCall(ref callee, ref args) => if let Expr_::ExprPath(ref qpath) = callee.node {
68 let def = cx.tables.qpath_def(qpath, callee.hir_id);
70 Def::Struct(..) | Def::Variant(..) | Def::StructCtor(..) | Def::VariantCtor(..) => {
71 args.iter().all(|arg| has_no_effect(cx, arg))
78 Expr_::ExprBlock(ref block) => {
79 block.stmts.is_empty() && if let Some(ref expr) = block.expr {
80 has_no_effect(cx, expr)
89 #[derive(Copy, Clone)]
92 impl LintPass for Pass {
93 fn get_lints(&self) -> LintArray {
94 lint_array!(NO_EFFECT, UNNECESSARY_OPERATION)
98 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
99 fn check_stmt(&mut self, cx: &LateContext<'a, 'tcx>, stmt: &'tcx Stmt) {
100 if let StmtSemi(ref expr, _) = stmt.node {
101 if has_no_effect(cx, expr) {
102 span_lint(cx, NO_EFFECT, stmt.span, "statement with no effect");
103 } else if let Some(reduced) = reduce_expression(cx, expr) {
104 let mut snippet = String::new();
106 if in_macro(e.span) {
109 if let Some(snip) = snippet_opt(cx, e.span) {
110 snippet.push_str(&snip);
118 UNNECESSARY_OPERATION,
120 "statement can be reduced",
130 fn reduce_expression<'a>(cx: &LateContext, expr: &'a Expr) -> Option<Vec<&'a Expr>> {
131 if in_macro(expr.span) {
135 Expr_::ExprIndex(ref a, ref b) => Some(vec![&**a, &**b]),
136 Expr_::ExprBinary(ref binop, ref a, ref b) if binop.node != BiAnd && binop.node != BiOr => {
137 Some(vec![&**a, &**b])
139 Expr_::ExprArray(ref v) | Expr_::ExprTup(ref v) => Some(v.iter().collect()),
140 Expr_::ExprRepeat(ref inner, _) |
141 Expr_::ExprCast(ref inner, _) |
142 Expr_::ExprType(ref inner, _) |
143 Expr_::ExprUnary(_, ref inner) |
144 Expr_::ExprField(ref inner, _) |
145 Expr_::ExprTupField(ref inner, _) |
146 Expr_::ExprAddrOf(_, ref inner) |
147 Expr_::ExprBox(ref inner) => reduce_expression(cx, inner).or_else(|| Some(vec![inner])),
148 Expr_::ExprStruct(_, ref fields, ref base) => Some(
156 Expr_::ExprCall(ref callee, ref args) => if let Expr_::ExprPath(ref qpath) = callee.node {
157 let def = cx.tables.qpath_def(qpath, callee.hir_id);
159 Def::Struct(..) | Def::Variant(..) | Def::StructCtor(..) | Def::VariantCtor(..) => {
160 Some(args.iter().collect())
167 Expr_::ExprBlock(ref block) => {
168 if block.stmts.is_empty() {
169 block.expr.as_ref().and_then(|e| {
171 BlockCheckMode::UnsafeBlock(UnsafeSource::UserProvided) => None,
172 BlockCheckMode::DefaultBlock => Some(vec![&**e]),
173 // in case of compiler-inserted signaling blocks
174 _ => reduce_expression(cx, e),