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:: { SourceFileRange , SpanExt } ;
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} ;
@@ -91,7 +91,7 @@ pub(crate) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, arg: &Expr<'_>, name:
9191 } )
9292 . is_none ( ) ;
9393
94- if ret_count != 0 {
94+ if !can_lint || ret_count != 0 {
9595 // A return expression that didn't return the original value was found.
9696 return ;
9797 }
@@ -100,18 +100,25 @@ 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 ( s) = s. map_range ( cx, SourceFileRange :: add_leading_whitespace) {
105+ edits. push ( ( s, 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 ( s) = s. map_range ( cx, |range| {
112+ range. edit_range ( |src, range| {
113+ let src = src. get ( range. clone ( ) ) ?;
114+ let trimmed = src. trim_start_matches ( [ ' ' , '\t' , '\n' , '\r' , '(' ] ) ;
115+ trimmed. starts_with ( '&' ) . then ( || {
116+ let pos = range. start + src. len ( ) - trimmed. len ( ) ;
117+ pos..pos + 1
118+ } )
111119 } )
112- } ) ;
113- if let Some ( range) = range {
114- addr_of_edits. push ( ( range. with_ctxt ( s. ctxt ( ) ) , String :: new ( ) ) ) ;
120+ } ) {
121+ addr_of_edits. push ( ( s, String :: new ( ) ) ) ;
115122 } else {
116123 requires_copy = true ;
117124 requires_deref = true ;
@@ -158,10 +165,10 @@ pub(crate) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, arg: &Expr<'_>, name:
158165 }
159166 }
160167
161- if can_lint
162- && ( !requires_copy || cx. type_is_copy_modulo_regions ( arg_ty) )
168+ if ( !requires_copy || cx. type_is_copy_modulo_regions ( arg_ty) )
163169 // This case could be handled, but a fair bit of care would need to be taken.
164170 && ( !requires_deref || arg_ty. is_freeze ( cx. tcx , cx. typing_env ( ) ) )
171+ && let Some ( final_expr_span) = final_expr. span . map_range ( cx, SourceFileRange :: add_leading_whitespace)
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