about summary refs log tree commit diff
diff options
context:
space:
mode:
authorspore <spore2050@gmail.com>2025-01-12 20:18:53 +0800
committerspore <spore2050@gmail.com>2025-01-12 20:20:39 +0800
commit74e2e8b59834aede7aafee57e00ede07ae13739b (patch)
tree0c1ba3f51899d8d7b54448c37a0b878d6feb5ace
parentf52724c917216a8c9126479b0e0c097afe447593 (diff)
downloadrust-74e2e8b59834aede7aafee57e00ede07ae13739b.tar.gz
rust-74e2e8b59834aede7aafee57e00ede07ae13739b.zip
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.
-rw-r--r--compiler/rustc_lint/src/types/literal.rs4
-rw-r--r--tests/ui/lint/type-overflow.rs2
-rw-r--r--tests/ui/lint/type-overflow.stderr2
3 files changed, 4 insertions, 4 deletions
diff --git a/compiler/rustc_lint/src/types/literal.rs b/compiler/rustc_lint/src/types/literal.rs
index f7a59437bbc..4b5163522f8 100644
--- a/compiler/rustc_lint/src/types/literal.rs
+++ b/compiler/rustc_lint/src/types/literal.rs
@@ -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()
diff --git a/tests/ui/lint/type-overflow.rs b/tests/ui/lint/type-overflow.rs
index 102d042773b..1e74a8925f6 100644
--- a/tests/ui/lint/type-overflow.rs
+++ b/tests/ui/lint/type-overflow.rs
@@ -40,7 +40,7 @@ fn main() {
     //~| HELP consider using the type `u128` instead
 
     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
 
     let fail = -0b1111_1111i8; //~WARNING literal out of range for `i8`
diff --git a/tests/ui/lint/type-overflow.stderr b/tests/ui/lint/type-overflow.stderr
index 720044c68eb..9fdb05ed1c0 100644
--- a/tests/ui/lint/type-overflow.stderr
+++ b/tests/ui/lint/type-overflow.stderr
@@ -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`
-   = 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`
    |
 LL |     let fail = 0x8FFF_FFFF_FFFF_FFFEu32 as i32;