Preserve literal suffixes

This commit is contained in:
varkor 2019-04-27 13:26:06 +01:00
parent 218982bef0
commit 0556e4891e
3 changed files with 25 additions and 2 deletions

View File

@ -172,10 +172,26 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TypeLimits {
if let Ok(start) = cx.sess().source_map()
.span_to_snippet(eps[0].span)
{
use ast::{LitKind::*, LitIntType};
// We need to preserve the literal's suffix,
// as it may determine typing information.
let suffix = match lit.node {
Int(_, LitIntType::Signed(s)) => {
format!("{}", s)
}
Int(_, LitIntType::Unsigned(s)) => {
format!("{}", s)
}
Int(_, LitIntType::Unsuffixed) => {
"".to_owned()
}
_ => bug!(),
};
let suggestion = format!(
"{}..={}",
"{}..={}{}",
start,
lit_val - 1,
suffix,
);
err.span_suggestion(
parent_expr.span,

View File

@ -6,6 +6,7 @@ fn main() {
let range_c = 0..=256; //~ ERROR literal out of range for `u8`
let range_d = 256..5; //~ ERROR literal out of range for `u8`
let range_e = 0..257; //~ ERROR literal out of range for `u8`
let _range_f = 0..256u8; //~ ERROR range endpoint is out of range for `u8`
range_a.collect::<Vec<u8>>();
range_b.collect::<Vec<u8>>();

View File

@ -28,5 +28,11 @@ error: literal out of range for `u8`
LL | let range_e = 0..257;
| ^^^
error: aborting due to 4 previous errors
error: range endpoint is out of range for `u8`
--> $DIR/lint-range-endpoint-overflow.rs:9:20
|
LL | let _range_f = 0..256u8;
| ^^^^^^^^ help: use an inclusive range instead: `0..=255u8`
error: aborting due to 5 previous errors