@@ -2,7 +2,7 @@ use clippy_utils::diagnostics::span_lint_and_sugg;
2
2
use clippy_utils:: sugg:: Sugg ;
3
3
use clippy_utils:: ty:: is_type_diagnostic_item;
4
4
use clippy_utils:: usage:: contains_return_break_continue_macro;
5
- use clippy_utils:: { eager_or_lazy, get_enclosing_block , in_macro, is_else_clause, is_lang_ctor} ;
5
+ use clippy_utils:: { eager_or_lazy, in_macro, is_else_clause, is_lang_ctor} ;
6
6
use if_chain:: if_chain;
7
7
use rustc_errors:: Applicability ;
8
8
use rustc_hir:: LangItem :: OptionSome ;
@@ -81,7 +81,6 @@ struct OptionIfLetElseOccurence {
81
81
method_sugg : String ,
82
82
some_expr : String ,
83
83
none_expr : String ,
84
- wrap_braces : bool ,
85
84
}
86
85
87
86
/// Extracts the body of a given arm. If the arm contains only an expression,
@@ -106,37 +105,6 @@ fn extract_body_from_arm<'a>(arm: &'a Arm<'a>) -> Option<&'a Expr<'a>> {
106
105
}
107
106
}
108
107
109
- /// If this is the else body of an if/else expression, then we need to wrap
110
- /// it in curly braces. Otherwise, we don't.
111
- fn should_wrap_in_braces ( cx : & LateContext < ' _ > , expr : & Expr < ' _ > ) -> bool {
112
- get_enclosing_block ( cx, expr. hir_id ) . map_or ( false , |parent| {
113
- let mut should_wrap = false ;
114
-
115
- if let Some ( Expr {
116
- kind :
117
- ExprKind :: Match (
118
- _,
119
- arms,
120
- MatchSource :: IfLetDesugar {
121
- contains_else_clause : true ,
122
- } ,
123
- ) ,
124
- ..
125
- } ) = parent. expr
126
- {
127
- should_wrap = expr. hir_id == arms[ 1 ] . body . hir_id ;
128
- } else if let Some ( Expr {
129
- kind : ExprKind :: If ( _, _, Some ( else_clause) ) ,
130
- ..
131
- } ) = parent. expr
132
- {
133
- should_wrap = expr. hir_id == else_clause. hir_id ;
134
- }
135
-
136
- should_wrap
137
- } )
138
- }
139
-
140
108
fn format_option_in_sugg ( cx : & LateContext < ' _ > , cond_expr : & Expr < ' _ > , as_ref : bool , as_mut : bool ) -> String {
141
109
format ! (
142
110
"{}{}" ,
@@ -176,7 +144,6 @@ fn detect_option_if_let_else<'tcx>(
176
144
let none_body = extract_body_from_arm( & arms[ 1 ] ) ?;
177
145
let method_sugg = if eager_or_lazy:: is_eagerness_candidate( cx, none_body) { "map_or" } else { "map_or_else" } ;
178
146
let capture_name = id. name. to_ident_string( ) ;
179
- let wrap_braces = should_wrap_in_braces( cx, expr) ;
180
147
let ( as_ref, as_mut) = match & cond_expr. kind {
181
148
ExprKind :: AddrOf ( _, Mutability :: Not , _) => ( true , false ) ,
182
149
ExprKind :: AddrOf ( _, Mutability :: Mut , _) => ( false , true ) ,
@@ -192,7 +159,6 @@ fn detect_option_if_let_else<'tcx>(
192
159
method_sugg: method_sugg. to_string( ) ,
193
160
some_expr: format!( "|{}{}| {}" , capture_mut, capture_name, Sugg :: hir( cx, some_body, ".." ) ) ,
194
161
none_expr: format!( "{}{}" , if method_sugg == "map_or" { "" } else { "|| " } , Sugg :: hir( cx, none_body, ".." ) ) ,
195
- wrap_braces,
196
162
} )
197
163
} else {
198
164
None
@@ -210,13 +176,8 @@ impl<'tcx> LateLintPass<'tcx> for OptionIfLetElse {
210
176
format ! ( "use Option::{} instead of an if let/else" , detection. method_sugg) . as_str ( ) ,
211
177
"try" ,
212
178
format ! (
213
- "{}{}.{}({}, {}){}" ,
214
- if detection. wrap_braces { "{ " } else { "" } ,
215
- detection. option,
216
- detection. method_sugg,
217
- detection. none_expr,
218
- detection. some_expr,
219
- if detection. wrap_braces { " }" } else { "" } ,
179
+ "{}.{}({}, {})" ,
180
+ detection. option, detection. method_sugg, detection. none_expr, detection. some_expr,
220
181
) ,
221
182
Applicability :: MaybeIncorrect ,
222
183
) ;
0 commit comments