1
1
use clippy_utils:: diagnostics:: { span_lint_and_sugg, span_lint_hir_and_then} ;
2
- use clippy_utils:: source:: { SpanRangeExt , snippet_with_context } ;
3
- use clippy_utils:: sugg:: has_enclosing_paren;
2
+ use clippy_utils:: source:: SpanRangeExt ;
3
+ use clippy_utils:: sugg:: { Sugg , has_enclosing_paren} ;
4
4
use clippy_utils:: visitors:: { Descend , for_each_expr} ;
5
5
use clippy_utils:: {
6
6
binary_expr_needs_parentheses, fn_def_id, is_from_proc_macro, is_inside_let_else, is_res_lang_ctor,
@@ -137,7 +137,6 @@ enum RetReplacement<'tcx> {
137
137
Empty ,
138
138
Block ,
139
139
Unit ,
140
- NeedsPar ( Cow < ' tcx , str > , Applicability ) ,
141
140
Expr ( Cow < ' tcx , str > , Applicability ) ,
142
141
}
143
142
@@ -147,13 +146,12 @@ impl RetReplacement<'_> {
147
146
Self :: Empty | Self :: Expr ( ..) => "remove `return`" ,
148
147
Self :: Block => "replace `return` with an empty block" ,
149
148
Self :: Unit => "replace `return` with a unit value" ,
150
- Self :: NeedsPar ( ..) => "remove `return` and wrap the sequence with parentheses" ,
151
149
}
152
150
}
153
151
154
152
fn applicability ( & self ) -> Applicability {
155
153
match self {
156
- Self :: Expr ( _, ap) | Self :: NeedsPar ( _ , ap ) => * ap,
154
+ Self :: Expr ( _, ap) => * ap,
157
155
_ => Applicability :: MachineApplicable ,
158
156
}
159
157
}
@@ -165,7 +163,6 @@ impl Display for RetReplacement<'_> {
165
163
Self :: Empty => write ! ( f, "" ) ,
166
164
Self :: Block => write ! ( f, "{{}}" ) ,
167
165
Self :: Unit => write ! ( f, "()" ) ,
168
- Self :: NeedsPar ( inner, _) => write ! ( f, "({inner})" ) ,
169
166
Self :: Expr ( inner, _) => write ! ( f, "{inner}" ) ,
170
167
}
171
168
}
@@ -365,12 +362,13 @@ fn check_final_expr<'tcx>(
365
362
}
366
363
367
364
let mut applicability = Applicability :: MachineApplicable ;
368
- let ( snippet, _) = snippet_with_context ( cx, inner_expr. span , ret_span. ctxt ( ) , ".." , & mut applicability) ;
369
- if binary_expr_needs_parentheses ( inner_expr) {
370
- RetReplacement :: NeedsPar ( snippet, applicability)
371
- } else {
372
- RetReplacement :: Expr ( snippet, applicability)
373
- }
365
+ let sugg = Sugg :: hir_with_context ( cx, inner_expr, ret_span. ctxt ( ) , ".." , & mut applicability) ;
366
+ let snippet = match sugg {
367
+ Sugg :: BinOp ( ..) => sugg. maybe_par ( ) . to_string ( ) ,
368
+ _ => sugg. to_string ( ) ,
369
+ } ;
370
+
371
+ RetReplacement :: Expr ( snippet. into ( ) , applicability)
374
372
} else {
375
373
match match_ty_opt {
376
374
Some ( match_ty) => {
0 commit comments