@@ -3,8 +3,8 @@ use clippy_utils::source::{snippet_opt, snippet_with_context};
33use clippy_utils:: sugg:: has_enclosing_paren;
44use clippy_utils:: visitors:: { for_each_expr_with_closures, Descend } ;
55use clippy_utils:: {
6- fn_def_id, is_from_proc_macro, is_inside_let_else, is_res_lang_ctor, path_res, path_to_local_id , span_contains_cfg ,
7- span_find_starting_semi,
6+ binary_expr_needs_parentheses , fn_def_id, is_from_proc_macro, is_inside_let_else, is_res_lang_ctor, path_res,
7+ path_to_local_id , span_contains_cfg , span_find_starting_semi,
88} ;
99use core:: ops:: ControlFlow ;
1010use rustc_errors:: Applicability ;
@@ -129,7 +129,7 @@ enum RetReplacement<'tcx> {
129129 Empty ,
130130 Block ,
131131 Unit ,
132- IfSequence ( Cow < ' tcx , str > , Applicability ) ,
132+ NeedsPar ( Cow < ' tcx , str > , Applicability ) ,
133133 Expr ( Cow < ' tcx , str > , Applicability ) ,
134134}
135135
@@ -139,13 +139,13 @@ impl<'tcx> RetReplacement<'tcx> {
139139 Self :: Empty | Self :: Expr ( ..) => "remove `return`" ,
140140 Self :: Block => "replace `return` with an empty block" ,
141141 Self :: Unit => "replace `return` with a unit value" ,
142- Self :: IfSequence ( ..) => "remove `return` and wrap the sequence with parentheses" ,
142+ Self :: NeedsPar ( ..) => "remove `return` and wrap the sequence with parentheses" ,
143143 }
144144 }
145145
146146 fn applicability ( & self ) -> Applicability {
147147 match self {
148- Self :: Expr ( _, ap) | Self :: IfSequence ( _, ap) => * ap,
148+ Self :: Expr ( _, ap) | Self :: NeedsPar ( _, ap) => * ap,
149149 _ => Applicability :: MachineApplicable ,
150150 }
151151 }
@@ -157,7 +157,7 @@ impl<'tcx> Display for RetReplacement<'tcx> {
157157 Self :: Empty => write ! ( f, "" ) ,
158158 Self :: Block => write ! ( f, "{{}}" ) ,
159159 Self :: Unit => write ! ( f, "()" ) ,
160- Self :: IfSequence ( inner, _) => write ! ( f, "({inner})" ) ,
160+ Self :: NeedsPar ( inner, _) => write ! ( f, "({inner})" ) ,
161161 Self :: Expr ( inner, _) => write ! ( f, "{inner}" ) ,
162162 }
163163 }
@@ -244,7 +244,11 @@ impl<'tcx> LateLintPass<'tcx> for Return {
244244 err. span_label ( local. span , "unnecessary `let` binding" ) ;
245245
246246 if let Some ( mut snippet) = snippet_opt ( cx, initexpr. span ) {
247- if !cx. typeck_results ( ) . expr_adjustments ( retexpr) . is_empty ( ) {
247+ if binary_expr_needs_parentheses ( initexpr) {
248+ if !has_enclosing_paren ( & snippet) {
249+ snippet = format ! ( "({snippet})" ) ;
250+ }
251+ } else if !cx. typeck_results ( ) . expr_adjustments ( retexpr) . is_empty ( ) {
248252 if !has_enclosing_paren ( & snippet) {
249253 snippet = format ! ( "({snippet})" ) ;
250254 }
@@ -349,8 +353,8 @@ fn check_final_expr<'tcx>(
349353
350354 let mut applicability = Applicability :: MachineApplicable ;
351355 let ( snippet, _) = snippet_with_context ( cx, inner_expr. span , ret_span. ctxt ( ) , ".." , & mut applicability) ;
352- if expr_contains_conjunctive_ifs ( inner_expr) {
353- RetReplacement :: IfSequence ( snippet, applicability)
356+ if binary_expr_needs_parentheses ( inner_expr) {
357+ RetReplacement :: NeedsPar ( snippet, applicability)
354358 } else {
355359 RetReplacement :: Expr ( snippet, applicability)
356360 }
@@ -404,18 +408,6 @@ fn check_final_expr<'tcx>(
404408 }
405409}
406410
407- fn expr_contains_conjunctive_ifs < ' tcx > ( expr : & ' tcx Expr < ' tcx > ) -> bool {
408- fn contains_if ( expr : & Expr < ' _ > , on_if : bool ) -> bool {
409- match expr. kind {
410- ExprKind :: If ( ..) => on_if,
411- ExprKind :: Binary ( _, left, right) => contains_if ( left, true ) || contains_if ( right, true ) ,
412- _ => false ,
413- }
414- }
415-
416- contains_if ( expr, false )
417- }
418-
419411fn emit_return_lint (
420412 cx : & LateContext < ' _ > ,
421413 ret_span : Span ,
0 commit comments