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, sym} ;
@@ -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,18 +98,25 @@ 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_range ( cx, SourceFileRange :: add_leading_whitespace) {
103
+ edits. push ( ( s, String :: new ( ) ) ) ;
104
+ } else {
105
+ return ;
106
+ }
107
+ } ,
102
108
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
+ } )
109
117
} )
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 ( ) ) ) ;
113
120
} else {
114
121
requires_copy = true ;
115
122
requires_deref = true ;
@@ -156,10 +163,10 @@ pub(crate) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, arg: &Expr<'_>, name:
156
163
}
157
164
}
158
165
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) )
161
167
// This case could be handled, but a fair bit of care would need to be taken.
162
168
&& ( !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)
163
170
{
164
171
if requires_deref {
165
172
edits. push ( ( param. span . shrink_to_lo ( ) , "&" . into ( ) ) ) ;
@@ -172,13 +179,7 @@ pub(crate) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, arg: &Expr<'_>, name:
172
179
_ => return ,
173
180
} ;
174
181
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 ( ) ) ) ;
182
183
let app = if edits. iter ( ) . any ( |( s, _) | s. from_expansion ( ) ) {
183
184
Applicability :: MaybeIncorrect
184
185
} else {
0 commit comments