11use clippy_utils:: diagnostics:: span_lint_and_sugg;
22use clippy_utils:: numeric_literal:: NumericLiteral ;
3- use clippy_utils:: source:: { SpanExt , snippet_opt } ;
3+ use clippy_utils:: source:: SpanExt ;
44use clippy_utils:: visitors:: { Visitable , for_each_expr_without_closures} ;
55use clippy_utils:: { get_parent_expr, is_hir_ty_cfg_dependant, is_ty_alias, path_to_local} ;
66use rustc_ast:: { LitFloatType , LitIntType , LitKind } ;
@@ -23,7 +23,10 @@ pub(super) fn check<'tcx>(
2323 cast_from : Ty < ' tcx > ,
2424 cast_to : Ty < ' tcx > ,
2525) -> 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+ } ;
2730
2831 if let ty:: RawPtr ( ..) = cast_from. kind ( )
2932 // check both mutability and type are the same
@@ -56,7 +59,7 @@ pub(super) fn check<'tcx>(
5659 "casting raw pointers to the same type and constness is unnecessary (`{cast_from}` -> `{cast_to}`)"
5760 ) ,
5861 "try" ,
59- cast_str . clone ( ) ,
62+ src . to_owned ( ) ,
6063 Applicability :: MaybeIncorrect ,
6164 ) ;
6265 }
@@ -102,10 +105,7 @@ pub(super) fn check<'tcx>(
102105 }
103106
104107 if let Some ( lit) = get_numeric_literal ( cast_expr) {
105- let literal_str = & cast_str;
106-
107108 if let LitKind :: Int ( n, _) = lit. node
108- && let Some ( src) = cast_expr. span . get_source_text ( cx)
109109 && cast_to. is_floating_point ( )
110110 && let Some ( num_lit) = NumericLiteral :: from_lit_kind ( & src, & lit. node )
111111 && let from_nbits = 128 - n. get ( ) . leading_zeros ( )
@@ -121,20 +121,18 @@ pub(super) fn check<'tcx>(
121121
122122 match lit. node {
123123 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) ;
125125 return false ;
126126 } ,
127127 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) ;
129129 return false ;
130130 } ,
131131 LitKind :: Int ( _, LitIntType :: Signed ( _) | LitIntType :: Unsigned ( _) )
132132 | LitKind :: Float ( _, LitFloatType :: Suffixed ( _) )
133133 if cast_from. kind ( ) == cast_to. kind ( ) =>
134134 {
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 ) {
138136 lint_unnecessary_cast ( cx, expr, num_lit. integer , cast_from, cast_to) ;
139137 return true ;
140138 }
@@ -196,9 +194,9 @@ pub(super) fn check<'tcx>(
196194 format ! ( "casting to the same type is unnecessary (`{cast_from}` -> `{cast_to}`)" ) ,
197195 "try" ,
198196 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 ( ) ,
202200 } ,
203201 Applicability :: MachineApplicable ,
204202 ) ;
0 commit comments