You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Rollup merge of #135249 - s-cerevisiae:fix-overflowing-literals-help, r=chenyukang
Fix overflows in the implementation of `overflowing_literals` lint's help
This PR fixes two overflow problems that cause the `overflowing_literals` lint to behave incorrectly in some edge cases.
1. When an integer literal is between `i128::MAX` and `u128::MAX`, an overflowing `as` cast can cause the suggested type to be overly small. It's fixed by using checked type conversion and returning `u128` when it's the only choice. (Fixes#135248)
2. When an integer literal is `i128::MIN` but is of a smaller type, an overflowing negation cause the compiler to panic in debug build. Fixed by checking the number size beforehand and `wrapping_neg`. (Fixes#131849)
Edit: extracted the type conversion part into a standalone function to separate the concern of overflowing.
LL | let fail = 0x8000_0000_0000_0000_0000_0000_0000_0000;
72
+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
73
+
|
74
+
= note: the literal `0x8000_0000_0000_0000_0000_0000_0000_0000` (decimal `170141183460469231731687303715884105728`) does not fit into the type `i32` and will become `0i32`
75
+
= help: consider using the type `u128` instead
76
+
77
+
warning: literal out of range for `i32`
78
+
--> $DIR/type-overflow.rs:31:17
79
+
|
80
+
LL | let fail = -0x8000_0000_0000_0000_0000_0000_0000_0000; // issue #131849
81
+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
82
+
|
83
+
= note: the literal `0x8000_0000_0000_0000_0000_0000_0000_0000` (decimal `170141183460469231731687303715884105728`) does not fit into the type `i32`
84
+
= note: and the value `-0x8000_0000_0000_0000_0000_0000_0000_0000` will become `0i32`
85
+
= help: consider using the type `i128` instead
86
+
87
+
warning: literal out of range for `i128`
88
+
--> $DIR/type-overflow.rs:35:17
89
+
|
90
+
LL | let fail = -0x8000_0000_0000_0000_0000_0000_0000_0001i128;
91
+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
92
+
|
93
+
= note: the literal `0x8000_0000_0000_0000_0000_0000_0000_0001i128` (decimal `170141183460469231731687303715884105729`) does not fit into the type `i128`
94
+
= note: and the value `-0x8000_0000_0000_0000_0000_0000_0000_0001i128` will become `170141183460469231731687303715884105727i128`
95
+
96
+
warning: literal out of range for `i8`
97
+
--> $DIR/type-overflow.rs:38:16
98
+
|
99
+
LL | let fail = 340282366920938463463374607431768211455i8;
100
+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
101
+
|
102
+
= note: the literal `340282366920938463463374607431768211455i8` does not fit into the type `i8` whose range is `-128..=127`
103
+
= help: consider using the type `u128` instead
104
+
105
+
warning: literal out of range for `i32`
106
+
--> $DIR/type-overflow.rs:42:16
70
107
|
71
108
LL | let fail = 0x8FFF_FFFF_FFFF_FFFE;
72
109
| ^^^^^^^^^^^^^^^^^^^^^
73
110
|
74
111
= note: the literal `0x8FFF_FFFF_FFFF_FFFE` (decimal `10376293541461622782`) does not fit into the type `i32` and will become `-2i32`
75
-
= help: consider using the type `i128` instead
112
+
= help: consider using the type `u64` instead
76
113
help: to use as a negative number (decimal `-2`), consider using the type `u32` for the literal and cast it to `i32`
77
114
|
78
115
LL | let fail = 0x8FFF_FFFF_FFFF_FFFEu32 as i32;
79
116
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
80
117
81
118
warning: literal out of range for `i8`
82
-
--> $DIR/type-overflow.rs:21:17
119
+
--> $DIR/type-overflow.rs:46:17
83
120
|
84
121
LL | let fail = -0b1111_1111i8;
85
122
| ^^^^^^^^^^^^^ help: consider using the type `i16` instead: `0b1111_1111i16`
86
123
|
87
124
= note: the literal `0b1111_1111i8` (decimal `255`) does not fit into the type `i8`
88
125
= note: and the value `-0b1111_1111i8` will become `1i8`
0 commit comments