Skip to content

Commit be23697

Browse files
committedJan 8, 2025
Detect overflow when the literal is larger than i128::MAX
1 parent 6f2ca60 commit be23697

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed
 

‎compiler/rustc_lint/src/types/literal.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -206,12 +206,15 @@ fn get_type_suggestion(t: Ty<'_>, val: u128, negative: bool) -> Option<&'static
206206
ty::Uint(_) => Some(Integer::fit_unsigned(val).uint_ty_str()),
207207
ty::Int(_) if negative => Some(Integer::fit_signed(-(val as i128)).int_ty_str()),
208208
ty::Int(int) => {
209-
let signed = Integer::fit_signed(val as i128);
210209
let unsigned = Integer::fit_unsigned(val);
211-
Some(if Some(unsigned.size().bits()) == int.bit_width() {
212-
unsigned.uint_ty_str()
210+
Some(if let Ok(signed) = i128::try_from(val).map(Integer::fit_signed) {
211+
if Some(unsigned.size().bits()) == int.bit_width() {
212+
unsigned.uint_ty_str()
213+
} else {
214+
signed.int_ty_str()
215+
}
213216
} else {
214-
signed.int_ty_str()
217+
unsigned.uint_ty_str()
215218
})
216219
}
217220
_ => None,

0 commit comments

Comments
 (0)