1
+ mod cast_lossless;
1
2
mod cast_precision_loss;
2
3
mod utils;
3
4
@@ -18,9 +19,8 @@ use rustc_target::abi::LayoutOf;
18
19
use crate :: consts:: { constant, Constant } ;
19
20
use crate :: utils:: sugg:: Sugg ;
20
21
use crate :: utils:: {
21
- in_constant, is_hir_ty_cfg_dependant, is_isize_or_usize, meets_msrv, method_chain_args,
22
- numeric_literal:: NumericLiteral , sext, snippet_opt, snippet_with_applicability, span_lint, span_lint_and_sugg,
23
- span_lint_and_then,
22
+ is_hir_ty_cfg_dependant, is_isize_or_usize, meets_msrv, method_chain_args, numeric_literal:: NumericLiteral , sext,
23
+ snippet_opt, snippet_with_applicability, span_lint, span_lint_and_sugg, span_lint_and_then,
24
24
} ;
25
25
26
26
use utils:: int_ty_to_nbits;
@@ -254,52 +254,6 @@ declare_clippy_lint! {
254
254
"casting a function pointer to a numeric type not wide enough to store the address"
255
255
}
256
256
257
- fn should_strip_parens ( op : & Expr < ' _ > , snip : & str ) -> bool {
258
- if let ExprKind :: Binary ( _, _, _) = op. kind {
259
- if snip. starts_with ( '(' ) && snip. ends_with ( ')' ) {
260
- return true ;
261
- }
262
- }
263
- false
264
- }
265
-
266
- fn span_lossless_lint ( cx : & LateContext < ' _ > , expr : & Expr < ' _ > , op : & Expr < ' _ > , cast_from : Ty < ' _ > , cast_to : Ty < ' _ > ) {
267
- // Do not suggest using From in consts/statics until it is valid to do so (see #2267).
268
- if in_constant ( cx, expr. hir_id ) {
269
- return ;
270
- }
271
- // The suggestion is to use a function call, so if the original expression
272
- // has parens on the outside, they are no longer needed.
273
- let mut applicability = Applicability :: MachineApplicable ;
274
- let opt = snippet_opt ( cx, op. span ) ;
275
- let sugg = opt. as_ref ( ) . map_or_else (
276
- || {
277
- applicability = Applicability :: HasPlaceholders ;
278
- ".."
279
- } ,
280
- |snip| {
281
- if should_strip_parens ( op, snip) {
282
- & snip[ 1 ..snip. len ( ) - 1 ]
283
- } else {
284
- snip. as_str ( )
285
- }
286
- } ,
287
- ) ;
288
-
289
- span_lint_and_sugg (
290
- cx,
291
- CAST_LOSSLESS ,
292
- expr. span ,
293
- & format ! (
294
- "casting `{}` to `{}` may become silently lossy if you later change the type" ,
295
- cast_from, cast_to
296
- ) ,
297
- "try" ,
298
- format ! ( "{}::from({})" , cast_to, sugg) ,
299
- applicability,
300
- ) ;
301
- }
302
-
303
257
enum ArchSuffix {
304
258
_32,
305
259
_64,
@@ -423,16 +377,6 @@ fn check_truncation_and_wrapping(cx: &LateContext<'_>, expr: &Expr<'_>, cast_fro
423
377
}
424
378
}
425
379
426
- fn check_lossless ( cx : & LateContext < ' _ > , expr : & Expr < ' _ > , op : & Expr < ' _ > , cast_from : Ty < ' _ > , cast_to : Ty < ' _ > ) {
427
- let cast_signed_to_unsigned = cast_from. is_signed ( ) && !cast_to. is_signed ( ) ;
428
- let from_nbits = int_ty_to_nbits ( cast_from, cx. tcx ) ;
429
- let to_nbits = int_ty_to_nbits ( cast_to, cx. tcx ) ;
430
- if !is_isize_or_usize ( cast_from) && !is_isize_or_usize ( cast_to) && from_nbits < to_nbits && !cast_signed_to_unsigned
431
- {
432
- span_lossless_lint ( cx, expr, op, cast_from, cast_to) ;
433
- }
434
- }
435
-
436
380
declare_lint_pass ! ( Casts => [
437
381
CAST_PRECISION_LOSS ,
438
382
CAST_SIGN_LOSS ,
@@ -584,18 +528,8 @@ fn lint_numeric_casts<'tcx>(
584
528
cast_to : Ty < ' tcx > ,
585
529
) {
586
530
cast_precision_loss:: check ( cx, expr, cast_from, cast_to) ;
531
+ cast_lossless:: check ( cx, expr, cast_expr, cast_from, cast_to) ;
587
532
match ( cast_from. is_integral ( ) , cast_to. is_integral ( ) ) {
588
- ( true , false ) => {
589
- let from_nbits = int_ty_to_nbits ( cast_from, cx. tcx ) ;
590
- let to_nbits = if let ty:: Float ( FloatTy :: F32 ) = cast_to. kind ( ) {
591
- 32
592
- } else {
593
- 64
594
- } ;
595
- if from_nbits < to_nbits {
596
- span_lossless_lint ( cx, expr, cast_expr, cast_from, cast_to) ;
597
- }
598
- } ,
599
533
( false , true ) => {
600
534
span_lint (
601
535
cx,
@@ -618,7 +552,6 @@ fn lint_numeric_casts<'tcx>(
618
552
( true , true ) => {
619
553
check_loss_of_sign ( cx, expr, cast_expr, cast_from, cast_to) ;
620
554
check_truncation_and_wrapping ( cx, expr, cast_from, cast_to) ;
621
- check_lossless ( cx, expr, cast_expr, cast_from, cast_to) ;
622
555
} ,
623
556
( false , false ) => {
624
557
if let ( & ty:: Float ( FloatTy :: F64 ) , & ty:: Float ( FloatTy :: F32 ) ) = ( & cast_from. kind ( ) , & cast_to. kind ( ) ) {
@@ -629,10 +562,8 @@ fn lint_numeric_casts<'tcx>(
629
562
"casting `f64` to `f32` may truncate the value" ,
630
563
) ;
631
564
}
632
- if let ( & ty:: Float ( FloatTy :: F32 ) , & ty:: Float ( FloatTy :: F64 ) ) = ( & cast_from. kind ( ) , & cast_to. kind ( ) ) {
633
- span_lossless_lint ( cx, expr, cast_expr, cast_from, cast_to) ;
634
- }
635
565
} ,
566
+ ( _, _) => { } ,
636
567
}
637
568
}
638
569
0 commit comments