2 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
3 use rustc::{declare_tool_lint, lint_array};
5 use rustc::hir::intravisit::{walk_expr, NestedVisitorMap, Visitor};
8 /// **What it does:** Checks for `if` conditions that use blocks to contain an
11 /// **Why is this bad?** It isn't really Rust style, same as using parentheses
12 /// to contain expressions.
14 /// **Known problems:** None.
20 declare_clippy_lint! {
21 pub BLOCK_IN_IF_CONDITION_EXPR,
23 "braces that can be eliminated in conditions, e.g. `if { true } ...`"
26 /// **What it does:** Checks for `if` conditions that use blocks containing
27 /// statements, or conditions that use closures with blocks.
29 /// **Why is this bad?** Using blocks in the condition makes it hard to read.
31 /// **Known problems:** None.
35 /// if { let x = somefunc(); x } ..
37 /// if somefunc(|x| { x == 47 }) ..
39 declare_clippy_lint! {
40 pub BLOCK_IN_IF_CONDITION_STMT,
42 "complex blocks in conditions, e.g. `if { let x = true; x } ...`"
45 #[derive(Copy, Clone)]
46 pub struct BlockInIfCondition;
48 impl LintPass for BlockInIfCondition {
49 fn get_lints(&self) -> LintArray {
50 lint_array!(BLOCK_IN_IF_CONDITION_EXPR, BLOCK_IN_IF_CONDITION_STMT)
54 struct ExVisitor<'a, 'tcx: 'a> {
55 found_block: Option<&'tcx Expr>,
56 cx: &'a LateContext<'a, 'tcx>,
59 impl<'a, 'tcx: 'a> Visitor<'tcx> for ExVisitor<'a, 'tcx> {
60 fn visit_expr(&mut self, expr: &'tcx Expr) {
61 if let ExprKind::Closure(_, _, eid, _, _) = expr.node {
62 let body = self.cx.tcx.hir.body(eid);
64 if matches!(ex.node, ExprKind::Block(_, _)) {
65 self.found_block = Some(ex);
69 walk_expr(self, expr);
71 fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
72 NestedVisitorMap::None
76 const BRACED_EXPR_MESSAGE: &str = "omit braces around single expression condition";
77 const COMPLEX_BLOCK_MESSAGE: &str = "in an 'if' condition, avoid complex blocks or closures with blocks; \
78 instead, move the block or closure higher and bind it with a 'let'";
80 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for BlockInIfCondition {
81 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
82 if let ExprKind::If(ref check, ref then, _) = expr.node {
83 if let ExprKind::Block(ref block, _) = check.node {
84 if block.rules == DefaultBlock {
85 if block.stmts.is_empty() {
86 if let Some(ref ex) = block.expr {
87 // don't dig into the expression here, just suggest that they remove
89 if in_macro(expr.span) || differing_macro_contexts(expr.span, ex.span) {
94 BLOCK_IN_IF_CONDITION_EXPR,
99 snippet_block(cx, ex.span, ".."),
100 snippet_block(cx, then.span, "..")
108 .map_or_else(|| block.stmts[0].span, |e| e.span);
109 if in_macro(span) || differing_macro_contexts(expr.span, span) {
115 BLOCK_IN_IF_CONDITION_STMT,
117 COMPLEX_BLOCK_MESSAGE,
119 "try\nlet res = {};\nif res {} ... ",
120 snippet_block(cx, block.span, ".."),
121 snippet_block(cx, then.span, "..")
127 let mut visitor = ExVisitor {
131 walk_expr(&mut visitor, check);
132 if let Some(block) = visitor.found_block {
133 span_lint(cx, BLOCK_IN_IF_CONDITION_STMT, block.span, COMPLEX_BLOCK_MESSAGE);