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, sym} ;
@@ -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,18 +98,25 @@ 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_range ( cx, SourceFileRange :: add_leading_whitespace) {
103+ edits. push ( ( s, String :: new ( ) ) ) ;
104+ } else {
105+ return ;
106+ }
107+ } ,
102108 UseKind :: Borrowed ( s) => {
103- let range = s. map_range ( cx, |_, src, range| {
104- let src = src. get ( range. clone ( ) ) ?;
105- let trimmed = src. trim_start_matches ( [ ' ' , '\t' , '\n' , '\r' , '(' ] ) ;
106- trimmed. starts_with ( '&' ) . then ( || {
107- let pos = range. start + src. len ( ) - trimmed. len ( ) ;
108- pos..pos + 1
109+ if let Some ( s) = s. map_range ( cx, |range| {
110+ range. edit_range ( |src, range| {
111+ let src = src. get ( range. clone ( ) ) ?;
112+ let trimmed = src. trim_start_matches ( [ ' ' , '\t' , '\n' , '\r' , '(' ] ) ;
113+ trimmed. starts_with ( '&' ) . then ( || {
114+ let pos = range. start + src. len ( ) - trimmed. len ( ) ;
115+ pos..pos + 1
116+ } )
109117 } )
110- } ) ;
111- if let Some ( range) = range {
112- addr_of_edits. push ( ( range. with_ctxt ( s. ctxt ( ) ) , String :: new ( ) ) ) ;
118+ } ) {
119+ addr_of_edits. push ( ( s, String :: new ( ) ) ) ;
113120 } else {
114121 requires_copy = true ;
115122 requires_deref = true ;
@@ -156,10 +163,10 @@ pub(crate) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, arg: &Expr<'_>, name:
156163 }
157164 }
158165
159- if can_lint
160- && ( !requires_copy || cx. type_is_copy_modulo_regions ( arg_ty) )
166+ if ( !requires_copy || cx. type_is_copy_modulo_regions ( arg_ty) )
161167 // This case could be handled, but a fair bit of care would need to be taken.
162168 && ( !requires_deref || arg_ty. is_freeze ( cx. tcx , cx. typing_env ( ) ) )
169+ && let Some ( final_expr_span) = final_expr. span . map_range ( cx, SourceFileRange :: add_leading_whitespace)
163170 {
164171 if requires_deref {
165172 edits. push ( ( param. span . shrink_to_lo ( ) , "&" . into ( ) ) ) ;
@@ -172,13 +179,7 @@ pub(crate) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, arg: &Expr<'_>, name:
172179 _ => return ,
173180 } ;
174181 edits. push ( ( name_span, edit. to_string ( ) ) ) ;
175- edits. push ( (
176- final_expr
177- . span
178- . with_leading_whitespace ( cx)
179- . with_ctxt ( final_expr. span . ctxt ( ) ) ,
180- String :: new ( ) ,
181- ) ) ;
182+ edits. push ( ( final_expr_span, String :: new ( ) ) ) ;
182183 let app = if edits. iter ( ) . any ( |( s, _) | s. from_expansion ( ) ) {
183184 Applicability :: MaybeIncorrect
184185 } else {
0 commit comments