3 use rustc::hir::map::Map;
4 use rustc::lint::in_external_macro;
5 use rustc_hir::intravisit::{walk_expr, NestedVisitorMap, Visitor};
7 use rustc_lint::{LateContext, LateLintPass, LintContext};
8 use rustc_session::{declare_lint_pass, declare_tool_lint};
10 declare_clippy_lint! {
11 /// **What it does:** Checks for `if` conditions that use blocks to contain an
14 /// **Why is this bad?** It isn't really Rust style, same as using parentheses
15 /// to contain expressions.
17 /// **Known problems:** None.
21 /// if { true } { /* ... */ }
23 pub BLOCK_IN_IF_CONDITION_EXPR,
25 "braces that can be eliminated in conditions, e.g., `if { true } ...`"
28 declare_clippy_lint! {
29 /// **What it does:** Checks for `if` conditions that use blocks containing
30 /// statements, or conditions that use closures with blocks.
32 /// **Why is this bad?** Using blocks in the condition makes it hard to read.
34 /// **Known problems:** None.
38 /// if { let x = somefunc(); x } {}
40 /// if somefunc(|x| { x == 47 }) {}
42 pub BLOCK_IN_IF_CONDITION_STMT,
44 "complex blocks in conditions, e.g., `if { let x = true; x } ...`"
47 declare_lint_pass!(BlockInIfCondition => [BLOCK_IN_IF_CONDITION_EXPR, BLOCK_IN_IF_CONDITION_STMT]);
49 struct ExVisitor<'a, 'tcx> {
50 found_block: Option<&'tcx Expr<'tcx>>,
51 cx: &'a LateContext<'a, 'tcx>,
54 impl<'a, 'tcx> Visitor<'tcx> for ExVisitor<'a, 'tcx> {
57 fn visit_expr(&mut self, expr: &'tcx Expr<'tcx>) {
58 if let ExprKind::Closure(_, _, eid, _, _) = expr.kind {
59 let body = self.cx.tcx.hir().body(eid);
61 if matches!(ex.kind, ExprKind::Block(_, _)) && !body.value.span.from_expansion() {
62 self.found_block = Some(ex);
66 walk_expr(self, expr);
68 fn nested_visit_map(&mut self) -> NestedVisitorMap<'_, Self::Map> {
69 NestedVisitorMap::None
73 const BRACED_EXPR_MESSAGE: &str = "omit braces around single expression condition";
74 const COMPLEX_BLOCK_MESSAGE: &str = "in an `if` condition, avoid complex blocks or closures with blocks; \
75 instead, move the block or closure higher and bind it with a `let`";
77 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for BlockInIfCondition {
78 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr<'_>) {
79 if in_external_macro(cx.sess(), expr.span) {
82 if let Some((check, then, _)) = higher::if_block(&expr) {
83 if let ExprKind::Block(block, _) = &check.kind {
84 if block.rules == BlockCheckMode::DefaultBlock {
85 if block.stmts.is_empty() {
86 if let Some(ex) = &block.expr {
87 // don't dig into the expression here, just suggest that they remove
89 if expr.span.from_expansion() || 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, "..")
105 let span = block.expr.as_ref().map_or_else(|| block.stmts[0].span, |e| e.span);
106 if span.from_expansion() || differing_macro_contexts(expr.span, span) {
112 BLOCK_IN_IF_CONDITION_STMT,
114 COMPLEX_BLOCK_MESSAGE,
116 "try\nlet res = {};\nif res {} ... ",
117 snippet_block(cx, block.span, ".."),
118 snippet_block(cx, then.span, "..")
124 let mut visitor = ExVisitor { found_block: None, cx };
125 walk_expr(&mut visitor, check);
126 if let Some(block) = visitor.found_block {
127 span_lint(cx, BLOCK_IN_IF_CONDITION_STMT, block.span, COMPLEX_BLOCK_MESSAGE);