Skip to content

Commit 0556e48

Browse files
committed
Preserve literal suffixes
1 parent 218982b commit 0556e48

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

src/librustc_lint/types.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,10 +172,26 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TypeLimits {
172172
if let Ok(start) = cx.sess().source_map()
173173
.span_to_snippet(eps[0].span)
174174
{
175+
use ast::{LitKind::*, LitIntType};
176+
// We need to preserve the literal's suffix,
177+
// as it may determine typing information.
178+
let suffix = match lit.node {
179+
Int(_, LitIntType::Signed(s)) => {
180+
format!("{}", s)
181+
}
182+
Int(_, LitIntType::Unsigned(s)) => {
183+
format!("{}", s)
184+
}
185+
Int(_, LitIntType::Unsuffixed) => {
186+
"".to_owned()
187+
}
188+
_ => bug!(),
189+
};
175190
let suggestion = format!(
176-
"{}..={}",
191+
"{}..={}{}",
177192
start,
178193
lit_val - 1,
194+
suffix,
179195
);
180196
err.span_suggestion(
181197
parent_expr.span,

src/test/ui/lint/lint-range-endpoint-overflow.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ fn main() {
66
let range_c = 0..=256; //~ ERROR literal out of range for `u8`
77
let range_d = 256..5; //~ ERROR literal out of range for `u8`
88
let range_e = 0..257; //~ ERROR literal out of range for `u8`
9+
let _range_f = 0..256u8; //~ ERROR range endpoint is out of range for `u8`
910

1011
range_a.collect::<Vec<u8>>();
1112
range_b.collect::<Vec<u8>>();

src/test/ui/lint/lint-range-endpoint-overflow.stderr

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,11 @@ error: literal out of range for `u8`
2828
LL | let range_e = 0..257;
2929
| ^^^
3030

31-
error: aborting due to 4 previous errors
31+
error: range endpoint is out of range for `u8`
32+
--> $DIR/lint-range-endpoint-overflow.rs:9:20
33+
|
34+
LL | let _range_f = 0..256u8;
35+
| ^^^^^^^^ help: use an inclusive range instead: `0..=255u8`
36+
37+
error: aborting due to 5 previous errors
3238

0 commit comments

Comments
 (0)