1
1
use clippy_utils:: diagnostics:: span_lint_and_sugg;
2
2
use clippy_utils:: numeric_literal:: NumericLiteral ;
3
- use clippy_utils:: source:: { SpanExt , snippet_opt } ;
3
+ use clippy_utils:: source:: SpanExt ;
4
4
use clippy_utils:: visitors:: { Visitable , for_each_expr_without_closures} ;
5
5
use clippy_utils:: { get_parent_expr, is_hir_ty_cfg_dependant, is_ty_alias, path_to_local} ;
6
6
use rustc_ast:: { LitFloatType , LitIntType , LitKind } ;
@@ -23,7 +23,10 @@ pub(super) fn check<'tcx>(
23
23
cast_from : Ty < ' tcx > ,
24
24
cast_to : Ty < ' tcx > ,
25
25
) -> bool {
26
- let cast_str = snippet_opt ( cx, cast_expr. span ) . unwrap_or_default ( ) ;
26
+ // FIXME: Delay this to where it's needed.
27
+ let Some ( src) = cast_expr. span . get_source_text ( cx) else {
28
+ return false ;
29
+ } ;
27
30
28
31
if let ty:: RawPtr ( ..) = cast_from. kind ( )
29
32
// check both mutability and type are the same
@@ -56,7 +59,7 @@ pub(super) fn check<'tcx>(
56
59
"casting raw pointers to the same type and constness is unnecessary (`{cast_from}` -> `{cast_to}`)"
57
60
) ,
58
61
"try" ,
59
- cast_str . clone ( ) ,
62
+ src . to_owned ( ) ,
60
63
Applicability :: MaybeIncorrect ,
61
64
) ;
62
65
}
@@ -102,10 +105,7 @@ pub(super) fn check<'tcx>(
102
105
}
103
106
104
107
if let Some ( lit) = get_numeric_literal ( cast_expr) {
105
- let literal_str = & cast_str;
106
-
107
108
if let LitKind :: Int ( n, _) = lit. node
108
- && let Some ( src) = cast_expr. span . get_source_text ( cx)
109
109
&& cast_to. is_floating_point ( )
110
110
&& let Some ( num_lit) = NumericLiteral :: from_lit_kind ( & src, & lit. node )
111
111
&& let from_nbits = 128 - n. get ( ) . leading_zeros ( )
@@ -121,20 +121,18 @@ pub(super) fn check<'tcx>(
121
121
122
122
match lit. node {
123
123
LitKind :: Int ( _, LitIntType :: Unsuffixed ) if cast_to. is_integral ( ) => {
124
- lint_unnecessary_cast ( cx, expr, literal_str , cast_from, cast_to) ;
124
+ lint_unnecessary_cast ( cx, expr, & src , cast_from, cast_to) ;
125
125
return false ;
126
126
} ,
127
127
LitKind :: Float ( _, LitFloatType :: Unsuffixed ) if cast_to. is_floating_point ( ) => {
128
- lint_unnecessary_cast ( cx, expr, literal_str , cast_from, cast_to) ;
128
+ lint_unnecessary_cast ( cx, expr, & src , cast_from, cast_to) ;
129
129
return false ;
130
130
} ,
131
131
LitKind :: Int ( _, LitIntType :: Signed ( _) | LitIntType :: Unsigned ( _) )
132
132
| LitKind :: Float ( _, LitFloatType :: Suffixed ( _) )
133
133
if cast_from. kind ( ) == cast_to. kind ( ) =>
134
134
{
135
- if let Some ( src) = cast_expr. span . get_source_text ( cx)
136
- && let Some ( num_lit) = NumericLiteral :: from_lit_kind ( & src, & lit. node )
137
- {
135
+ if let Some ( num_lit) = NumericLiteral :: from_lit_kind ( & src, & lit. node ) {
138
136
lint_unnecessary_cast ( cx, expr, num_lit. integer , cast_from, cast_to) ;
139
137
return true ;
140
138
}
@@ -196,9 +194,9 @@ pub(super) fn check<'tcx>(
196
194
format ! ( "casting to the same type is unnecessary (`{cast_from}` -> `{cast_to}`)" ) ,
197
195
"try" ,
198
196
match surrounding {
199
- MaybeParenOrBlock :: Paren => format ! ( "({cast_str })" ) ,
200
- MaybeParenOrBlock :: Block => format ! ( "{{ {cast_str } }}" ) ,
201
- MaybeParenOrBlock :: Nothing => cast_str ,
197
+ MaybeParenOrBlock :: Paren => format ! ( "({src })" ) ,
198
+ MaybeParenOrBlock :: Block => format ! ( "{{ {src } }}" ) ,
199
+ MaybeParenOrBlock :: Nothing => src . to_owned ( ) ,
202
200
} ,
203
201
Applicability :: MachineApplicable ,
204
202
) ;
0 commit comments