about summary refs log tree commit diff
diff options
context:
space:
mode:
authorNadrieril <nadrieril+git@gmail.com>2023-10-11 03:28:49 +0200
committerNadrieril <nadrieril+git@gmail.com>2023-10-11 04:55:55 +0200
commitdcdddb7a60db996db0627a8b6a7d543213123dc4 (patch)
tree480719ed150dc4110888b24bb9f1dc977fbf2ed5
parent1baf8bf54d55674e1026fba50d9385512b8bdd8e (diff)
downloadrust-dcdddb7a60db996db0627a8b6a7d543213123dc4.tar.gz
rust-dcdddb7a60db996db0627a8b6a7d543213123dc4.zip
Fix span of overflow lint for negated literals
-rw-r--r--compiler/rustc_lint/src/types.rs17
-rw-r--r--tests/ui/lint/lint-type-overflow.stderr42
2 files changed, 30 insertions, 29 deletions
diff --git a/compiler/rustc_lint/src/types.rs b/compiler/rustc_lint/src/types.rs
index 44cf1591c7d..d252f651c78 100644
--- a/compiler/rustc_lint/src/types.rs
+++ b/compiler/rustc_lint/src/types.rs
@@ -140,13 +140,15 @@ declare_lint! {
 pub struct TypeLimits {
     /// Id of the last visited negated expression
     negated_expr_id: Option<hir::HirId>,
+    /// Span of the last visited negated expression
+    negated_expr_span: Option<Span>,
 }
 
 impl_lint_pass!(TypeLimits => [UNUSED_COMPARISONS, OVERFLOWING_LITERALS, INVALID_NAN_COMPARISONS]);
 
 impl TypeLimits {
     pub fn new() -> TypeLimits {
-        TypeLimits { negated_expr_id: None }
+        TypeLimits { negated_expr_id: None, negated_expr_span: None }
     }
 }
 
@@ -426,17 +428,15 @@ fn lint_int_literal<'tcx>(
             return;
         }
 
-        let lit = cx
-            .sess()
-            .source_map()
-            .span_to_snippet(lit.span)
-            .expect("must get snippet from literal");
+        let span = if negative { type_limits.negated_expr_span.unwrap() } else { e.span };
+        let lit =
+            cx.sess().source_map().span_to_snippet(span).expect("must get snippet from literal");
         let help = get_type_suggestion(cx.typeck_results().node_type(e.hir_id), v, negative)
             .map(|suggestion_ty| OverflowingIntHelp { suggestion_ty });
 
         cx.emit_spanned_lint(
             OVERFLOWING_LITERALS,
-            e.span,
+            span,
             OverflowingInt { ty: t.name_str(), lit, min, max, help },
         );
     }
