Fix unwrap error in overflowing int literal
This commit is contained in:
parent
43ca9d18e3
commit
ace6bb9869
4 changed files with 32 additions and 4 deletions
|
@ -543,7 +543,11 @@ impl<'tcx> LateLintPass<'tcx> for TypeLimits {
|
||||||
lit: &'tcx hir::Lit,
|
lit: &'tcx hir::Lit,
|
||||||
negated: bool,
|
negated: bool,
|
||||||
) {
|
) {
|
||||||
lint_literal(cx, self, hir_id, lit.span, lit, negated)
|
if negated {
|
||||||
|
self.negated_expr_id = Some(hir_id);
|
||||||
|
self.negated_expr_span = Some(lit.span);
|
||||||
|
}
|
||||||
|
lint_literal(cx, self, hir_id, lit.span, lit, negated);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx hir::Expr<'tcx>) {
|
fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx hir::Expr<'tcx>) {
|
||||||
|
|
|
@ -245,12 +245,11 @@ fn lint_int_literal<'tcx>(
|
||||||
lit: &hir::Lit,
|
lit: &hir::Lit,
|
||||||
t: ty::IntTy,
|
t: ty::IntTy,
|
||||||
v: u128,
|
v: u128,
|
||||||
negated: bool,
|
|
||||||
) {
|
) {
|
||||||
let int_type = t.normalize(cx.sess().target.pointer_width);
|
let int_type = t.normalize(cx.sess().target.pointer_width);
|
||||||
let (min, max) = int_ty_range(int_type);
|
let (min, max) = int_ty_range(int_type);
|
||||||
let max = max as u128;
|
let max = max as u128;
|
||||||
let negative = negated ^ (type_limits.negated_expr_id == Some(hir_id));
|
let negative = type_limits.negated_expr_id == Some(hir_id);
|
||||||
|
|
||||||
// Detect literal value out of range [min, max] inclusive
|
// Detect literal value out of range [min, max] inclusive
|
||||||
// avoiding use of -min to prevent overflow/panic
|
// avoiding use of -min to prevent overflow/panic
|
||||||
|
@ -366,7 +365,7 @@ pub(crate) fn lint_literal<'tcx>(
|
||||||
ty::Int(t) => {
|
ty::Int(t) => {
|
||||||
match lit.node {
|
match lit.node {
|
||||||
ast::LitKind::Int(v, ast::LitIntType::Signed(_) | ast::LitIntType::Unsuffixed) => {
|
ast::LitKind::Int(v, ast::LitIntType::Signed(_) | ast::LitIntType::Unsuffixed) => {
|
||||||
lint_int_literal(cx, type_limits, hir_id, span, lit, t, v.get(), negated)
|
lint_int_literal(cx, type_limits, hir_id, span, lit, t, v.get())
|
||||||
}
|
}
|
||||||
_ => bug!(),
|
_ => bug!(),
|
||||||
};
|
};
|
||||||
|
|
4
tests/ui/lint/lint-overflowing-int-136675.rs
Normal file
4
tests/ui/lint/lint-overflowing-int-136675.rs
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
fn main() {
|
||||||
|
if let -129 = 0i8 {} //~ ERROR literal out of range for `i8`
|
||||||
|
let x: i8 = -129; //~ ERROR literal out of range for `i8`
|
||||||
|
}
|
21
tests/ui/lint/lint-overflowing-int-136675.stderr
Normal file
21
tests/ui/lint/lint-overflowing-int-136675.stderr
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
error: literal out of range for `i8`
|
||||||
|
--> $DIR/lint-overflowing-int-136675.rs:2:12
|
||||||
|
|
|
||||||
|
LL | if let -129 = 0i8 {}
|
||||||
|
| ^^^^
|
||||||
|
|
|
||||||
|
= note: the literal `-129` does not fit into the type `i8` whose range is `-128..=127`
|
||||||
|
= help: consider using the type `i16` instead
|
||||||
|
= note: `#[deny(overflowing_literals)]` on by default
|
||||||
|
|
||||||
|
error: literal out of range for `i8`
|
||||||
|
--> $DIR/lint-overflowing-int-136675.rs:3:17
|
||||||
|
|
|
||||||
|
LL | let x: i8 = -129;
|
||||||
|
| ^^^^
|
||||||
|
|
|
||||||
|
= note: the literal `-129` does not fit into the type `i8` whose range is `-128..=127`
|
||||||
|
= help: consider using the type `i16` instead
|
||||||
|
|
||||||
|
error: aborting due to 2 previous errors
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue