Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 25e4dd9

Browse files
committed
Avoid loading a snippet and render the literal directly
1 parent bf8b704 commit 25e4dd9

File tree

5 files changed

+13
-17
lines changed

5 files changed

+13
-17
lines changed

compiler/rustc_lint/src/types/literal.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -284,11 +284,7 @@ fn lint_int_literal<'tcx>(
284284
}
285285

286286
let span = if negative { type_limits.negated_expr_span.unwrap() } else { span };
287-
let lit = cx
288-
.sess()
289-
.source_map()
290-
.span_to_snippet(span)
291-
.unwrap_or_else(|_| if negative { format!("-{v}") } else { v.to_string() });
287+
let lit = if negative { format!("-{v}") } else { v.to_string() };
292288
// FIXME: Don't suggest unsigned types for negative literals
293289
let help = get_type_suggestion(cx.typeck_results().node_type(hir_id), v, negative)
294290
.map(|suggestion_ty| OverflowingIntHelp { suggestion_ty });

tests/ui/enum/enum-discrim-too-small2.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ error: literal out of range for `i32`
2727
LL | Ci32 = 3_000_000_000,
2828
| ^^^^^^^^^^^^^
2929
|
30-
= note: the literal `3_000_000_000` does not fit into the type `i32` whose range is `-2147483648..=2147483647`
30+
= note: the literal `3000000000` does not fit into the type `i32` whose range is `-2147483648..=2147483647`
3131
= help: consider using the type `u32` instead
3232

3333
error: literal out of range for `i64`

tests/ui/lint/lint-type-overflow.stderr

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ error: literal out of range for `i8`
4343
LL | let x3: i8 = -(129);
4444
| ^^^^^^
4545
|
46-
= note: the literal `-(129)` does not fit into the type `i8` whose range is `-128..=127`
46+
= note: the literal `-129` does not fit into the type `i8` whose range is `-128..=127`
4747
= help: consider using the type `i16` instead
4848

4949
error: literal out of range for `i8`
@@ -70,7 +70,7 @@ error: literal out of range for `i8`
7070
LL | let x = 128_i8;
7171
| ^^^^^^
7272
|
73-
= note: the literal `128_i8` does not fit into the type `i8` whose range is `-128..=127`
73+
= note: the literal `128` does not fit into the type `i8` whose range is `-128..=127`
7474
= help: consider using the type `u8` instead
7575

7676
error: literal out of range for `i8`
@@ -79,7 +79,7 @@ error: literal out of range for `i8`
7979
LL | let x = -129_i8;
8080
| ^^^^^^^
8181
|
82-
= note: the literal `-129_i8` does not fit into the type `i8` whose range is `-128..=127`
82+
= note: the literal `-129` does not fit into the type `i8` whose range is `-128..=127`
8383
= help: consider using the type `i16` instead
8484

8585
error: literal out of range for `i32`
@@ -97,7 +97,7 @@ error: literal out of range for `i32`
9797
LL | let x = 2147483648_i32;
9898
| ^^^^^^^^^^^^^^
9999
|
100-
= note: the literal `2147483648_i32` does not fit into the type `i32` whose range is `-2147483648..=2147483647`
100+
= note: the literal `2147483648` does not fit into the type `i32` whose range is `-2147483648..=2147483647`
101101
= help: consider using the type `u32` instead
102102

103103
error: literal out of range for `i32`
@@ -115,7 +115,7 @@ error: literal out of range for `i32`
115115
LL | let x = -2147483649_i32;
116116
| ^^^^^^^^^^^^^^^
117117
|
118-
= note: the literal `-2147483649_i32` does not fit into the type `i32` whose range is `-2147483648..=2147483647`
118+
= note: the literal `-2147483649` does not fit into the type `i32` whose range is `-2147483648..=2147483647`
119119
= help: consider using the type `i64` instead
120120

121121
error: literal out of range for `i32`
@@ -133,7 +133,7 @@ error: literal out of range for `i64`
133133
LL | let x = 9223372036854775808_i64;
134134
| ^^^^^^^^^^^^^^^^^^^^^^^
135135
|
136-
= note: the literal `9223372036854775808_i64` does not fit into the type `i64` whose range is `-9223372036854775808..=9223372036854775807`
136+
= note: the literal `9223372036854775808` does not fit into the type `i64` whose range is `-9223372036854775808..=9223372036854775807`
137137
= help: consider using the type `u64` instead
138138

139139
error: literal out of range for `i64`
@@ -142,7 +142,7 @@ error: literal out of range for `i64`
142142
LL | let x = 18446744073709551615_i64;
143143
| ^^^^^^^^^^^^^^^^^^^^^^^^
144144
|
145-
= note: the literal `18446744073709551615_i64` does not fit into the type `i64` whose range is `-9223372036854775808..=9223372036854775807`
145+
= note: the literal `18446744073709551615` does not fit into the type `i64` whose range is `-9223372036854775808..=9223372036854775807`
146146
= help: consider using the type `u64` instead
147147

148148
error: literal out of range for `i64`
@@ -160,7 +160,7 @@ error: literal out of range for `i64`
160160
LL | let x = -9223372036854775809_i64;
161161
| ^^^^^^^^^^^^^^^^^^^^^^^^
162162
|
163-
= note: the literal `-9223372036854775809_i64` does not fit into the type `i64` whose range is `-9223372036854775808..=9223372036854775807`
163+
= note: the literal `-9223372036854775809` does not fit into the type `i64` whose range is `-9223372036854775808..=9223372036854775807`
164164
= help: consider using the type `i128` instead
165165

166166
error: aborting due to 18 previous errors

tests/ui/lint/lint-type-overflow2.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ error: literal out of range for `i8`
1818
LL | let x2: i8 = --128;
1919
| ^^^^^
2020
|
21-
= note: the literal `--128` does not fit into the type `i8` whose range is `-128..=127`
21+
= note: the literal `128` does not fit into the type `i8` whose range is `-128..=127`
2222
= help: consider using the type `u8` instead
2323
note: the lint level is defined here
2424
--> $DIR/lint-type-overflow2.rs:3:9

tests/ui/lint/type-overflow.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ warning: literal out of range for `i8`
44
LL | let error = 255i8;
55
| ^^^^^
66
|
7-
= note: the literal `255i8` does not fit into the type `i8` whose range is `-128..=127`
7+
= note: the literal `255` does not fit into the type `i8` whose range is `-128..=127`
88
= help: consider using the type `u8` instead
99
note: the lint level is defined here
1010
--> $DIR/type-overflow.rs:2:9
@@ -104,7 +104,7 @@ warning: literal out of range for `i8`
104104
LL | let fail = 340282366920938463463374607431768211455i8;
105105
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
106106
|
107-
= note: the literal `340282366920938463463374607431768211455i8` does not fit into the type `i8` whose range is `-128..=127`
107+
= note: the literal `340282366920938463463374607431768211455` does not fit into the type `i8` whose range is `-128..=127`
108108
= help: consider using the type `u128` instead
109109

110110
warning: literal out of range for `i32`

0 commit comments

Comments
 (0)