1
1
mod cast_lossless;
2
2
mod cast_possible_truncation;
3
+ mod cast_possible_wrap;
3
4
mod cast_precision_loss;
4
5
mod cast_sign_loss;
5
6
mod utils;
@@ -20,8 +21,8 @@ use rustc_target::abi::LayoutOf;
20
21
21
22
use crate :: utils:: sugg:: Sugg ;
22
23
use crate :: utils:: {
23
- is_hir_ty_cfg_dependant, is_isize_or_usize , meets_msrv, numeric_literal:: NumericLiteral , snippet_opt,
24
- snippet_with_applicability , span_lint, span_lint_and_sugg, span_lint_and_then,
24
+ is_hir_ty_cfg_dependant, meets_msrv, numeric_literal:: NumericLiteral , snippet_opt, snippet_with_applicability ,
25
+ span_lint, span_lint_and_sugg, span_lint_and_then,
25
26
} ;
26
27
27
28
use utils:: int_ty_to_nbits;
@@ -255,49 +256,6 @@ declare_clippy_lint! {
255
256
"casting a function pointer to a numeric type not wide enough to store the address"
256
257
}
257
258
258
- enum ArchSuffix {
259
- _32,
260
- _64,
261
- None ,
262
- }
263
-
264
- fn check_truncation_and_wrapping ( cx : & LateContext < ' _ > , expr : & Expr < ' _ > , cast_from : Ty < ' _ > , cast_to : Ty < ' _ > ) {
265
- let arch_64_suffix = " on targets with 64-bit wide pointers" ;
266
- let arch_32_suffix = " on targets with 32-bit wide pointers" ;
267
- let cast_unsigned_to_signed = !cast_from. is_signed ( ) && cast_to. is_signed ( ) ;
268
- let from_nbits = int_ty_to_nbits ( cast_from, cx. tcx ) ;
269
- let to_nbits = int_ty_to_nbits ( cast_to, cx. tcx ) ;
270
- let ( span_wrap, suffix_wrap) = match ( is_isize_or_usize ( cast_from) , is_isize_or_usize ( cast_to) ) {
271
- ( true , true ) | ( false , false ) => ( to_nbits == from_nbits && cast_unsigned_to_signed, ArchSuffix :: None ) ,
272
- ( true , false ) => ( to_nbits <= 32 && cast_unsigned_to_signed, ArchSuffix :: _32) ,
273
- ( false , true ) => (
274
- cast_unsigned_to_signed,
275
- if from_nbits == 64 {
276
- ArchSuffix :: _64
277
- } else {
278
- ArchSuffix :: _32
279
- } ,
280
- ) ,
281
- } ;
282
- if span_wrap {
283
- span_lint (
284
- cx,
285
- CAST_POSSIBLE_WRAP ,
286
- expr. span ,
287
- & format ! (
288
- "casting `{}` to `{}` may wrap around the value{}" ,
289
- cast_from,
290
- cast_to,
291
- match suffix_wrap {
292
- ArchSuffix :: _32 => arch_32_suffix,
293
- ArchSuffix :: _64 => arch_64_suffix,
294
- ArchSuffix :: None => "" ,
295
- }
296
- ) ,
297
- ) ;
298
- }
299
- }
300
-
301
259
declare_lint_pass ! ( Casts => [
302
260
CAST_PRECISION_LOSS ,
303
261
CAST_SIGN_LOSS ,
@@ -449,16 +407,10 @@ fn lint_numeric_casts<'tcx>(
449
407
cast_to : Ty < ' tcx > ,
450
408
) {
451
409
cast_possible_truncation:: check ( cx, expr, cast_from, cast_to) ;
410
+ cast_possible_wrap:: check ( cx, expr, cast_from, cast_to) ;
452
411
cast_precision_loss:: check ( cx, expr, cast_from, cast_to) ;
453
412
cast_lossless:: check ( cx, expr, cast_op, cast_from, cast_to) ;
454
413
cast_sign_loss:: check ( cx, expr, cast_op, cast_from, cast_to) ;
455
-
456
- match ( cast_from. is_integral ( ) , cast_to. is_integral ( ) ) {
457
- ( true , true ) => {
458
- check_truncation_and_wrapping ( cx, expr, cast_from, cast_to) ;
459
- } ,
460
- ( _, _) => { } ,
461
- }
462
414
}
463
415
464
416
fn lint_cast_ptr_alignment < ' tcx > ( cx : & LateContext < ' tcx > , expr : & Expr < ' _ > , cast_from : Ty < ' tcx > , cast_to : Ty < ' tcx > ) {
0 commit comments