@@ -2,11 +2,12 @@ use clippy_config::Conf;
22use clippy_utils:: diagnostics:: span_lint_and_sugg;
33use clippy_utils:: msrvs:: { self , Msrv } ;
44use clippy_utils:: source:: snippet_with_applicability;
5- use clippy_utils:: { SpanlessEq , is_in_const_context, is_integer_literal} ;
5+ use clippy_utils:: { SpanlessEq , is_in_const_context, is_integer_literal, sym } ;
66use rustc_errors:: Applicability ;
77use rustc_hir:: { BinOpKind , Expr , ExprKind , QPath , TyKind } ;
88use rustc_lint:: { LateContext , LateLintPass , LintContext } ;
99use rustc_session:: impl_lint_pass;
10+ use rustc_span:: Symbol ;
1011
1112declare_clippy_lint ! {
1213 /// ### What it does
@@ -98,7 +99,7 @@ impl LateLintPass<'_> for CheckedConversions {
9899struct Conversion < ' a > {
99100 cvt : ConversionType ,
100101 expr_to_cast : & ' a Expr < ' a > ,
101- to_type : Option < & ' a str > ,
102+ to_type : Option < Symbol > ,
102103}
103104
104105/// The kind of conversion that is checked
@@ -150,7 +151,7 @@ impl<'a> Conversion<'a> {
150151 }
151152
152153 /// Try to construct a new conversion if the conversion type is valid
153- fn try_new ( expr_to_cast : & ' a Expr < ' _ > , from_type : & str , to_type : & ' a str ) -> Option < Conversion < ' a > > {
154+ fn try_new ( expr_to_cast : & ' a Expr < ' _ > , from_type : Symbol , to_type : Symbol ) -> Option < Conversion < ' a > > {
154155 ConversionType :: try_new ( from_type, to_type) . map ( |cvt| Conversion {
155156 cvt,
156157 expr_to_cast,
@@ -171,7 +172,7 @@ impl<'a> Conversion<'a> {
171172impl ConversionType {
172173 /// Creates a conversion type if the type is allowed & conversion is valid
173174 #[ must_use]
174- fn try_new ( from : & str , to : & str ) -> Option < Self > {
175+ fn try_new ( from : Symbol , to : Symbol ) -> Option < Self > {
175176 if UINTS . contains ( & from) {
176177 Some ( Self :: FromUnsigned )
177178 } else if SINTS . contains ( & from) {
@@ -190,7 +191,7 @@ impl ConversionType {
190191
191192/// Check for `expr <= (to_type::MAX as from_type)`
192193fn check_upper_bound < ' tcx > ( lt : & ' tcx Expr < ' tcx > , gt : & ' tcx Expr < ' tcx > ) -> Option < Conversion < ' tcx > > {
193- if let Some ( ( from, to) ) = get_types_from_cast ( gt, INTS , " max_value" , " MAX" ) {
194+ if let Some ( ( from, to) ) = get_types_from_cast ( gt, INTS , sym :: max_value, sym :: MAX ) {
194195 Conversion :: try_new ( lt, from, to)
195196 } else {
196197 None
@@ -209,23 +210,23 @@ fn check_lower_bound_zero<'a>(candidate: &'a Expr<'_>, check: &'a Expr<'_>) -> O
209210
210211/// Check for `expr >= (to_type::MIN as from_type)`
211212fn check_lower_bound_min < ' a > ( candidate : & ' a Expr < ' _ > , check : & ' a Expr < ' _ > ) -> Option < Conversion < ' a > > {
212- if let Some ( ( from, to) ) = get_types_from_cast ( check, SINTS , " min_value" , " MIN" ) {
213+ if let Some ( ( from, to) ) = get_types_from_cast ( check, SINTS , sym :: min_value, sym :: MIN ) {
213214 Conversion :: try_new ( candidate, from, to)
214215 } else {
215216 None
216217 }
217218}
218219
219220/// Tries to extract the from- and to-type from a cast expression
220- fn get_types_from_cast < ' a > (
221- expr : & ' a Expr < ' _ > ,
222- types : & ' a [ & str ] ,
223- func : & ' a str ,
224- assoc_const : & ' a str ,
225- ) -> Option < ( & ' a str , & ' a str ) > {
221+ fn get_types_from_cast (
222+ expr : & Expr < ' _ > ,
223+ types : & [ Symbol ] ,
224+ func : Symbol ,
225+ assoc_const : Symbol ,
226+ ) -> Option < ( Symbol , Symbol ) > {
226227 // `to_type::max_value() as from_type`
227228 // or `to_type::MAX as from_type`
228- let call_from_cast: Option < ( & Expr < ' _ > , & str ) > = if let ExprKind :: Cast ( limit, from_type) = & expr. kind
229+ let call_from_cast: Option < ( & Expr < ' _ > , Symbol ) > = if let ExprKind :: Cast ( limit, from_type) = & expr. kind
229230 // to_type::max_value(), from_type
230231 && let TyKind :: Path ( from_type_path) = & from_type. kind
231232 && let Some ( from_sym) = int_ty_to_sym ( from_type_path)
@@ -236,12 +237,12 @@ fn get_types_from_cast<'a>(
236237 } ;
237238
238239 // `from_type::from(to_type::max_value())`
239- let limit_from: Option < ( & Expr < ' _ > , & str ) > = call_from_cast. or_else ( || {
240+ let limit_from: Option < ( & Expr < ' _ > , Symbol ) > = call_from_cast. or_else ( || {
240241 if let ExprKind :: Call ( from_func, [ limit] ) = & expr. kind
241242 // `from_type::from, to_type::max_value()`
242243 // `from_type::from`
243244 && let ExprKind :: Path ( path) = & from_func. kind
244- && let Some ( from_sym) = get_implementing_type ( path, INTS , " from" )
245+ && let Some ( from_sym) = get_implementing_type ( path, INTS , sym :: from)
245246 {
246247 Some ( ( limit, from_sym) )
247248 } else {
@@ -273,32 +274,41 @@ fn get_types_from_cast<'a>(
273274}
274275
275276/// Gets the type which implements the called function
276- fn get_implementing_type < ' a > ( path : & QPath < ' _ > , candidates : & ' a [ & str ] , function : & str ) -> Option < & ' a str > {
277+ fn get_implementing_type ( path : & QPath < ' _ > , candidates : & [ Symbol ] , function : Symbol ) -> Option < Symbol > {
277278 if let QPath :: TypeRelative ( ty, path) = & path
278- && path. ident . name . as_str ( ) == function
279+ && path. ident . name == function
279280 && let TyKind :: Path ( QPath :: Resolved ( None , tp) ) = & ty. kind
280281 && let [ int] = tp. segments
281282 {
282- let name = int. ident . name . as_str ( ) ;
283- candidates. iter ( ) . find ( |c| & name == * c) . copied ( )
283+ candidates. iter ( ) . find ( |c| int. ident . name == * * c) . copied ( )
284284 } else {
285285 None
286286 }
287287}
288288
289289/// Gets the type as a string, if it is a supported integer
290- fn int_ty_to_sym < ' tcx > ( path : & QPath < ' _ > ) -> Option < & ' tcx str > {
290+ fn int_ty_to_sym ( path : & QPath < ' _ > ) -> Option < Symbol > {
291291 if let QPath :: Resolved ( _, path) = * path
292292 && let [ ty] = path. segments
293293 {
294- let name = ty. ident . name . as_str ( ) ;
295- INTS . iter ( ) . find ( |c| & name == * c) . copied ( )
294+ INTS . iter ( ) . find ( |c| ty. ident . name == * * c) . copied ( )
296295 } else {
297296 None
298297 }
299298}
300299
301300// Constants
302- const UINTS : & [ & str ] = & [ "u8" , "u16" , "u32" , "u64" , "usize" ] ;
303- const SINTS : & [ & str ] = & [ "i8" , "i16" , "i32" , "i64" , "isize" ] ;
304- const INTS : & [ & str ] = & [ "u8" , "u16" , "u32" , "u64" , "usize" , "i8" , "i16" , "i32" , "i64" , "isize" ] ;
301+ const UINTS : & [ Symbol ] = & [ sym:: u8, sym:: u16, sym:: u32, sym:: u64, sym:: usize] ;
302+ const SINTS : & [ Symbol ] = & [ sym:: i8, sym:: i16, sym:: i32, sym:: i64, sym:: isize] ;
303+ const INTS : & [ Symbol ] = & [
304+ sym:: u8,
305+ sym:: u16,
306+ sym:: u32,
307+ sym:: u64,
308+ sym:: usize,
309+ sym:: i8,
310+ sym:: i16,
311+ sym:: i32,
312+ sym:: i64,
313+ sym:: isize,
314+ ] ;
0 commit comments