Skip to content

Commit 526e328

Browse files
committed
Fix unwrap error in overflowing int literal
1 parent 43ca9d1 commit 526e328

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

compiler/rustc_lint/src/types/literal.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ fn lint_int_literal<'tcx>(
274274
return;
275275
}
276276

277-
let span = if negative { type_limits.negated_expr_span.unwrap() } else { span };
277+
let span = if negative { type_limits.negated_expr_span.unwrap_or(span) } else { span };
278278
let lit = cx
279279
.sess()
280280
.source_map()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
fn main() {
2+
if let -129 = 0i8 {} //~ ERROR literal out of range for `i8`
3+
let x: i8 = -129; //~ ERROR literal out of range for `i8`
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
error: literal out of range for `i8`
2+
--> $DIR/lint-overflowing-int-136675.rs:2:12
3+
|
4+
LL | if let -129 = 0i8 {}
5+
| ^^^^
6+
|
7+
= note: the literal `-129` does not fit into the type `i8` whose range is `-128..=127`
8+
= help: consider using the type `i16` instead
9+
= note: `#[deny(overflowing_literals)]` on by default
10+
11+
error: literal out of range for `i8`
12+
--> $DIR/lint-overflowing-int-136675.rs:3:17
13+
|
14+
LL | let x: i8 = -129;
15+
| ^^^^
16+
|
17+
= note: the literal `-129` does not fit into the type `i8` whose range is `-128..=127`
18+
= help: consider using the type `i16` instead
19+
20+
error: aborting due to 2 previous errors
21+

0 commit comments

Comments
 (0)