@@ -2,6 +2,7 @@ use crate::utils::*;
2
2
use matches:: matches;
3
3
use rustc:: hir:: map:: Map ;
4
4
use rustc:: lint:: in_external_macro;
5
+ use rustc_errors:: Applicability ;
5
6
use rustc_hir:: intravisit:: { walk_expr, NestedVisitorMap , Visitor } ;
6
7
use rustc_hir:: * ;
7
8
use rustc_lint:: { LateContext , LateLintPass , LintContext } ;
@@ -79,8 +80,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for BlockInIfCondition {
79
80
if in_external_macro ( cx. sess ( ) , expr. span ) {
80
81
return ;
81
82
}
82
- if let Some ( ( check , then , _) ) = higher:: if_block ( & expr) {
83
- if let ExprKind :: Block ( block, _) = & check . kind {
83
+ if let Some ( ( cond , _ , _) ) = higher:: if_block ( & expr) {
84
+ if let ExprKind :: Block ( block, _) = & cond . kind {
84
85
if block. rules == BlockCheckMode :: DefaultBlock {
85
86
if block. stmts . is_empty ( ) {
86
87
if let Some ( ex) = & block. expr {
@@ -89,16 +90,24 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for BlockInIfCondition {
89
90
if expr. span . from_expansion ( ) || differing_macro_contexts ( expr. span , ex. span ) {
90
91
return ;
91
92
}
92
- span_lint_and_help (
93
+ let mut applicability = Applicability :: MachineApplicable ;
94
+ span_lint_and_sugg (
93
95
cx,
94
96
BLOCK_IN_IF_CONDITION_EXPR ,
95
- check . span ,
97
+ cond . span ,
96
98
BRACED_EXPR_MESSAGE ,
97
- & format ! (
98
- "try\n if {} {} ... " ,
99
- snippet_block( cx, ex. span, ".." ) ,
100
- snippet_block( cx, then. span, ".." )
99
+ "try" ,
100
+ format ! (
101
+ "{}" ,
102
+ snippet_block_with_applicability(
103
+ cx,
104
+ ex. span,
105
+ ".." ,
106
+ Some ( expr. span) ,
107
+ & mut applicability
108
+ )
101
109
) ,
110
+ applicability,
102
111
) ;
103
112
}
104
113
} else {
@@ -107,22 +116,30 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for BlockInIfCondition {
107
116
return ;
108
117
}
109
118
// move block higher
110
- span_lint_and_help (
119
+ let mut applicability = Applicability :: MachineApplicable ;
120
+ span_lint_and_sugg (
111
121
cx,
112
122
BLOCK_IN_IF_CONDITION_STMT ,
113
- check . span ,
123
+ expr . span . with_hi ( cond . span . hi ( ) ) ,
114
124
COMPLEX_BLOCK_MESSAGE ,
115
- & format ! (
116
- "try\n let res = {};\n if res {} ... " ,
117
- snippet_block( cx, block. span, ".." ) ,
118
- snippet_block( cx, then. span, ".." )
125
+ "try" ,
126
+ format ! (
127
+ "let res = {}; if res" ,
128
+ snippet_block_with_applicability(
129
+ cx,
130
+ block. span,
131
+ ".." ,
132
+ Some ( expr. span) ,
133
+ & mut applicability
134
+ ) ,
119
135
) ,
136
+ applicability,
120
137
) ;
121
138
}
122
139
}
123
140
} else {
124
141
let mut visitor = ExVisitor { found_block : None , cx } ;
125
- walk_expr ( & mut visitor, check ) ;
142
+ walk_expr ( & mut visitor, cond ) ;
126
143
if let Some ( block) = visitor. found_block {
127
144
span_lint ( cx, BLOCK_IN_IF_CONDITION_STMT , block. span , COMPLEX_BLOCK_MESSAGE ) ;
128
145
}
0 commit comments