3 use rustc::declare_lint_pass;
4 use rustc::hir::intravisit::{walk_expr, NestedVisitorMap, Visitor};
5 use rustc::lint::{in_external_macro, LateContext, LateLintPass, LintArray, LintContext, LintPass};
7 use rustc_session::declare_tool_lint;
10 /// **What it does:** Checks for `if` conditions that use blocks to contain an
13 /// **Why is this bad?** It isn't really Rust style, same as using parentheses
14 /// to contain expressions.
16 /// **Known problems:** None.
20 /// if { true } { /* ... */ }
22 pub BLOCK_IN_IF_CONDITION_EXPR,
24 "braces that can be eliminated in conditions, e.g., `if { true } ...`"
27 declare_clippy_lint! {
28 /// **What it does:** Checks for `if` conditions that use blocks containing
29 /// statements, or conditions that use closures with blocks.
31 /// **Why is this bad?** Using blocks in the condition makes it hard to read.
33 /// **Known problems:** None.
37 /// if { let x = somefunc(); x } {}
39 /// if somefunc(|x| { x == 47 }) {}
41 pub BLOCK_IN_IF_CONDITION_STMT,
43 "complex blocks in conditions, e.g., `if { let x = true; x } ...`"
46 declare_lint_pass!(BlockInIfCondition => [BLOCK_IN_IF_CONDITION_EXPR, BLOCK_IN_IF_CONDITION_STMT]);
48 struct ExVisitor<'a, 'tcx> {
49 found_block: Option<&'tcx Expr<'tcx>>,
50 cx: &'a LateContext<'a, 'tcx>,
53 impl<'a, 'tcx> Visitor<'tcx> for ExVisitor<'a, 'tcx> {
54 fn visit_expr(&mut self, expr: &'tcx Expr<'tcx>) {
55 if let ExprKind::Closure(_, _, eid, _, _) = expr.kind {
56 let body = self.cx.tcx.hir().body(eid);
58 if matches!(ex.kind, ExprKind::Block(_, _)) && !body.value.span.from_expansion() {
59 self.found_block = Some(ex);
63 walk_expr(self, expr);
65 fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
66 NestedVisitorMap::None
70 const BRACED_EXPR_MESSAGE: &str = "omit braces around single expression condition";
71 const COMPLEX_BLOCK_MESSAGE: &str = "in an `if` condition, avoid complex blocks or closures with blocks; \
72 instead, move the block or closure higher and bind it with a `let`";
74 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for BlockInIfCondition {
75 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr<'_>) {
76 if in_external_macro(cx.sess(), expr.span) {
79 if let Some((check, then, _)) = higher::if_block(&expr) {
80 if let ExprKind::Block(block, _) = &check.kind {
81 if block.rules == BlockCheckMode::DefaultBlock {
82 if block.stmts.is_empty() {
83 if let Some(ex) = &block.expr {
84 // don't dig into the expression here, just suggest that they remove
86 if expr.span.from_expansion() || differing_macro_contexts(expr.span, ex.span) {
91 BLOCK_IN_IF_CONDITION_EXPR,
96 snippet_block(cx, ex.span, ".."),
97 snippet_block(cx, then.span, "..")
102 let span = block.expr.as_ref().map_or_else(|| block.stmts[0].span, |e| e.span);
103 if span.from_expansion() || differing_macro_contexts(expr.span, span) {
109 BLOCK_IN_IF_CONDITION_STMT,
111 COMPLEX_BLOCK_MESSAGE,
113 "try\nlet res = {};\nif res {} ... ",
114 snippet_block(cx, block.span, ".."),
115 snippet_block(cx, then.span, "..")
121 let mut visitor = ExVisitor { found_block: None, cx };
122 walk_expr(&mut visitor, check);
123 if let Some(block) = visitor.found_block {
124 span_lint(cx, BLOCK_IN_IF_CONDITION_STMT, block.span, COMPLEX_BLOCK_MESSAGE);