1 use crate::utils::{differing_macro_contexts, higher, snippet_block_with_applicability, span_lint, span_lint_and_sugg};
2 use rustc_errors::Applicability;
3 use rustc_hir::intravisit::{walk_expr, NestedVisitorMap, Visitor};
4 use rustc_hir::{BlockCheckMode, Expr, ExprKind};
5 use rustc_lint::{LateContext, LateLintPass, LintContext};
6 use rustc_middle::hir::map::Map;
7 use rustc_middle::lint::in_external_macro;
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 containing an
12 /// expression, statements or conditions that use closures with blocks.
14 /// **Why is this bad?** Style, using blocks in the condition makes it hard to read.
16 /// **Known problems:** None.
21 /// if { true } { /* ... */ }
24 /// if true { /* ... */ }
30 /// # fn somefunc() -> bool { true };
32 /// if { let x = somefunc(); x } { /* ... */ }
35 /// let res = { let x = somefunc(); x };
36 /// if res { /* ... */ }
38 pub BLOCKS_IN_IF_CONDITIONS,
40 "useless or complex blocks that can be eliminated in conditions"
43 declare_lint_pass!(BlocksInIfConditions => [BLOCKS_IN_IF_CONDITIONS]);
45 struct ExVisitor<'a, 'tcx> {
46 found_block: Option<&'tcx Expr<'tcx>>,
47 cx: &'a LateContext<'tcx>,
50 impl<'a, 'tcx> Visitor<'tcx> for ExVisitor<'a, 'tcx> {
53 fn visit_expr(&mut self, expr: &'tcx Expr<'tcx>) {
54 if let ExprKind::Closure(_, _, eid, _, _) = expr.kind {
55 let body = self.cx.tcx.hir().body(eid);
57 if matches!(ex.kind, ExprKind::Block(_, _)) && !body.value.span.from_expansion() {
58 self.found_block = Some(ex);
62 walk_expr(self, expr);
64 fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
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<'tcx> LateLintPass<'tcx> for BlocksInIfConditions {
74 fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
75 if in_external_macro(cx.sess(), expr.span) {
78 if let Some((cond, _, _)) = higher::if_block(&expr) {
79 if let ExprKind::Block(block, _) = &cond.kind {
80 if block.rules == BlockCheckMode::DefaultBlock {
81 if block.stmts.is_empty() {
82 if let Some(ex) = &block.expr {
83 // don't dig into the expression here, just suggest that they remove
85 if expr.span.from_expansion() || differing_macro_contexts(expr.span, ex.span) {
88 let mut applicability = Applicability::MachineApplicable;
91 BLOCKS_IN_IF_CONDITIONS,
97 snippet_block_with_applicability(
109 let span = block.expr.as_ref().map_or_else(|| block.stmts[0].span, |e| e.span);
110 if span.from_expansion() || differing_macro_contexts(expr.span, span) {
114 let mut applicability = Applicability::MachineApplicable;
117 BLOCKS_IN_IF_CONDITIONS,
118 expr.span.with_hi(cond.span.hi()),
119 COMPLEX_BLOCK_MESSAGE,
122 "let res = {}; if res",
123 snippet_block_with_applicability(
136 let mut visitor = ExVisitor { found_block: None, cx };
137 walk_expr(&mut visitor, cond);
138 if let Some(block) = visitor.found_block {
139 span_lint(cx, BLOCKS_IN_IF_CONDITIONS, block.span, COMPLEX_BLOCK_MESSAGE);