11use clippy_utils:: diagnostics:: span_lint_and_sugg;
22use clippy_utils:: numeric_literal:: NumericLiteral ;
33use clippy_utils:: res:: MaybeResPath ;
4- use clippy_utils:: source:: { SpanExt , snippet_opt } ;
4+ use clippy_utils:: source:: SpanExt ;
55use clippy_utils:: visitors:: { Visitable , for_each_expr_without_closures} ;
66use clippy_utils:: { get_parent_expr, is_hir_ty_cfg_dependant, is_ty_alias} ;
77use rustc_ast:: { LitFloatType , LitIntType , LitKind } ;
@@ -24,7 +24,10 @@ pub(super) fn check<'tcx>(
2424 cast_from : Ty < ' tcx > ,
2525 cast_to : Ty < ' tcx > ,
2626) -> bool {
27- let cast_str = snippet_opt ( cx, cast_expr. span ) . unwrap_or_default ( ) ;
27+ // FIXME: Delay this to where it's needed.
28+ let Some ( src) = cast_expr. span . get_text ( cx) else {
29+ return false ;
30+ } ;
2831
2932 if let ty:: RawPtr ( ..) = cast_from. kind ( )
3033 // check both mutability and type are the same
@@ -57,7 +60,7 @@ pub(super) fn check<'tcx>(
5760 "casting raw pointers to the same type and constness is unnecessary (`{cast_from}` -> `{cast_to}`)"
5861 ) ,
5962 "try" ,
60- cast_str . clone ( ) ,
63+ src . to_owned ( ) ,
6164 Applicability :: MaybeIncorrect ,
6265 ) ;
6366 }
@@ -103,10 +106,7 @@ pub(super) fn check<'tcx>(
103106 }
104107
105108 if let Some ( lit) = get_numeric_literal ( cast_expr) {
106- let literal_str = & cast_str;
107-
108109 if let LitKind :: Int ( n, _) = lit. node
109- && let Some ( src) = cast_expr. span . get_text ( cx)
110110 && cast_to. is_floating_point ( )
111111 && let Some ( num_lit) = NumericLiteral :: from_lit_kind ( & src, & lit. node )
112112 && let from_nbits = 128 - n. get ( ) . leading_zeros ( )
@@ -122,20 +122,18 @@ pub(super) fn check<'tcx>(
122122
123123 match lit. node {
124124 LitKind :: Int ( _, LitIntType :: Unsuffixed ) if cast_to. is_integral ( ) => {
125- lint_unnecessary_cast ( cx, expr, literal_str , cast_from, cast_to) ;
125+ lint_unnecessary_cast ( cx, expr, & src , cast_from, cast_to) ;
126126 return false ;
127127 } ,
128128 LitKind :: Float ( _, LitFloatType :: Unsuffixed ) if cast_to. is_floating_point ( ) => {
129- lint_unnecessary_cast ( cx, expr, literal_str , cast_from, cast_to) ;
129+ lint_unnecessary_cast ( cx, expr, & src , cast_from, cast_to) ;
130130 return false ;
131131 } ,
132132 LitKind :: Int ( _, LitIntType :: Signed ( _) | LitIntType :: Unsigned ( _) )
133133 | LitKind :: Float ( _, LitFloatType :: Suffixed ( _) )
134134 if cast_from. kind ( ) == cast_to. kind ( ) =>
135135 {
136- if let Some ( src) = cast_expr. span . get_text ( cx)
137- && let Some ( num_lit) = NumericLiteral :: from_lit_kind ( & src, & lit. node )
138- {
136+ if let Some ( num_lit) = NumericLiteral :: from_lit_kind ( & src, & lit. node ) {
139137 lint_unnecessary_cast ( cx, expr, num_lit. integer , cast_from, cast_to) ;
140138 return true ;
141139 }
@@ -197,9 +195,9 @@ pub(super) fn check<'tcx>(
197195 format ! ( "casting to the same type is unnecessary (`{cast_from}` -> `{cast_to}`)" ) ,
198196 "try" ,
199197 match surrounding {
200- MaybeParenOrBlock :: Paren => format ! ( "({cast_str })" ) ,
201- MaybeParenOrBlock :: Block => format ! ( "{{ {cast_str } }}" ) ,
202- MaybeParenOrBlock :: Nothing => cast_str ,
198+ MaybeParenOrBlock :: Paren => format ! ( "({src })" ) ,
199+ MaybeParenOrBlock :: Block => format ! ( "{{ {src } }}" ) ,
200+ MaybeParenOrBlock :: Nothing => src . to_owned ( ) ,
203201 } ,
204202 Applicability :: MachineApplicable ,
205203 ) ;
0 commit comments