1
Fork 0

Suggest the smallest fitting type instead

Changes the behavior of the `overflowing_literals` suggestion so that it
always suggest the smallest type regardless of the original type size.
This commit is contained in:
spore 2025-01-12 20:18:53 +08:00
parent f52724c917
commit 74e2e8b598
3 changed files with 4 additions and 4 deletions

View file

@ -204,14 +204,14 @@ fn get_type_suggestion(t: Ty<'_>, val: u128, negative: bool) -> Option<&'static
match t.kind() {
ty::Uint(ty::UintTy::Usize) | ty::Int(ty::IntTy::Isize) => None,
ty::Uint(_) => Some(Integer::fit_unsigned(val).uint_ty_str()),
ty::Int(int) => {
ty::Int(_) => {
let signed = literal_to_i128(val, negative).map(Integer::fit_signed);
if negative {
signed.map(Integer::int_ty_str)
} else {
let unsigned = Integer::fit_unsigned(val);
Some(if let Some(signed) = signed {
if Some(unsigned.size().bits()) == int.bit_width() {
if unsigned.size() < signed.size() {
unsigned.uint_ty_str()
} else {
signed.int_ty_str()