3 use rustc::hir::intravisit::{walk_expr, NestedVisitorMap, Visitor};
5 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
6 use rustc::{declare_lint_pass, declare_tool_lint};
9 /// **What it does:** Checks for `if` conditions that use blocks to contain an
12 /// **Why is this bad?** It isn't really Rust style, same as using parentheses
13 /// to contain expressions.
15 /// **Known problems:** None.
19 /// if { true } { /* ... */ }
21 pub BLOCK_IN_IF_CONDITION_EXPR,
23 "braces that can be eliminated in conditions, e.g., `if { true } ...`"
26 declare_clippy_lint! {
27 /// **What it does:** Checks for `if` conditions that use blocks containing
28 /// statements, or conditions that use closures with blocks.
30 /// **Why is this bad?** Using blocks in the condition makes it hard to read.
32 /// **Known problems:** None.
36 /// if { let x = somefunc(); x } {}
38 /// if somefunc(|x| { x == 47 }) {}
40 pub BLOCK_IN_IF_CONDITION_STMT,
42 "complex blocks in conditions, e.g., `if { let x = true; x } ...`"
45 declare_lint_pass!(BlockInIfCondition => [BLOCK_IN_IF_CONDITION_EXPR, BLOCK_IN_IF_CONDITION_STMT]);
47 struct ExVisitor<'a, 'tcx: 'a> {
48 found_block: Option<&'tcx Expr>,
49 cx: &'a LateContext<'a, 'tcx>,
52 impl<'a, 'tcx: 'a> Visitor<'tcx> for ExVisitor<'a, 'tcx> {
53 fn visit_expr(&mut self, expr: &'tcx Expr) {
54 if let ExprKind::Closure(_, _, eid, _, _) = expr.node {
55 let body = self.cx.tcx.hir().body(eid);
57 if matches!(ex.node, ExprKind::Block(_, _)) && !in_macro(body.value.span) {
58 self.found_block = Some(ex);
62 walk_expr(self, expr);
64 fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
65 NestedVisitorMap::None
69 const BRACED_EXPR_MESSAGE: &str = "omit braces around single expression condition";
70 const COMPLEX_BLOCK_MESSAGE: &str = "in an 'if' condition, avoid complex blocks or closures with blocks; \
71 instead, move the block or closure higher and bind it with a 'let'";
73 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for BlockInIfCondition {
74 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
75 if let ExprKind::If(check, then, _) = &expr.node {
76 if let ExprKind::Block(block, _) = &check.node {
77 if block.rules == DefaultBlock {
78 if block.stmts.is_empty() {
79 if let Some(ex) = &block.expr {
80 // don't dig into the expression here, just suggest that they remove
82 if in_macro(expr.span) || differing_macro_contexts(expr.span, ex.span) {
87 BLOCK_IN_IF_CONDITION_EXPR,
92 snippet_block(cx, ex.span, ".."),
93 snippet_block(cx, then.span, "..")
98 let span = block.expr.as_ref().map_or_else(|| block.stmts[0].span, |e| e.span);
99 if in_macro(span) || differing_macro_contexts(expr.span, span) {
105 BLOCK_IN_IF_CONDITION_STMT,
107 COMPLEX_BLOCK_MESSAGE,
109 "try\nlet res = {};\nif res {} ... ",
110 snippet_block(cx, block.span, ".."),
111 snippet_block(cx, then.span, "..")
117 let mut visitor = ExVisitor { found_block: None, cx };
118 walk_expr(&mut visitor, check);
119 if let Some(block) = visitor.found_block {
120 span_lint(cx, BLOCK_IN_IF_CONDITION_STMT, block.span, COMPLEX_BLOCK_MESSAGE);