@@ -3,8 +3,8 @@ use clippy_utils::source::{snippet_opt, snippet_with_context};
3
3
use clippy_utils:: sugg:: has_enclosing_paren;
4
4
use clippy_utils:: visitors:: { for_each_expr_with_closures, Descend } ;
5
5
use 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,
8
8
} ;
9
9
use core:: ops:: ControlFlow ;
10
10
use rustc_errors:: Applicability ;
@@ -129,7 +129,7 @@ enum RetReplacement<'tcx> {
129
129
Empty ,
130
130
Block ,
131
131
Unit ,
132
- IfSequence ( Cow < ' tcx , str > , Applicability ) ,
132
+ NeedsPar ( Cow < ' tcx , str > , Applicability ) ,
133
133
Expr ( Cow < ' tcx , str > , Applicability ) ,
134
134
}
135
135
@@ -139,13 +139,13 @@ impl<'tcx> RetReplacement<'tcx> {
139
139
Self :: Empty | Self :: Expr ( ..) => "remove `return`" ,
140
140
Self :: Block => "replace `return` with an empty block" ,
141
141
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" ,
143
143
}
144
144
}
145
145
146
146
fn applicability ( & self ) -> Applicability {
147
147
match self {
148
- Self :: Expr ( _, ap) | Self :: IfSequence ( _, ap) => * ap,
148
+ Self :: Expr ( _, ap) | Self :: NeedsPar ( _, ap) => * ap,
149
149
_ => Applicability :: MachineApplicable ,
150
150
}
151
151
}
@@ -157,7 +157,7 @@ impl<'tcx> Display for RetReplacement<'tcx> {
157
157
Self :: Empty => write ! ( f, "" ) ,
158
158
Self :: Block => write ! ( f, "{{}}" ) ,
159
159
Self :: Unit => write ! ( f, "()" ) ,
160
- Self :: IfSequence ( inner, _) => write ! ( f, "({inner})" ) ,
160
+ Self :: NeedsPar ( inner, _) => write ! ( f, "({inner})" ) ,
161
161
Self :: Expr ( inner, _) => write ! ( f, "{inner}" ) ,
162
162
}
163
163
}
@@ -244,7 +244,11 @@ impl<'tcx> LateLintPass<'tcx> for Return {
244
244
err. span_label ( local. span , "unnecessary `let` binding" ) ;
245
245
246
246
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 ( ) {
248
252
if !has_enclosing_paren ( & snippet) {
249
253
snippet = format ! ( "({snippet})" ) ;
250
254
}
@@ -349,8 +353,8 @@ fn check_final_expr<'tcx>(
349
353
350
354
let mut applicability = Applicability :: MachineApplicable ;
351
355
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)
354
358
} else {
355
359
RetReplacement :: Expr ( snippet, applicability)
356
360
}
@@ -404,18 +408,6 @@ fn check_final_expr<'tcx>(
404
408
}
405
409
}
406
410
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
-
419
411
fn emit_return_lint (
420
412
cx : & LateContext < ' _ > ,
421
413
ret_span : Span ,
0 commit comments