]> git.lizzy.rs Git - rust.git/commitdiff
Fix ICE in decimal_literal_representation lint
authorPhilipp Hansch <dev@phansch.net>
Mon, 8 Apr 2019 20:06:02 +0000 (22:06 +0200)
committerPhilipp Hansch <dev@phansch.net>
Mon, 8 Apr 2019 20:16:34 +0000 (22:16 +0200)
Handling the integer parsing properly instead of just unwrapping.

Note that the test is not catching the ICE because plain UI tests
[currently hide ICEs][compiletest_issue]. Once that issue is fixed, this
test would fail properly again.

[compiletest_issue]: https://github.com/laumann/compiletest-rs/issues/169

clippy_lints/src/literal_representation.rs
tests/ui/crashes/ice-3891.rs [new file with mode: 0644]
tests/ui/crashes/ice-3891.stderr [new file with mode: 0644]

index 5c2ca4b1c9358115db095347bf34f3e2747e6625..3ea818d3db604440ad6a6751dfaa53e896925932 100644 (file)
@@ -529,19 +529,23 @@ fn check_lit(self, cx: &EarlyContext<'_>, lit: &Lit) {
             then {
                 let digit_info = DigitInfo::new(&src, false);
                 if digit_info.radix == Radix::Decimal {
-                    let val = digit_info.digits
+                    if let Ok(val) = digit_info.digits
                         .chars()
                         .filter(|&c| c != '_')
                         .collect::<String>()
-                        .parse::<u128>().unwrap();
-                    if val < u128::from(self.threshold) {
-                        return
+                        .parse::<u128>() {
+                        if val < u128::from(self.threshold) {
+                            return
+                        }
+                        let hex = format!("{:#X}", val);
+                        let digit_info = DigitInfo::new(&hex[..], false);
+                        let _ = Self::do_lint(digit_info.digits).map_err(|warning_type| {
+                            warning_type.display(&digit_info.grouping_hint(), cx, lit.span)
+                        });
                     }
-                    let hex = format!("{:#X}", val);
-                    let digit_info = DigitInfo::new(&hex[..], false);
-                    let _ = Self::do_lint(digit_info.digits).map_err(|warning_type| {
-                        warning_type.display(&digit_info.grouping_hint(), cx, lit.span)
-                    });
+                    else {
+                        return
+                    };
                 }
             }
         }
diff --git a/tests/ui/crashes/ice-3891.rs b/tests/ui/crashes/ice-3891.rs
new file mode 100644 (file)
index 0000000..05c5134
--- /dev/null
@@ -0,0 +1,3 @@
+fn main() {
+    1x;
+}
diff --git a/tests/ui/crashes/ice-3891.stderr b/tests/ui/crashes/ice-3891.stderr
new file mode 100644 (file)
index 0000000..16aedbd
--- /dev/null
@@ -0,0 +1,10 @@
+error: invalid suffix `x` for numeric literal
+  --> $DIR/ice-3891.rs:2:5
+   |
+LL |     1x;
+   |     ^^ invalid suffix `x`
+   |
+   = help: the suffix must be one of the integral types (`u32`, `isize`, etc)
+
+error: aborting due to previous error
+