@@ -36,31 +36,36 @@ declare_clippy_lint! {
3636 /// if res { /* ... */ }
3737 /// ```
3838 #[ clippy:: version = "1.45.0" ]
39- pub BLOCKS_IN_IF_CONDITIONS ,
39+ pub BLOCKS_IN_CONDITIONS ,
4040 style,
4141 "useless or complex blocks that can be eliminated in conditions"
4242}
4343
44- declare_lint_pass ! ( BlocksInIfConditions => [ BLOCKS_IN_IF_CONDITIONS ] ) ;
44+ declare_lint_pass ! ( BlocksInConditions => [ BLOCKS_IN_CONDITIONS ] ) ;
4545
4646const BRACED_EXPR_MESSAGE : & str = "omit braces around single expression condition" ;
47- const COMPLEX_BLOCK_MESSAGE : & str = "in an `if` condition, avoid complex blocks or closures with blocks; \
48- instead, move the block or closure higher and bind it with a `let`";
4947
50- impl < ' tcx > LateLintPass < ' tcx > for BlocksInIfConditions {
48+ impl < ' tcx > LateLintPass < ' tcx > for BlocksInConditions {
5149 fn check_expr ( & mut self , cx : & LateContext < ' tcx > , expr : & ' tcx Expr < ' _ > ) {
5250 if in_external_macro ( cx. sess ( ) , expr. span ) {
5351 return ;
5452 }
55- let Some ( ( cond, keyword) ) = higher:: If :: hir ( expr) . map ( |hif| ( hif. cond , "if" ) ) . or (
56- if let ExprKind :: Match ( match_ex, _, MatchSource :: Normal ) = expr. kind {
57- Some ( ( match_ex, "match" ) )
53+
54+ let Some ( ( cond, keyword, desc) ) = higher:: If :: hir ( expr)
55+ . map ( |hif| ( hif. cond , "if" , "an `if` condition" ) )
56+ . or ( if let ExprKind :: Match ( match_ex, _, MatchSource :: Normal ) = expr. kind {
57+ Some ( ( match_ex, "match" , "a `match` scrutinee" ) )
5858 } else {
5959 None
60- } ,
61- ) else {
60+ } )
61+ else {
6262 return ;
6363 } ;
64+ let complex_block_message = & format ! (
65+ "in {desc}, avoid complex blocks or closures with blocks; \
66+ instead, move the block or closure higher and bind it with a `let`",
67+ ) ;
68+
6469 if let ExprKind :: Block ( block, _) = & cond. kind {
6570 if block. rules == BlockCheckMode :: DefaultBlock {
6671 if block. stmts . is_empty ( ) {
@@ -73,20 +78,12 @@ impl<'tcx> LateLintPass<'tcx> for BlocksInIfConditions {
7378 let mut applicability = Applicability :: MachineApplicable ;
7479 span_lint_and_sugg (
7580 cx,
76- BLOCKS_IN_IF_CONDITIONS ,
81+ BLOCKS_IN_CONDITIONS ,
7782 cond. span ,
7883 BRACED_EXPR_MESSAGE ,
7984 "try" ,
80- format ! (
81- "{}" ,
82- snippet_block_with_applicability(
83- cx,
84- ex. span,
85- ".." ,
86- Some ( expr. span) ,
87- & mut applicability
88- )
89- ) ,
85+ snippet_block_with_applicability ( cx, ex. span , ".." , Some ( expr. span ) , & mut applicability)
86+ . to_string ( ) ,
9087 applicability,
9188 ) ;
9289 }
@@ -99,9 +96,9 @@ impl<'tcx> LateLintPass<'tcx> for BlocksInIfConditions {
9996 let mut applicability = Applicability :: MachineApplicable ;
10097 span_lint_and_sugg (
10198 cx,
102- BLOCKS_IN_IF_CONDITIONS ,
99+ BLOCKS_IN_CONDITIONS ,
103100 expr. span . with_hi ( cond. span . hi ( ) ) ,
104- COMPLEX_BLOCK_MESSAGE ,
101+ complex_block_message ,
105102 "try" ,
106103 format ! (
107104 "let res = {}; {keyword} res" ,
@@ -128,7 +125,7 @@ impl<'tcx> LateLintPass<'tcx> for BlocksInIfConditions {
128125 let ex = & body. value ;
129126 if let ExprKind :: Block ( block, _) = ex. kind {
130127 if !body. value . span . from_expansion ( ) && !block. stmts . is_empty ( ) {
131- span_lint ( cx, BLOCKS_IN_IF_CONDITIONS , ex. span , COMPLEX_BLOCK_MESSAGE ) ;
128+ span_lint ( cx, BLOCKS_IN_CONDITIONS , ex. span , complex_block_message ) ;
132129 return ControlFlow :: Continue ( Descend :: No ) ;
133130 }
134131 }
0 commit comments