11use clippy_utils:: diagnostics:: span_lint_and_then;
22use clippy_utils:: msrvs:: { self , Msrv } ;
33use clippy_utils:: res:: { MaybeDef , MaybeResPath } ;
4- use clippy_utils:: source:: { IntoSpan , SpanExt } ;
4+ use clippy_utils:: source:: { FileRangeExt , SpanExt , StrExt } ;
55use clippy_utils:: ty:: get_field_by_name;
66use clippy_utils:: visitors:: { for_each_expr, for_each_expr_without_closures} ;
77use clippy_utils:: { ExprUseNode , expr_use_ctxt, sym} ;
@@ -100,18 +100,22 @@ pub(crate) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, arg: &Expr<'_>, name:
100100 let mut addr_of_edits = Vec :: with_capacity ( delayed. len ( ) ) ;
101101 for x in delayed {
102102 match x {
103- UseKind :: Return ( s) => edits. push ( ( s. with_leading_whitespace ( cx) . with_ctxt ( s. ctxt ( ) ) , String :: new ( ) ) ) ,
103+ UseKind :: Return ( s) => {
104+ if let Some ( sp) = s. map_range ( cx, |scx, range| range. with_leading_whitespace ( scx) ) {
105+ edits. push ( ( sp, String :: new ( ) ) ) ;
106+ } else {
107+ return ;
108+ }
109+ } ,
104110 UseKind :: Borrowed ( s) => {
105- let range = s. map_range ( cx, |_, src, range| {
106- let src = src. get ( range. clone ( ) ) ?;
107- let trimmed = src. trim_start_matches ( [ ' ' , '\t' , '\n' , '\r' , '(' ] ) ;
108- trimmed. starts_with ( '&' ) . then ( || {
109- let pos = range. start + src. len ( ) - trimmed. len ( ) ;
110- pos..pos + 1
111+ if let Some ( sp) = s. map_range ( cx, |scx, range| {
112+ range. map_range_text ( scx, |src| {
113+ src. trim_start_matches ( [ ' ' , '\t' , '\n' , '\r' , '(' ] )
114+ . split_prefix ( '&' )
115+ . map ( |[ x, _] | x)
111116 } )
112- } ) ;
113- if let Some ( range) = range {
114- addr_of_edits. push ( ( range. with_ctxt ( s. ctxt ( ) ) , String :: new ( ) ) ) ;
117+ } ) {
118+ addr_of_edits. push ( ( sp, String :: new ( ) ) ) ;
115119 } else {
116120 requires_copy = true ;
117121 requires_deref = true ;
@@ -162,6 +166,9 @@ pub(crate) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, arg: &Expr<'_>, name:
162166 && ( !requires_copy || cx. type_is_copy_modulo_regions ( arg_ty) )
163167 // This case could be handled, but a fair bit of care would need to be taken.
164168 && ( !requires_deref || arg_ty. is_freeze ( cx. tcx , cx. typing_env ( ) ) )
169+ && let Some ( final_expr_span) = final_expr
170+ . span
171+ . map_range ( cx, |scx, range| range. with_leading_whitespace ( scx) )
165172 {
166173 if requires_deref {
167174 edits. push ( ( param. span . shrink_to_lo ( ) , "&" . into ( ) ) ) ;
@@ -174,13 +181,7 @@ pub(crate) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, arg: &Expr<'_>, name:
174181 _ => return ,
175182 } ;
176183 edits. push ( ( name_span, edit. to_string ( ) ) ) ;
177- edits. push ( (
178- final_expr
179- . span
180- . with_leading_whitespace ( cx)
181- . with_ctxt ( final_expr. span . ctxt ( ) ) ,
182- String :: new ( ) ,
183- ) ) ;
184+ edits. push ( ( final_expr_span, String :: new ( ) ) ) ;
184185 let app = if edits. iter ( ) . any ( |( s, _) | s. from_expansion ( ) ) {
185186 Applicability :: MaybeIncorrect
186187 } else {
0 commit comments