]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_lints/src/blocks_in_if_conditions.rs
Rollup merge of #73870 - sexxi-goose:projection-ty, r=nikomatsakis
[rust.git] / src / tools / clippy / clippy_lints / src / blocks_in_if_conditions.rs
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};
9
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.
13     ///
14     /// **Why is this bad?** Style, using blocks in the condition makes it hard to read.
15     ///
16     /// **Known problems:** None.
17     ///
18     /// **Examples:**
19     /// ```rust
20     /// // Bad
21     /// if { true } { /* ... */ }
22     ///
23     /// // Good
24     /// if true { /* ... */ }
25     /// ```
26     ///
27     /// // or
28     ///
29     /// ```rust
30     /// # fn somefunc() -> bool { true };
31     ///
32     /// // Bad
33     /// if { let x = somefunc(); x } { /* ... */ }
34     ///
35     /// // Good
36     /// let res = { let x = somefunc(); x };
37     /// if res { /* ... */ }
38     /// ```
39     pub BLOCKS_IN_IF_CONDITIONS,
40     style,
41     "useless or complex blocks that can be eliminated in conditions"
42 }
43
44 declare_lint_pass!(BlocksInIfConditions => [BLOCKS_IN_IF_CONDITIONS]);
45
46 struct ExVisitor<'a, 'tcx> {
47     found_block: Option<&'tcx Expr<'tcx>>,
48     cx: &'a LateContext<'tcx>,
49 }
50
51 impl<'a, 'tcx> Visitor<'tcx> for ExVisitor<'a, 'tcx> {
52     type Map = Map<'tcx>;
53
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);
57             let ex = &body.value;
58             if matches!(ex.kind, ExprKind::Block(_, _)) && !body.value.span.from_expansion() {
59                 self.found_block = Some(ex);
60                 return;
61             }
62         }
63         walk_expr(self, expr);
64     }
65     fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
66         NestedVisitorMap::None
67     }
68 }
69
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`";
73
74 impl<'tcx> LateLintPass<'tcx> for BlocksInIfConditions {
75     fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
76         if in_external_macro(cx.sess(), expr.span) {
77             return;
78         }
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
85                             // the block
86                             if expr.span.from_expansion() || differing_macro_contexts(expr.span, ex.span) {
87                                 return;
88                             }
89                             let mut applicability = Applicability::MachineApplicable;
90                             span_lint_and_sugg(
91                                 cx,
92                                 BLOCKS_IN_IF_CONDITIONS,
93                                 cond.span,
94                                 BRACED_EXPR_MESSAGE,
95                                 "try",
96                                 format!(
97                                     "{}",
98                                     snippet_block_with_applicability(
99                                         cx,
100                                         ex.span,
101                                         "..",
102                                         Some(expr.span),
103                                         &mut applicability
104                                     )
105                                 ),
106                                 applicability,
107                             );
108                         }
109                     } else {
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) {
112                             return;
113                         }
114                         // move block higher
115                         let mut applicability = Applicability::MachineApplicable;
116                         span_lint_and_sugg(
117                             cx,
118                             BLOCKS_IN_IF_CONDITIONS,
119                             expr.span.with_hi(cond.span.hi()),
120                             COMPLEX_BLOCK_MESSAGE,
121                             "try",
122                             format!(
123                                 "let res = {}; if res",
124                                 snippet_block_with_applicability(
125                                     cx,
126                                     block.span,
127                                     "..",
128                                     Some(expr.span),
129                                     &mut applicability
130                                 ),
131                             ),
132                             applicability,
133                         );
134                     }
135                 }
136             } else {
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);
141                 }
142             }
143         }
144     }
145 }