@@ -2,11 +2,12 @@ use clippy_config::Conf;
2
2
use clippy_utils:: diagnostics:: span_lint_and_sugg;
3
3
use clippy_utils:: msrvs:: { self , Msrv } ;
4
4
use 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 } ;
6
6
use rustc_errors:: Applicability ;
7
7
use rustc_hir:: { BinOpKind , Expr , ExprKind , QPath , TyKind } ;
8
8
use rustc_lint:: { LateContext , LateLintPass , LintContext } ;
9
9
use rustc_session:: impl_lint_pass;
10
+ use rustc_span:: Symbol ;
10
11
11
12
declare_clippy_lint ! {
12
13
/// ### What it does
@@ -98,7 +99,7 @@ impl LateLintPass<'_> for CheckedConversions {
98
99
struct Conversion < ' a > {
99
100
cvt : ConversionType ,
100
101
expr_to_cast : & ' a Expr < ' a > ,
101
- to_type : Option < & ' a str > ,
102
+ to_type : Option < Symbol > ,
102
103
}
103
104
104
105
/// The kind of conversion that is checked
@@ -150,7 +151,7 @@ impl<'a> Conversion<'a> {
150
151
}
151
152
152
153
/// 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 > > {
154
155
ConversionType :: try_new ( from_type, to_type) . map ( |cvt| Conversion {
155
156
cvt,
156
157
expr_to_cast,
@@ -171,7 +172,7 @@ impl<'a> Conversion<'a> {
171
172
impl ConversionType {
172
173
/// Creates a conversion type if the type is allowed & conversion is valid
173
174
#[ must_use]
174
- fn try_new ( from : & str , to : & str ) -> Option < Self > {
175
+ fn try_new ( from : Symbol , to : Symbol ) -> Option < Self > {
175
176
if UINTS . contains ( & from) {
176
177
Some ( Self :: FromUnsigned )
177
178
} else if SINTS . contains ( & from) {
@@ -190,7 +191,7 @@ impl ConversionType {
190
191
191
192
/// Check for `expr <= (to_type::MAX as from_type)`
192
193
fn 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 ) {
194
195
Conversion :: try_new ( lt, from, to)
195
196
} else {
196
197
None
@@ -209,23 +210,23 @@ fn check_lower_bound_zero<'a>(candidate: &'a Expr<'_>, check: &'a Expr<'_>) -> O
209
210
210
211
/// Check for `expr >= (to_type::MIN as from_type)`
211
212
fn 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 ) {
213
214
Conversion :: try_new ( candidate, from, to)
214
215
} else {
215
216
None
216
217
}
217
218
}
218
219
219
220
/// 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 ) > {
226
227
// `to_type::max_value() as from_type`
227
228
// 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
229
230
// to_type::max_value(), from_type
230
231
&& let TyKind :: Path ( from_type_path) = & from_type. kind
231
232
&& let Some ( from_sym) = int_ty_to_sym ( from_type_path)
@@ -236,12 +237,12 @@ fn get_types_from_cast<'a>(
236
237
} ;
237
238
238
239
// `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 ( || {
240
241
if let ExprKind :: Call ( from_func, [ limit] ) = & expr. kind
241
242
// `from_type::from, to_type::max_value()`
242
243
// `from_type::from`
243
244
&& 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)
245
246
{
246
247
Some ( ( limit, from_sym) )
247
248
} else {
@@ -273,32 +274,41 @@ fn get_types_from_cast<'a>(
273
274
}
274
275
275
276
/// 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 > {
277
278
if let QPath :: TypeRelative ( ty, path) = & path
278
- && path. ident . name . as_str ( ) == function
279
+ && path. ident . name == function
279
280
&& let TyKind :: Path ( QPath :: Resolved ( None , tp) ) = & ty. kind
280
281
&& let [ int] = tp. segments
281
282
{
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 ( )
284
284
} else {
285
285
None
286
286
}
287
287
}
288
288
289
289
/// 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 > {
291
291
if let QPath :: Resolved ( _, path) = * path
292
292
&& let [ ty] = path. segments
293
293
{
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 ( )
296
295
} else {
297
296
None
298
297
}
299
298
}
300
299
301
300
// 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