@@ -2,7 +2,7 @@ use clippy_utils::diagnostics::span_lint_and_sugg;
22use clippy_utils:: sugg:: Sugg ;
33use clippy_utils:: ty:: is_type_diagnostic_item;
44use 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} ;
66use if_chain:: if_chain;
77use rustc_errors:: Applicability ;
88use rustc_hir:: LangItem :: OptionSome ;
@@ -81,7 +81,6 @@ struct OptionIfLetElseOccurence {
8181 method_sugg : String ,
8282 some_expr : String ,
8383 none_expr : String ,
84- wrap_braces : bool ,
8584}
8685
8786/// 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>> {
106105 }
107106}
108107
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-
140108fn format_option_in_sugg ( cx : & LateContext < ' _ > , cond_expr : & Expr < ' _ > , as_ref : bool , as_mut : bool ) -> String {
141109 format ! (
142110 "{}{}" ,
@@ -176,7 +144,6 @@ fn detect_option_if_let_else<'tcx>(
176144 let none_body = extract_body_from_arm( & arms[ 1 ] ) ?;
177145 let method_sugg = if eager_or_lazy:: is_eagerness_candidate( cx, none_body) { "map_or" } else { "map_or_else" } ;
178146 let capture_name = id. name. to_ident_string( ) ;
179- let wrap_braces = should_wrap_in_braces( cx, expr) ;
180147 let ( as_ref, as_mut) = match & cond_expr. kind {
181148 ExprKind :: AddrOf ( _, Mutability :: Not , _) => ( true , false ) ,
182149 ExprKind :: AddrOf ( _, Mutability :: Mut , _) => ( false , true ) ,
@@ -192,7 +159,6 @@ fn detect_option_if_let_else<'tcx>(
192159 method_sugg: method_sugg. to_string( ) ,
193160 some_expr: format!( "|{}{}| {}" , capture_mut, capture_name, Sugg :: hir( cx, some_body, ".." ) ) ,
194161 none_expr: format!( "{}{}" , if method_sugg == "map_or" { "" } else { "|| " } , Sugg :: hir( cx, none_body, ".." ) ) ,
195- wrap_braces,
196162 } )
197163 } else {
198164 None
@@ -210,13 +176,8 @@ impl<'tcx> LateLintPass<'tcx> for OptionIfLetElse {
210176 format ! ( "use Option::{} instead of an if let/else" , detection. method_sugg) . as_str ( ) ,
211177 "try" ,
212178 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,
220181 ) ,
221182 Applicability :: MaybeIncorrect ,
222183 ) ;
0 commit comments