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:
parent
f52724c917
commit
74e2e8b598
3 changed files with 4 additions and 4 deletions
|
@ -204,14 +204,14 @@ fn get_type_suggestion(t: Ty<'_>, val: u128, negative: bool) -> Option<&'static
|
||||||
match t.kind() {
|
match t.kind() {
|
||||||
ty::Uint(ty::UintTy::Usize) | ty::Int(ty::IntTy::Isize) => None,
|
ty::Uint(ty::UintTy::Usize) | ty::Int(ty::IntTy::Isize) => None,
|
||||||
ty::Uint(_) => Some(Integer::fit_unsigned(val).uint_ty_str()),
|
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);
|
let signed = literal_to_i128(val, negative).map(Integer::fit_signed);
|
||||||
if negative {
|
if negative {
|
||||||
signed.map(Integer::int_ty_str)
|
signed.map(Integer::int_ty_str)
|
||||||
} else {
|
} else {
|
||||||
let unsigned = Integer::fit_unsigned(val);
|
let unsigned = Integer::fit_unsigned(val);
|
||||||
Some(if let Some(signed) = signed {
|
Some(if let Some(signed) = signed {
|
||||||
if Some(unsigned.size().bits()) == int.bit_width() {
|
if unsigned.size() < signed.size() {
|
||||||
unsigned.uint_ty_str()
|
unsigned.uint_ty_str()
|
||||||
} else {
|
} else {
|
||||||
signed.int_ty_str()
|
signed.int_ty_str()
|
||||||
|
|
|
@ -40,7 +40,7 @@ fn main() {
|
||||||
//~| HELP consider using the type `u128` instead
|
//~| HELP consider using the type `u128` instead
|
||||||
|
|
||||||
let fail = 0x8FFF_FFFF_FFFF_FFFE; //~WARNING literal out of range for `i32`
|
let fail = 0x8FFF_FFFF_FFFF_FFFE; //~WARNING literal out of range for `i32`
|
||||||
//~| HELP consider using the type `i128` instead
|
//~| HELP consider using the type `u64` instead
|
||||||
//~| HELP
|
//~| HELP
|
||||||
|
|
||||||
let fail = -0b1111_1111i8; //~WARNING literal out of range for `i8`
|
let fail = -0b1111_1111i8; //~WARNING literal out of range for `i8`
|
||||||
|
|
|
@ -109,7 +109,7 @@ LL | let fail = 0x8FFF_FFFF_FFFF_FFFE;
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^
|
| ^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
|
|
||||||
= note: the literal `0x8FFF_FFFF_FFFF_FFFE` (decimal `10376293541461622782`) does not fit into the type `i32` and will become `-2i32`
|
= note: the literal `0x8FFF_FFFF_FFFF_FFFE` (decimal `10376293541461622782`) does not fit into the type `i32` and will become `-2i32`
|
||||||
= help: consider using the type `i128` instead
|
= help: consider using the type `u64` instead
|
||||||
help: to use as a negative number (decimal `-2`), consider using the type `u32` for the literal and cast it to `i32`
|
help: to use as a negative number (decimal `-2`), consider using the type `u32` for the literal and cast it to `i32`
|
||||||
|
|
|
|
||||||
LL | let fail = 0x8FFF_FFFF_FFFF_FFFEu32 as i32;
|
LL | let fail = 0x8FFF_FFFF_FFFF_FFFEu32 as i32;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue