@@ -8,43 +8,40 @@ use rustc_middle::lint::in_external_macro;
88use rustc_session:: { declare_lint_pass, declare_tool_lint} ;
99
1010declare_clippy_lint ! {
11- /// **What it does:** Checks for `if` conditions that use blocks to contain an
12- /// expression.
11+ /// **What it does:** Checks for `if` conditions that use blocks containing an
12+ /// expression, statements or conditions that use closures with blocks .
1313 ///
14- /// **Why is this bad?** It isn't really Rust style, same as using parentheses
15- /// to contain expressions.
14+ /// **Why is this bad?** Style, using blocks in the condition makes it hard to read.
1615 ///
1716 /// **Known problems:** None.
1817 ///
19- /// **Example :**
18+ /// **Examples :**
2019 /// ```rust
20+ /// // Bad
2121 /// if { true } { /* ... */ }
22+ ///
23+ /// // Good
24+ /// if true { /* ... */ }
2225 /// ```
23- pub BLOCK_IN_IF_CONDITION_EXPR ,
24- style,
25- "braces that can be eliminated in conditions, e.g., `if { true } ...`"
26- }
27-
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.
3126 ///
32- /// **Why is this bad?** Using blocks in the condition makes it hard to read.
27+ /// // or
3328 ///
34- /// **Known problems:** None.
29+ /// ```rust
30+ /// # fn somefunc() -> bool { true };
3531 ///
36- /// **Example:**
37- /// ```rust,ignore
38- /// if { let x = somefunc(); x } {}
39- /// // or
40- /// if somefunc(|x| { x == 47 }) {}
32+ /// // Bad
33+ /// if { let x = somefunc(); x } { /* ... */ }
34+ ///
35+ /// // Good
36+ /// let res = { let x = somefunc(); x };
37+ /// if res { /* ... */ }
4138 /// ```
42- pub BLOCK_IN_IF_CONDITION_STMT ,
39+ pub BLOCK_IN_IF_CONDITION ,
4340 style,
44- "complex blocks in conditions, e.g., `if { let x = true; x } ...` "
41+ "useless or complex blocks that can be eliminated in conditions "
4542}
4643
47- declare_lint_pass ! ( BlockInIfCondition => [ BLOCK_IN_IF_CONDITION_EXPR , BLOCK_IN_IF_CONDITION_STMT ] ) ;
44+ declare_lint_pass ! ( BlockInIfCondition => [ BLOCK_IN_IF_CONDITION ] ) ;
4845
4946struct ExVisitor < ' a , ' tcx > {
5047 found_block : Option < & ' tcx Expr < ' tcx > > ,
@@ -72,7 +69,7 @@ impl<'a, 'tcx> Visitor<'tcx> for ExVisitor<'a, 'tcx> {
7269
7370const BRACED_EXPR_MESSAGE : & str = "omit braces around single expression condition" ;
7471const 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`";
72+ instead, move the block or closure higher and bind it with a `let`";
7673
7774impl < ' a , ' tcx > LateLintPass < ' a , ' tcx > for BlockInIfCondition {
7875 fn check_expr ( & mut self , cx : & LateContext < ' a , ' tcx > , expr : & ' tcx Expr < ' _ > ) {
@@ -92,7 +89,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for BlockInIfCondition {
9289 let mut applicability = Applicability :: MachineApplicable ;
9390 span_lint_and_sugg (
9491 cx,
95- BLOCK_IN_IF_CONDITION_EXPR ,
92+ BLOCK_IN_IF_CONDITION ,
9693 cond. span ,
9794 BRACED_EXPR_MESSAGE ,
9895 "try" ,
@@ -118,7 +115,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for BlockInIfCondition {
118115 let mut applicability = Applicability :: MachineApplicable ;
119116 span_lint_and_sugg (
120117 cx,
121- BLOCK_IN_IF_CONDITION_STMT ,
118+ BLOCK_IN_IF_CONDITION ,
122119 expr. span . with_hi ( cond. span . hi ( ) ) ,
123120 COMPLEX_BLOCK_MESSAGE ,
124121 "try" ,
@@ -140,7 +137,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for BlockInIfCondition {
140137 let mut visitor = ExVisitor { found_block : None , cx } ;
141138 walk_expr ( & mut visitor, cond) ;
142139 if let Some ( block) = visitor. found_block {
143- span_lint ( cx, BLOCK_IN_IF_CONDITION_STMT , block. span , COMPLEX_BLOCK_MESSAGE ) ;
140+ span_lint ( cx, BLOCK_IN_IF_CONDITION , block. span , COMPLEX_BLOCK_MESSAGE ) ;
144141 }
145142 }
146143 }
0 commit comments