11use clippy_utils:: diagnostics:: span_lint_hir_and_then;
22use clippy_utils:: res:: MaybeResPath ;
3- use clippy_utils:: source:: snippet;
3+ use clippy_utils:: source:: { snippet, snippet_with_context } ;
44use clippy_utils:: visitors:: is_local_used;
55use rustc_errors:: Applicability ;
66use rustc_hir as hir;
@@ -117,14 +117,16 @@ fn check_block_inner<'tcx>(cx: &LateContext<'tcx>, stmt: &'tcx hir::Stmt<'tcx>,
117117 // FIXME: this should not suggest `mut` if we can detect that the variable is not
118118 // use mutably after the `if`
119119
120+ let mut applicability = Applicability :: HasPlaceholders ;
121+ let ( cond_snip, _) = snippet_with_context ( cx, cond. span , if_. span . ctxt ( ) , "_" , & mut applicability) ;
122+ let ( value_snip, _) = snippet_with_context ( cx, value. span , if_. span . ctxt ( ) , "<value>" , & mut applicability) ;
123+ let ( default_snip, _) =
124+ snippet_with_context ( cx, default. span , if_. span . ctxt ( ) , "<default>" , & mut applicability) ;
120125 let sug = format ! (
121- "let {mutability}{name} = if {cond } {{{then} {value } }} else {{{else} {default } }};" ,
126+ "let {mutability}{name} = if {cond_snip } {{{then} {value_snip } }} else {{{else} {default_snip } }};" ,
122127 name=ident. name,
123- cond=snippet( cx, cond. span, "_" ) ,
124128 then=if then. stmts. len( ) > 1 { " ..;" } else { "" } ,
125129 else=if default_multi_stmts { " ..;" } else { "" } ,
126- value=snippet( cx, value. span, "<value>" ) ,
127- default =snippet( cx, default . span, "<default>" ) ,
128130 ) ;
129131 span_lint_hir_and_then (
130132 cx,
@@ -133,12 +135,7 @@ fn check_block_inner<'tcx>(cx: &LateContext<'tcx>, stmt: &'tcx hir::Stmt<'tcx>,
133135 span,
134136 "`if _ { .. } else { .. }` is an expression" ,
135137 |diag| {
136- diag. span_suggestion (
137- span,
138- "it is more idiomatic to write" ,
139- sug,
140- Applicability :: HasPlaceholders ,
141- ) ;
138+ diag. span_suggestion ( span, "it is more idiomatic to write" , sug, applicability) ;
142139 if !mutability. is_empty ( ) {
143140 diag. note ( "you might not need `mut` at all" ) ;
144141 }
0 commit comments