1
1
use clippy_utils:: diagnostics:: span_lint_and_then;
2
2
use clippy_utils:: msrvs:: { self , Msrv } ;
3
- use clippy_utils:: source:: { IntoSpan , SpanExt } ;
3
+ use clippy_utils:: source:: { SourceFileRange , SpanExt } ;
4
4
use clippy_utils:: ty:: get_field_by_name;
5
5
use clippy_utils:: visitors:: { for_each_expr, for_each_expr_without_closures} ;
6
6
use 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:
89
89
} )
90
90
. is_none ( ) ;
91
91
92
- if ret_count != 0 {
92
+ if !can_lint || ret_count != 0 {
93
93
// A return expression that didn't return the original value was found.
94
94
return ;
95
95
}
@@ -98,19 +98,26 @@ pub(crate) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, arg: &Expr<'_>, name:
98
98
let mut addr_of_edits = Vec :: with_capacity ( delayed. len ( ) ) ;
99
99
for x in delayed {
100
100
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
+ } ,
102
108
UseKind :: Borrowed ( s) => {
103
109
#[ 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
+ } )
110
118
} )
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 ( ) ) ) ;
114
121
} else {
115
122
requires_copy = true ;
116
123
requires_deref = true ;
@@ -157,10 +164,10 @@ pub(crate) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, arg: &Expr<'_>, name:
157
164
}
158
165
}
159
166
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) )
162
168
// This case could be handled, but a fair bit of care would need to be taken.
163
169
&& ( !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)
164
171
{
165
172
if requires_deref {
166
173
edits. push ( ( param. span . shrink_to_lo ( ) , "&" . into ( ) ) ) ;
@@ -173,13 +180,7 @@ pub(crate) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, arg: &Expr<'_>, name:
173
180
_ => return ,
174
181
} ;
175
182
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 ( ) ) ) ;
183
184
let app = if edits. iter ( ) . any ( |( s, _) | s. from_expansion ( ) ) {
184
185
Applicability :: MaybeIncorrect
185
186
} else {
0 commit comments