Skip to content

Commit 844edfe

Browse files
compiler: reuse {un,}signed_fit in get_type_suggestion (nfc)
no need for a weird macro when a self-explanatory `match` will do.
1 parent 2db62e6 commit 844edfe

File tree

2 files changed

+36
-40
lines changed

2 files changed

+36
-40
lines changed

Diff for: compiler/rustc_abi/src/lib.rs

+22
Original file line numberDiff line numberDiff line change
@@ -833,6 +833,28 @@ pub enum Integer {
833833
}
834834

835835
impl Integer {
836+
pub fn int_ty_str(self) -> &'static str {
837+
use Integer::*;
838+
match self {
839+
I8 => "i8",
840+
I16 => "i16",
841+
I32 => "i32",
842+
I64 => "i64",
843+
I128 => "i128",
844+
}
845+
}
846+
847+
pub fn uint_ty_str(self) -> &'static str {
848+
use Integer::*;
849+
match self {
850+
I8 => "u8",
851+
I16 => "u16",
852+
I32 => "u32",
853+
I64 => "u64",
854+
I128 => "u128",
855+
}
856+
}
857+
836858
#[inline]
837859
pub fn size(self) -> Size {
838860
use Integer::*;

Diff for: compiler/rustc_lint/src/types/literal.rs

+14-40
Original file line numberDiff line numberDiff line change
@@ -202,49 +202,23 @@ fn report_bin_hex_error(
202202
)
203203
}
204204

205-
// This function finds the next fitting type and generates a suggestion string.
206-
// It searches for fitting types in the following way (`X < Y`):
207-
// - `iX`: if literal fits in `uX` => `uX`, else => `iY`
208-
// - `-iX` => `iY`
209-
// - `uX` => `uY`
205+
// Find the "next" fitting integer and return a suggestion string
210206
//
211-
// No suggestion for: `isize`, `usize`.
207+
// No suggestion is offered for `{i,u}size`. Otherwise, we try to suggest an equal-sized type.
212208
fn get_type_suggestion(t: Ty<'_>, val: u128, negative: bool) -> Option<&'static str> {
213-
use ty::IntTy::*;
214-
use ty::UintTy::*;
215-
macro_rules! find_fit {
216-
($ty:expr, $val:expr, $negative:expr,
217-
$($type:ident => [$($utypes:expr),*] => [$($itypes:expr),*]),+) => {
218-
{
219-
let _neg = if negative { 1 } else { 0 };
220-
match $ty {
221-
$($type => {
222-
$(if !negative && val <= uint_ty_range($utypes).1 {
223-
return Some($utypes.name_str())
224-
})*
225-
$(if val <= int_ty_range($itypes).1 as u128 + _neg {
226-
return Some($itypes.name_str())
227-
})*
228-
None
229-
},)+
230-
_ => None
231-
}
232-
}
233-
}
234-
}
235209
match t.kind() {
236-
ty::Int(i) => find_fit!(i, val, negative,
237-
I8 => [U8] => [I16, I32, I64, I128],
238-
I16 => [U16] => [I32, I64, I128],
239-
I32 => [U32] => [I64, I128],
240-
I64 => [U64] => [I128],
241-
I128 => [U128] => []),
242-
ty::Uint(u) => find_fit!(u, val, negative,
243-
U8 => [U8, U16, U32, U64, U128] => [],
244-
U16 => [U16, U32, U64, U128] => [],
245-
U32 => [U32, U64, U128] => [],
246-
U64 => [U64, U128] => [],
247-
U128 => [U128] => []),
210+
ty::Uint(ty::UintTy::Usize) | ty::Int(ty::IntTy::Isize) => None,
211+
ty::Uint(_) => Some(Integer::fit_unsigned(val).uint_ty_str()),
212+
ty::Int(_) if negative => Some(Integer::fit_signed(-(val as i128)).int_ty_str()),
213+
ty::Int(int) => {
214+
let signed = Integer::fit_signed(val as i128);
215+
let unsigned = Integer::fit_unsigned(val);
216+
Some(if Some(unsigned.size().bits()) == int.bit_width() {
217+
unsigned.uint_ty_str()
218+
} else {
219+
signed.int_ty_str()
220+
})
221+
}
248222
_ => None,
249223
}
250224
}

0 commit comments

Comments
 (0)