about summary refs log tree commit diff
diff options
context:
space:
mode:
authorkraktus <kraktus@users.noreply.github.com>2022-10-08 16:15:18 +0200
committerkraktus <kraktus@users.noreply.github.com>2022-10-08 16:15:18 +0200
commit6f4546a4be7b65da15b3170d72d3339ed1d2aeec (patch)
tree572db4453dcf52035bf68e25f7d434383a7ec6fe
parent2c8e473ffea22fdf9ffbecb53c1c7288d38fe923 (diff)
downloadrust-6f4546a4be7b65da15b3170d72d3339ed1d2aeec.tar.gz
rust-6f4546a4be7b65da15b3170d72d3339ed1d2aeec.zip
[`unnecessary_cast`] Do not lint negative hexadecimal literals when cast as float
Floats cannot be expressed as hexadecimal literals
-rw-r--r--clippy_lints/src/casts/unnecessary_cast.rs3
-rw-r--r--clippy_utils/src/numeric_literal.rs7
-rw-r--r--tests/ui/unnecessary_cast.fixed4
-rw-r--r--tests/ui/unnecessary_cast.rs4
4 files changed, 12 insertions, 6 deletions
diff --git a/clippy_lints/src/casts/unnecessary_cast.rs b/clippy_lints/src/casts/unnecessary_cast.rs
index 21ed7f4844c..c8596987e4d 100644
--- a/clippy_lints/src/casts/unnecessary_cast.rs
+++ b/clippy_lints/src/casts/unnecessary_cast.rs
@@ -59,9 +59,6 @@ pub(super) fn check<'tcx>(
                 lint_unnecessary_cast(cx, expr, literal_str, cast_from, cast_to);
                 return false;
             },
-            LitKind::Int(_, LitIntType::Unsuffixed) | LitKind::Float(_, LitFloatType::Unsuffixed) => {
-                return false;
-            },
             LitKind::Int(_, LitIntType::Signed(_) | LitIntType::Unsigned(_))
             | LitKind::Float(_, LitFloatType::Suffixed(_))
                 if cast_from.kind() == cast_to.kind() =>
diff --git a/clippy_utils/src/numeric_literal.rs b/clippy_utils/src/numeric_literal.rs
index 80098d9766c..c5dcd7b31f5 100644
--- a/clippy_utils/src/numeric_literal.rs
+++ b/clippy_utils/src/numeric_literal.rs
@@ -69,12 +69,13 @@ impl<'a> NumericLiteral<'a> {
 
     #[must_use]
     pub fn new(lit: &'a str, suffix: Option<&'a str>, float: bool) -> Self {
+        let unsigned_lit = lit.trim_start_matches('-');
         // Determine delimiter for radix prefix, if present, and radix.
-        let radix = if lit.starts_with("0x") {
+        let radix = if unsigned_lit.starts_with("0x") {
             Radix::Hexadecimal
-        } else if lit.starts_with("0b") {
+        } else if unsigned_lit.starts_with("0b") {
             Radix::Binary
-        } else if lit.starts_with("0o") {
+        } else if unsigned_lit.starts_with("0o") {
             Radix::Octal
         } else {
             Radix::Decimal
diff --git a/tests/ui/unnecessary_cast.fixed b/tests/ui/unnecessary_cast.fixed
index 94dc9642726..ec8c6abfab9 100644
--- a/tests/ui/unnecessary_cast.fixed
+++ b/tests/ui/unnecessary_cast.fixed
@@ -111,4 +111,8 @@ mod fixable {
 
         let _num = foo();
     }
+
+    fn issue_9603() {
+        let _: f32 = -0x400 as f32;
+    }
 }
diff --git a/tests/ui/unnecessary_cast.rs b/tests/ui/unnecessary_cast.rs
index e5150256f69..5213cdc269b 100644
--- a/tests/ui/unnecessary_cast.rs
+++ b/tests/ui/unnecessary_cast.rs
@@ -111,4 +111,8 @@ mod fixable {
 
         let _num = foo() as f32;
     }
+
+    fn issue_9603() {
+        let _: f32 = -0x400 as f32;
+    }
 }