Skip to content

Commit 477108d

Browse files
committed
FP: cast_lossless: disable lint when casting to (u)128 from any (u)int type
1 parent b5dcaae commit 477108d

File tree

4 files changed

+39
-24
lines changed

4 files changed

+39
-24
lines changed

clippy_lints/src/casts/cast_lossless.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use clippy_utils::ty::is_isize_or_usize;
66
use rustc_errors::Applicability;
77
use rustc_hir::{Expr, ExprKind, QPath, TyKind};
88
use rustc_lint::LateContext;
9-
use rustc_middle::ty::{self, FloatTy, Ty};
9+
use rustc_middle::ty::{self, FloatTy, Ty, UintTy};
1010

1111
use super::{utils, CAST_LOSSLESS};
1212

@@ -77,7 +77,10 @@ pub(super) fn check(
7777

7878
fn should_lint(cx: &LateContext<'_>, expr: &Expr<'_>, cast_from: Ty<'_>, cast_to: Ty<'_>, msrv: &Msrv) -> bool {
7979
// Do not suggest using From in consts/statics until it is valid to do so (see #2267).
80-
if in_constant(cx, expr.hir_id) {
80+
//
81+
// If destination is u128, do not lint because source type cannot be larger
82+
// If source is bool, still lint due to the lint message differing (refers to style)
83+
if in_constant(cx, expr.hir_id) || (!cast_from.is_bool() && matches!(cast_to.kind(), ty::Uint(UintTy::U128))) {
8184
return false;
8285
}
8386

tests/ui/cast_lossless_integer.fixed

+6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#![warn(clippy::cast_lossless)]
33

44
type I64 = i64;
5+
type U128 = u128;
56

67
fn main() {
78
// Test clippy::cast_lossless with casts to integer types
@@ -28,6 +29,11 @@ fn main() {
2829
let _ = u16::from(1u8 + 1u8);
2930

3031
let _ = I64::from(1i8);
32+
33+
// Do not lint if destination type is u128
34+
// see https://github.com/rust-lang/rust-clippy/issues/12492
35+
let _ = 1u8 as u128;
36+
let _ = 1u8 as U128;
3137
}
3238

3339
// The lint would suggest using `f64::from(input)` here but the `XX::from` function is not const,

tests/ui/cast_lossless_integer.rs

+6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#![warn(clippy::cast_lossless)]
33

44
type I64 = i64;
5+
type U128 = u128;
56

67
fn main() {
78
// Test clippy::cast_lossless with casts to integer types
@@ -28,6 +29,11 @@ fn main() {
2829
let _ = (1u8 + 1u8) as u16;
2930

3031
let _ = 1i8 as I64;
32+
33+
// Do not lint if destination type is u128
34+
// see https://github.com/rust-lang/rust-clippy/issues/12492
35+
let _ = 1u8 as u128;
36+
let _ = 1u8 as U128;
3137
}
3238

3339
// The lint would suggest using `f64::from(input)` here but the `XX::from` function is not const,

tests/ui/cast_lossless_integer.stderr

+22-22
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: casting `i8` to `i16` may become silently lossy if you later change the type
2-
--> tests/ui/cast_lossless_integer.rs:8:13
2+
--> tests/ui/cast_lossless_integer.rs:9:13
33
|
44
LL | let _ = 1i8 as i16;
55
| ^^^^^^^^^^ help: try: `i16::from(1i8)`
@@ -8,127 +8,127 @@ LL | let _ = 1i8 as i16;
88
= help: to override `-D warnings` add `#[allow(clippy::cast_lossless)]`
99

1010
error: casting `i8` to `i32` may become silently lossy if you later change the type
11-
--> tests/ui/cast_lossless_integer.rs:9:13
11+
--> tests/ui/cast_lossless_integer.rs:10:13
1212
|
1313
LL | let _ = 1i8 as i32;
1414
| ^^^^^^^^^^ help: try: `i32::from(1i8)`
1515

1616
error: casting `i8` to `i64` may become silently lossy if you later change the type
17-
--> tests/ui/cast_lossless_integer.rs:10:13
17+
--> tests/ui/cast_lossless_integer.rs:11:13
1818
|
1919
LL | let _ = 1i8 as i64;
2020
| ^^^^^^^^^^ help: try: `i64::from(1i8)`
2121

2222
error: casting `u8` to `i16` may become silently lossy if you later change the type
23-
--> tests/ui/cast_lossless_integer.rs:11:13
23+
--> tests/ui/cast_lossless_integer.rs:12:13
2424
|
2525
LL | let _ = 1u8 as i16;
2626
| ^^^^^^^^^^ help: try: `i16::from(1u8)`
2727

2828
error: casting `u8` to `i32` may become silently lossy if you later change the type
29-
--> tests/ui/cast_lossless_integer.rs:12:13
29+
--> tests/ui/cast_lossless_integer.rs:13:13
3030
|
3131
LL | let _ = 1u8 as i32;
3232
| ^^^^^^^^^^ help: try: `i32::from(1u8)`
3333

3434
error: casting `u8` to `i64` may become silently lossy if you later change the type
35-
--> tests/ui/cast_lossless_integer.rs:13:13
35+
--> tests/ui/cast_lossless_integer.rs:14:13
3636
|
3737
LL | let _ = 1u8 as i64;
3838
| ^^^^^^^^^^ help: try: `i64::from(1u8)`
3939

4040
error: casting `u8` to `u16` may become silently lossy if you later change the type
41-
--> tests/ui/cast_lossless_integer.rs:14:13
41+
--> tests/ui/cast_lossless_integer.rs:15:13
4242
|
4343
LL | let _ = 1u8 as u16;
4444
| ^^^^^^^^^^ help: try: `u16::from(1u8)`
4545

4646
error: casting `u8` to `u32` may become silently lossy if you later change the type
47-
--> tests/ui/cast_lossless_integer.rs:15:13
47+
--> tests/ui/cast_lossless_integer.rs:16:13
4848
|
4949
LL | let _ = 1u8 as u32;
5050
| ^^^^^^^^^^ help: try: `u32::from(1u8)`
5151

5252
error: casting `u8` to `u64` may become silently lossy if you later change the type
53-
--> tests/ui/cast_lossless_integer.rs:16:13
53+
--> tests/ui/cast_lossless_integer.rs:17:13
5454
|
5555
LL | let _ = 1u8 as u64;
5656
| ^^^^^^^^^^ help: try: `u64::from(1u8)`
5757

5858
error: casting `i16` to `i32` may become silently lossy if you later change the type
59-
--> tests/ui/cast_lossless_integer.rs:17:13
59+
--> tests/ui/cast_lossless_integer.rs:18:13
6060
|
6161
LL | let _ = 1i16 as i32;
6262
| ^^^^^^^^^^^ help: try: `i32::from(1i16)`
6363

6464
error: casting `i16` to `i64` may become silently lossy if you later change the type
65-
--> tests/ui/cast_lossless_integer.rs:18:13
65+
--> tests/ui/cast_lossless_integer.rs:19:13
6666
|
6767
LL | let _ = 1i16 as i64;
6868
| ^^^^^^^^^^^ help: try: `i64::from(1i16)`
6969

7070
error: casting `u16` to `i32` may become silently lossy if you later change the type
71-
--> tests/ui/cast_lossless_integer.rs:19:13
71+
--> tests/ui/cast_lossless_integer.rs:20:13
7272
|
7373
LL | let _ = 1u16 as i32;
7474
| ^^^^^^^^^^^ help: try: `i32::from(1u16)`
7575

7676
error: casting `u16` to `i64` may become silently lossy if you later change the type
77-
--> tests/ui/cast_lossless_integer.rs:20:13
77+
--> tests/ui/cast_lossless_integer.rs:21:13
7878
|
7979
LL | let _ = 1u16 as i64;
8080
| ^^^^^^^^^^^ help: try: `i64::from(1u16)`
8181

8282
error: casting `u16` to `u32` may become silently lossy if you later change the type
83-
--> tests/ui/cast_lossless_integer.rs:21:13
83+
--> tests/ui/cast_lossless_integer.rs:22:13
8484
|
8585
LL | let _ = 1u16 as u32;
8686
| ^^^^^^^^^^^ help: try: `u32::from(1u16)`
8787

8888
error: casting `u16` to `u64` may become silently lossy if you later change the type
89-
--> tests/ui/cast_lossless_integer.rs:22:13
89+
--> tests/ui/cast_lossless_integer.rs:23:13
9090
|
9191
LL | let _ = 1u16 as u64;
9292
| ^^^^^^^^^^^ help: try: `u64::from(1u16)`
9393

9494
error: casting `i32` to `i64` may become silently lossy if you later change the type
95-
--> tests/ui/cast_lossless_integer.rs:23:13
95+
--> tests/ui/cast_lossless_integer.rs:24:13
9696
|
9797
LL | let _ = 1i32 as i64;
9898
| ^^^^^^^^^^^ help: try: `i64::from(1i32)`
9999

100100
error: casting `u32` to `i64` may become silently lossy if you later change the type
101-
--> tests/ui/cast_lossless_integer.rs:24:13
101+
--> tests/ui/cast_lossless_integer.rs:25:13
102102
|
103103
LL | let _ = 1u32 as i64;
104104
| ^^^^^^^^^^^ help: try: `i64::from(1u32)`
105105

106106
error: casting `u32` to `u64` may become silently lossy if you later change the type
107-
--> tests/ui/cast_lossless_integer.rs:25:13
107+
--> tests/ui/cast_lossless_integer.rs:26:13
108108
|
109109
LL | let _ = 1u32 as u64;
110110
| ^^^^^^^^^^^ help: try: `u64::from(1u32)`
111111

112112
error: casting `u8` to `u16` may become silently lossy if you later change the type
113-
--> tests/ui/cast_lossless_integer.rs:28:13
113+
--> tests/ui/cast_lossless_integer.rs:29:13
114114
|
115115
LL | let _ = (1u8 + 1u8) as u16;
116116
| ^^^^^^^^^^^^^^^^^^ help: try: `u16::from(1u8 + 1u8)`
117117

118118
error: casting `i8` to `I64` may become silently lossy if you later change the type
119-
--> tests/ui/cast_lossless_integer.rs:30:13
119+
--> tests/ui/cast_lossless_integer.rs:31:13
120120
|
121121
LL | let _ = 1i8 as I64;
122122
| ^^^^^^^^^^ help: try: `I64::from(1i8)`
123123

124124
error: casting `i8` to `i32` may become silently lossy if you later change the type
125-
--> tests/ui/cast_lossless_integer.rs:64:13
125+
--> tests/ui/cast_lossless_integer.rs:70:13
126126
|
127127
LL | let _ = sign_cast!(x, u8, i8) as i32;
128128
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `i32::from(sign_cast!(x, u8, i8))`
129129

130130
error: casting `i8` to `i32` may become silently lossy if you later change the type
131-
--> tests/ui/cast_lossless_integer.rs:65:13
131+
--> tests/ui/cast_lossless_integer.rs:71:13
132132
|
133133
LL | let _ = (sign_cast!(x, u8, i8) + 1) as i32;
134134
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `i32::from(sign_cast!(x, u8, i8) + 1)`

0 commit comments

Comments
 (0)