Skip to content

Commit 4eaf543

Browse files
committed
Auto merge of #9609 - kraktus:hexa_f32, r=giraffate
[`unnecessary_cast`] Do not lint negative hexadecimal literals when cast as floats fix rust-lang/rust-clippy#9603 changelog: [`unnecessary_cast`] Do not lint negative hexadecimal literals when cast as floats
2 parents 34142fd + 6f4546a commit 4eaf543

File tree

4 files changed

+12
-6
lines changed

4 files changed

+12
-6
lines changed

clippy_lints/src/casts/unnecessary_cast.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,6 @@ pub(super) fn check<'tcx>(
5959
lint_unnecessary_cast(cx, expr, literal_str, cast_from, cast_to);
6060
return false;
6161
},
62-
LitKind::Int(_, LitIntType::Unsuffixed) | LitKind::Float(_, LitFloatType::Unsuffixed) => {
63-
return false;
64-
},
6562
LitKind::Int(_, LitIntType::Signed(_) | LitIntType::Unsigned(_))
6663
| LitKind::Float(_, LitFloatType::Suffixed(_))
6764
if cast_from.kind() == cast_to.kind() =>

clippy_utils/src/numeric_literal.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,13 @@ impl<'a> NumericLiteral<'a> {
6969

7070
#[must_use]
7171
pub fn new(lit: &'a str, suffix: Option<&'a str>, float: bool) -> Self {
72+
let unsigned_lit = lit.trim_start_matches('-');
7273
// Determine delimiter for radix prefix, if present, and radix.
73-
let radix = if lit.starts_with("0x") {
74+
let radix = if unsigned_lit.starts_with("0x") {
7475
Radix::Hexadecimal
75-
} else if lit.starts_with("0b") {
76+
} else if unsigned_lit.starts_with("0b") {
7677
Radix::Binary
77-
} else if lit.starts_with("0o") {
78+
} else if unsigned_lit.starts_with("0o") {
7879
Radix::Octal
7980
} else {
8081
Radix::Decimal

tests/ui/unnecessary_cast.fixed

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,8 @@ mod fixable {
111111

112112
let _num = foo();
113113
}
114+
115+
fn issue_9603() {
116+
let _: f32 = -0x400 as f32;
117+
}
114118
}

tests/ui/unnecessary_cast.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,8 @@ mod fixable {
111111

112112
let _num = foo() as f32;
113113
}
114+
115+
fn issue_9603() {
116+
let _: f32 = -0x400 as f32;
117+
}
114118
}

0 commit comments

Comments
 (0)