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 };
33 /// if { let x = somefunc(); x } { /* ... */ }
36 /// let res = { let x = somefunc(); x };
37 /// if res { /* ... */ }
39 pub BLOCKS_IN_IF_CONDITIONS,
41 "useless or complex blocks that can be eliminated in conditions"
44 declare_lint_pass!(BlocksInIfConditions => [BLOCKS_IN_IF_CONDITIONS]);
46 struct ExVisitor<'a, 'tcx> {
47 found_block: Option<&'tcx Expr<'tcx>>,
48 cx: &'a LateContext<'a, 'tcx>,
51 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(&mut self) -> NestedVisitorMap<Self::Map> {
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 BlocksInIfConditions {
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((cond, _, _)) = higher::if_block(&expr) {
80 if let ExprKind::Block(block, _) = &cond.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) {
89 let mut applicability = Applicability::MachineApplicable;
92 BLOCKS_IN_IF_CONDITIONS,
98 snippet_block_with_applicability(
110 let span = block.expr.as_ref().map_or_else(|| block.stmts[0].span, |e| e.span);
111 if span.from_expansion() || differing_macro_contexts(expr.span, span) {
115 let mut applicability = Applicability::MachineApplicable;
118 BLOCKS_IN_IF_CONDITIONS,
119 expr.span.with_hi(cond.span.hi()),
120 COMPLEX_BLOCK_MESSAGE,
123 "let res = {}; if res",
124 snippet_block_with_applicability(
137 let mut visitor = ExVisitor { found_block: None, cx };
138 walk_expr(&mut visitor, cond);
139 if let Some(block) = visitor.found_block {
140 span_lint(cx, BLOCKS_IN_IF_CONDITIONS, block.span, COMPLEX_BLOCK_MESSAGE);