11use clippy_utils:: diagnostics:: span_lint_and_then;
22use clippy_utils:: msrvs:: { self , Msrv } ;
3- use clippy_utils:: source:: { IntoSpan , SpanExt } ;
3+ use clippy_utils:: source:: { SourceFileRange , SpanExt } ;
44use clippy_utils:: ty:: get_field_by_name;
55use clippy_utils:: visitors:: { for_each_expr, for_each_expr_without_closures} ;
66use clippy_utils:: { ExprUseNode , expr_use_ctxt, is_diag_item_method, is_diag_trait_item, path_to_local_id} ;
@@ -89,7 +89,7 @@ pub(crate) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, arg: &Expr<'_>, name:
8989 } )
9090 . is_none ( ) ;
9191
92- if ret_count != 0 {
92+ if !can_lint || ret_count != 0 {
9393 // A return expression that didn't return the original value was found.
9494 return ;
9595 }
@@ -98,19 +98,26 @@ pub(crate) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, arg: &Expr<'_>, name:
9898 let mut addr_of_edits = Vec :: with_capacity ( delayed. len ( ) ) ;
9999 for x in delayed {
100100 match x {
101- UseKind :: Return ( s) => edits. push ( ( s. with_leading_whitespace ( cx) . with_ctxt ( s. ctxt ( ) ) , String :: new ( ) ) ) ,
101+ UseKind :: Return ( s) => {
102+ if let Some ( s) = s. map_span ( cx, SourceFileRange :: with_leading_whitespace) {
103+ edits. push ( ( s, String :: new ( ) ) ) ;
104+ } else {
105+ return ;
106+ }
107+ } ,
102108 UseKind :: Borrowed ( s) => {
103109 #[ expect( clippy:: range_plus_one) ]
104- let range = s. map_range ( cx, |src, range| {
105- let src = src. get ( range. clone ( ) ) ?;
106- let trimmed = src. trim_start_matches ( [ ' ' , '\t' , '\n' , '\r' , '(' ] ) ;
107- trimmed. starts_with ( '&' ) . then ( || {
108- let pos = range. start + src. len ( ) - trimmed. len ( ) ;
109- pos..pos + 1
110+ if let Some ( s) = s. map_span ( cx, |file| {
111+ file. map_range ( |_, src, range| {
112+ let src = src. get ( range. clone ( ) ) ?;
113+ let trimmed = src. trim_start_matches ( [ ' ' , '\t' , '\n' , '\r' , '(' ] ) ;
114+ trimmed. starts_with ( '&' ) . then ( || {
115+ let pos = range. start + src. len ( ) - trimmed. len ( ) ;
116+ pos..pos + 1
117+ } )
110118 } )
111- } ) ;
112- if let Some ( range) = range {
113- addr_of_edits. push ( ( range. with_ctxt ( s. ctxt ( ) ) , String :: new ( ) ) ) ;
119+ } ) {
120+ addr_of_edits. push ( ( s, String :: new ( ) ) ) ;
114121 } else {
115122 requires_copy = true ;
116123 requires_deref = true ;
@@ -157,10 +164,10 @@ pub(crate) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, arg: &Expr<'_>, name:
157164 }
158165 }
159166
160- if can_lint
161- && ( !requires_copy || cx. type_is_copy_modulo_regions ( arg_ty) )
167+ if ( !requires_copy || cx. type_is_copy_modulo_regions ( arg_ty) )
162168 // This case could be handled, but a fair bit of care would need to be taken.
163169 && ( !requires_deref || arg_ty. is_freeze ( cx. tcx , cx. typing_env ( ) ) )
170+ && let Some ( final_expr_span) = final_expr. span . map_span ( cx, SourceFileRange :: with_leading_whitespace)
164171 {
165172 if requires_deref {
166173 edits. push ( ( param. span . shrink_to_lo ( ) , "&" . into ( ) ) ) ;
@@ -173,13 +180,7 @@ pub(crate) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, arg: &Expr<'_>, name:
173180 _ => return ,
174181 } ;
175182 edits. push ( ( name_span, edit. to_string ( ) ) ) ;
176- edits. push ( (
177- final_expr
178- . span
179- . with_leading_whitespace ( cx)
180- . with_ctxt ( final_expr. span . ctxt ( ) ) ,
181- String :: new ( ) ,
182- ) ) ;
183+ edits. push ( ( final_expr_span, String :: new ( ) ) ) ;
183184 let app = if edits. iter ( ) . any ( |( s, _) | s. from_expansion ( ) ) {
184185 Applicability :: MaybeIncorrect
185186 } else {
0 commit comments