@@ -2,7 +2,7 @@ use clippy_utils::diagnostics::span_lint_and_then;
2
2
use clippy_utils:: higher:: IfLetOrMatch ;
3
3
use clippy_utils:: msrvs:: { self , Msrv } ;
4
4
use clippy_utils:: peel_blocks;
5
- use clippy_utils:: source:: snippet_opt ;
5
+ use clippy_utils:: source:: snippet_with_context ;
6
6
use clippy_utils:: ty:: is_type_diagnostic_item;
7
7
use clippy_utils:: visitors:: { for_each_expr, Descend } ;
8
8
use if_chain:: if_chain;
@@ -141,20 +141,18 @@ fn emit_manual_let_else(cx: &LateContext<'_>, span: Span, expr: &Expr<'_>, pat:
141
141
// * unused binding collision detection with existing ones
142
142
// * putting patterns with at the top level | inside ()
143
143
// for this to be machine applicable.
144
- let app = Applicability :: HasPlaceholders ;
144
+ let mut app = Applicability :: HasPlaceholders ;
145
+ let ( sn_pat, _) = snippet_with_context ( cx, pat. span , span. ctxt ( ) , "" , & mut app) ;
146
+ let ( sn_expr, _) = snippet_with_context ( cx, expr. span , span. ctxt ( ) , "" , & mut app) ;
147
+ let ( sn_else, _) = snippet_with_context ( cx, else_body. span , span. ctxt ( ) , "" , & mut app) ;
145
148
146
- if let Some ( sn_pat) = snippet_opt ( cx, pat. span ) &&
147
- let Some ( sn_expr) = snippet_opt ( cx, expr. span ) &&
148
- let Some ( sn_else) = snippet_opt ( cx, else_body. span )
149
- {
150
- let else_bl = if matches ! ( else_body. kind, ExprKind :: Block ( ..) ) {
151
- sn_else
152
- } else {
153
- format ! ( "{{ {sn_else} }}" )
154
- } ;
155
- let sugg = format ! ( "let {sn_pat} = {sn_expr} else {else_bl};" ) ;
156
- diag. span_suggestion ( span, "consider writing" , sugg, app) ;
157
- }
149
+ let else_bl = if matches ! ( else_body. kind, ExprKind :: Block ( ..) ) {
150
+ sn_else. into_owned ( )
151
+ } else {
152
+ format ! ( "{{ {sn_else} }}" )
153
+ } ;
154
+ let sugg = format ! ( "let {sn_pat} = {sn_expr} else {else_bl};" ) ;
155
+ diag. span_suggestion ( span, "consider writing" , sugg, app) ;
158
156
} ,
159
157
) ;
160
158
}
0 commit comments