@@ -622,9 +622,10 @@ impl<'tcx> LateLintPass<'tcx> for TypeLimits {
     fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx hir::Expr<'tcx>) {
         match e.kind {
             hir::ExprKind::Unary(hir::UnOp::Neg, ref expr) => {
-                // propagate negation, if the negation itself isn't negated
+                // Propagate negation, if the negation itself isn't negated
                 if self.negated_expr_id != Some(e.hir_id) {
                     self.negated_expr_id = Some(expr.hir_id);
+                    self.negated_expr_span = Some(e.span);
                 }
             }
             hir::ExprKind::Binary(binop, ref l, ref r) => {
diff --git a/tests/ui/lint/lint-type-overflow.stderr b/tests/ui/lint/lint-type-overflow.stderr
index 48d8228b802..971c3eb9b2e 100644
--- a/tests/ui/lint/lint-type-overflow.stderr
+++ b/tests/ui/lint/lint-type-overflow.stderr
@@ -29,21 +29,21 @@ LL |     let x1: i8 = 128;
    = help: consider using the type `u8` instead
 
 error: literal out of range for `i8`
-  --> $DIR/lint-type-overflow.rs:18:19
+  --> $DIR/lint-type-overflow.rs:18:18
    |
 LL |     let x3: i8 = -129;
-   |                   ^^^
+   |                  ^^^^
    |
-   = note: the literal `129` does not fit into the type `i8` whose range is `-128..=127`
+   = note: the literal `-129` does not fit into the type `i8` whose range is `-128..=127`
    = help: consider using the type `i16` instead
 
 error: literal out of range for `i8`
-  --> $DIR/lint-type-overflow.rs:19:19
+  --> $DIR/lint-type-overflow.rs:19:18
    |
 LL |     let x3: i8 = -(129);
-   |                   ^^^^^
+   |                  ^^^^^^
    |
-   = note: the literal `129` does not fit into the type `i8` whose range is `-128..=127`
+   = note: the literal `-(129)` does not fit into the type `i8` whose range is `-128..=127`
    = help: consider using the type `i16` instead
 
 error: literal out of range for `i8`
@@ -74,12 +74,12 @@ LL |     let x = 128_i8;
    = help: consider using the type `u8` instead
 
 error: literal out of range for `i8`
-  --> $DIR/lint-type-overflow.rs:28:14
+  --> $DIR/lint-type-overflow.rs:28:13
    |
 LL |     let x = -129_i8;
-   |              ^^^^^^
+   |             ^^^^^^^
    |
-   = note: the literal `129_i8` does not fit into the type `i8` whose range is `-128..=127`
+   = note: the literal `-129_i8` does not fit into the type `i8` whose range is `-128..=127`
    = help: consider using the type `i16` instead
 
 error: literal out of range for `i32`
@@ -101,21 +101,21 @@ LL |     let x = 2147483648_i32;
    = help: consider using the type `u32` instead
 
 error: literal out of range for `i32`
-  --> $DIR/lint-type-overflow.rs:36:19
+  --> $DIR/lint-type-overflow.rs:36:18
    |
 LL |     let x: i32 = -2147483649;
-   |                   ^^^^^^^^^^
+   |                  ^^^^^^^^^^^
    |
-   = note: the literal `2147483649` does not fit into the type `i32` whose range is `-2147483648..=2147483647`
+   = note: the literal `-2147483649` does not fit into the type `i32` whose range is `-2147483648..=2147483647`
    = help: consider using the type `i64` instead
 
 error: literal out of range for `i32`
-  --> $DIR/lint-type-overflow.rs:37:14
+  --> $DIR/lint-type-overflow.rs:37:13
    |
 LL |     let x = -2147483649_i32;
-   |              ^^^^^^^^^^^^^^
+   |             ^^^^^^^^^^^^^^^
    |
-   = note: the literal `2147483649_i32` does not fit into the type `i32` whose range is `-2147483648..=2147483647`
+   = note: the literal `-2147483649_i32` does not fit into the type `i32` whose range is `-2147483648..=2147483647`
    = help: consider using the type `i64` instead
 
 error: literal out of range for `i32`
@@ -146,21 +146,21 @@ LL |     let x = 18446744073709551615_i64;
    = help: consider using the type `u64` instead
 
 error: literal out of range for `i64`
-  --> $DIR/lint-type-overflow.rs:43:19
+  --> $DIR/lint-type-overflow.rs:43:18
    |
 LL |     let x: i64 = -9223372036854775809;
-   |                   ^^^^^^^^^^^^^^^^^^^
+   |                  ^^^^^^^^^^^^^^^^^^^^
    |
-   = note: the literal `9223372036854775809` does not fit into the type `i64` whose range is `-9223372036854775808..=9223372036854775807`
+   = note: the literal `-9223372036854775809` does not fit into the type `i64` whose range is `-9223372036854775808..=9223372036854775807`
    = help: consider using the type `i128` instead
 
 error: literal out of range for `i64`
-  --> $DIR/lint-type-overflow.rs:44:14
+  --> $DIR/lint-type-overflow.rs:44:13
    |
 LL |     let x = -9223372036854775809_i64;
-   |              ^^^^^^^^^^^^^^^^^^^^^^^
+   |             ^^^^^^^^^^^^^^^^^^^^^^^^
    |
-   = note: the literal `9223372036854775809_i64` does not fit into the type `i64` whose range is `-9223372036854775808..=9223372036854775807`
+   = note: the literal `-9223372036854775809_i64` does not fit into the type `i64` whose range is `-9223372036854775808..=9223372036854775807`
    = help: consider using the type `i128` instead
 
 error: aborting due to 18 previous errors