Skip to content

Commit 58d2101

Browse files
committed
Fix span of overflow lint for negated literals
1 parent 8e8875d commit 58d2101

File tree

2 files changed

+30
-29
lines changed

2 files changed

+30
-29
lines changed

compiler/rustc_lint/src/types.rs

+9-8
Original file line numberDiff line numberDiff line change
@@ -140,13 +140,15 @@ declare_lint! {
140140
pub struct TypeLimits {
141141
/// Id of the last visited negated expression
142142
negated_expr_id: Option<hir::HirId>,
143+
/// Span of the last visited negated expression
144+
negated_expr_span: Option<Span>,
143145
}
144146

145147
impl_lint_pass!(TypeLimits => [UNUSED_COMPARISONS, OVERFLOWING_LITERALS, INVALID_NAN_COMPARISONS]);
146148

147149
impl TypeLimits {
148150
pub fn new() -> TypeLimits {
149-
TypeLimits { negated_expr_id: None }
151+
TypeLimits { negated_expr_id: None, negated_expr_span: None }
150152
}
151153
}
152154

@@ -426,17 +428,15 @@ fn lint_int_literal<'tcx>(
426428
return;
427429
}
428430

429-
let lit = cx
430-
.sess()
431-
.source_map()
432-
.span_to_snippet(lit.span)
433-
.expect("must get snippet from literal");
431+
let span = if negative { type_limits.negated_expr_span.unwrap() } else { e.span };
432+
let lit =
433+
cx.sess().source_map().span_to_snippet(span).expect("must get snippet from literal");
434434
let help = get_type_suggestion(cx.typeck_results().node_type(e.hir_id), v, negative)
435435
.map(|suggestion_ty| OverflowingIntHelp { suggestion_ty });
436436

437437
cx.emit_spanned_lint(
438438
OVERFLOWING_LITERALS,
439-
e.span,
439+
span,
440440
OverflowingInt { ty: t.name_str(), lit, min, max, help },
441441
);
442442
}
@@ -622,9 +622,10 @@ impl<'tcx> LateLintPass<'tcx> for TypeLimits {
622622
fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx hir::Expr<'tcx>) {
623623
match e.kind {
624624
hir::ExprKind::Unary(hir::UnOp::Neg, ref expr) => {
625-
// propagate negation, if the negation itself isn't negated
625+
// Propagate negation, if the negation itself isn't negated
626626
if self.negated_expr_id != Some(e.hir_id) {
627627
self.negated_expr_id = Some(expr.hir_id);
628+
self.negated_expr_span = Some(e.span);
628629
}
629630
}
630631
hir::ExprKind::Binary(binop, ref l, ref r) => {

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

+21-21
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,21 @@ LL | let x1: i8 = 128;
2929
= help: consider using the type `u8` instead
3030

3131
error: literal out of range for `i8`
32-
--> $DIR/lint-type-overflow.rs:18:19
32+
--> $DIR/lint-type-overflow.rs:18:18
3333
|
3434
LL | let x3: i8 = -129;
35-
| ^^^
35+
| ^^^^
3636
|
37-
= note: the literal `129` does not fit into the type `i8` whose range is `-128..=127`
37+
= note: the literal `-129` does not fit into the type `i8` whose range is `-128..=127`
3838
= help: consider using the type `i16` instead
3939

4040
error: literal out of range for `i8`
41-
--> $DIR/lint-type-overflow.rs:19:19
41+
--> $DIR/lint-type-overflow.rs:19:18
4242
|
4343
LL | let x3: i8 = -(129);
44-
| ^^^^^
44+
| ^^^^^^
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`
@@ -74,12 +74,12 @@ LL | let x = 128_i8;
7474
= help: consider using the type `u8` instead
7575

7676
error: literal out of range for `i8`
77-
--> $DIR/lint-type-overflow.rs:28:14
77+
--> $DIR/lint-type-overflow.rs:28:13
7878
|
7979
LL | let x = -129_i8;
80-
| ^^^^^^
80+
| ^^^^^^^
8181
|
82-
= note: the literal `129_i8` does not fit into the type `i8` whose range is `-128..=127`
82+
= note: the literal `-129_i8` 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`
@@ -101,21 +101,21 @@ LL | let x = 2147483648_i32;
101101
= help: consider using the type `u32` instead
102102

103103
error: literal out of range for `i32`
104-
--> $DIR/lint-type-overflow.rs:36:19
104+
--> $DIR/lint-type-overflow.rs:36:18
105105
|
106106
LL | let x: i32 = -2147483649;
107-
| ^^^^^^^^^^
107+
| ^^^^^^^^^^^
108108
|
109-
= note: the literal `2147483649` does not fit into the type `i32` whose range is `-2147483648..=2147483647`
109+
= note: the literal `-2147483649` does not fit into the type `i32` whose range is `-2147483648..=2147483647`
110110
= help: consider using the type `i64` instead
111111

112112
error: literal out of range for `i32`
113-
--> $DIR/lint-type-overflow.rs:37:14
113+
--> $DIR/lint-type-overflow.rs:37:13
114114
|
115115
LL | let x = -2147483649_i32;
116-
| ^^^^^^^^^^^^^^
116+
| ^^^^^^^^^^^^^^^
117117
|
118-
= note: the literal `2147483649_i32` does not fit into the type `i32` whose range is `-2147483648..=2147483647`
118+
= note: the literal `-2147483649_i32` 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`
@@ -146,21 +146,21 @@ LL | let x = 18446744073709551615_i64;
146146
= help: consider using the type `u64` instead
147147

148148
error: literal out of range for `i64`
149-
--> $DIR/lint-type-overflow.rs:43:19
149+
--> $DIR/lint-type-overflow.rs:43:18
150150
|
151151
LL | let x: i64 = -9223372036854775809;
152-
| ^^^^^^^^^^^^^^^^^^^
152+
| ^^^^^^^^^^^^^^^^^^^^
153153
|
154-
= note: the literal `9223372036854775809` does not fit into the type `i64` whose range is `-9223372036854775808..=9223372036854775807`
154+
= note: the literal `-9223372036854775809` does not fit into the type `i64` whose range is `-9223372036854775808..=9223372036854775807`
155155
= help: consider using the type `i128` instead
156156

157157
error: literal out of range for `i64`
158-
--> $DIR/lint-type-overflow.rs:44:14
158+
--> $DIR/lint-type-overflow.rs:44:13
159159
|
160160
LL | let x = -9223372036854775809_i64;
161-
| ^^^^^^^^^^^^^^^^^^^^^^^
161+
| ^^^^^^^^^^^^^^^^^^^^^^^^
162162
|
163-
= note: the literal `9223372036854775809_i64` does not fit into the type `i64` whose range is `-9223372036854775808..=9223372036854775807`
163+
= note: the literal `-9223372036854775809_i64` 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

0 commit comments

Comments
 (0)