Skip to content

Commit e086cbb

Browse files
committed
Keep sign in int-to-float casts
1 parent 30f80c3 commit e086cbb

File tree

4 files changed

+51
-12
lines changed

4 files changed

+51
-12
lines changed

clippy_lints/src/types.rs

+17-2
Original file line numberDiff line numberDiff line change
@@ -1236,6 +1236,13 @@ declare_clippy_lint! {
12361236
/// let _ = 2i32 as i32;
12371237
/// let _ = 0.5 as f32;
12381238
/// ```
1239+
///
1240+
/// Better:
1241+
///
1242+
/// ```rust
1243+
/// let _ = 2_i32;
1244+
/// let _ = 0.5_f32;
1245+
/// ```
12391246
pub UNNECESSARY_CAST,
12401247
complexity,
12411248
"cast to the same type, e.g., `x as i32` where `x: i32`"
@@ -1612,7 +1619,8 @@ impl<'tcx> LateLintPass<'tcx> for Casts {
16121619
let to_nbits = fp_ty_mantissa_nbits(cast_to);
16131620
if from_nbits != 0 && to_nbits != 0 && from_nbits <= to_nbits && num_lit.is_decimal();
16141621
then {
1615-
show_unnecessary_cast(cx, expr, num_lit.integer, cast_from, cast_to);
1622+
let literal_str = if is_unary_neg(ex) { format!("-{}", num_lit.integer) } else { num_lit.integer.into() };
1623+
show_unnecessary_cast(cx, expr, &literal_str, cast_from, cast_to);
16161624
return;
16171625
}
16181626
}
@@ -1624,7 +1632,7 @@ impl<'tcx> LateLintPass<'tcx> for Casts {
16241632
LitKind::Float(_, LitFloatType::Unsuffixed) if cast_to.is_floating_point() => {
16251633
show_unnecessary_cast(cx, expr, &literal_str, cast_from, cast_to);
16261634
},
1627-
LitKind::Int(_, LitIntType::Unsuffixed) | LitKind::Float(_, LitFloatType::Unsuffixed) => (),
1635+
LitKind::Int(_, LitIntType::Unsuffixed) | LitKind::Float(_, LitFloatType::Unsuffixed) => {},
16281636
_ => {
16291637
if cast_from.kind() == cast_to.kind() && !in_external_macro(cx.sess(), expr.span) {
16301638
span_lint(
@@ -1649,6 +1657,13 @@ impl<'tcx> LateLintPass<'tcx> for Casts {
16491657
}
16501658
}
16511659

1660+
fn is_unary_neg(expr: &Expr<'_>) -> bool {
1661+
match expr.kind {
1662+
ExprKind::Unary(UnOp::UnNeg, _) => true,
1663+
_ => false,
1664+
}
1665+
}
1666+
16521667
fn get_numeric_literal<'e>(expr: &'e Expr<'e>) -> Option<&'e Lit> {
16531668
match expr.kind {
16541669
ExprKind::Lit(ref lit) => Some(lit),

tests/ui/unnecessary_cast_fixable.fixed

+3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ fn main() {
88
100_f32;
99
100_f64;
1010
100_f64;
11+
let _ = -100_f32;
12+
let _ = -100_f64;
13+
let _ = -100_f64;
1114
// Should not trigger
1215
#[rustfmt::skip]
1316
let v = vec!(1);

tests/ui/unnecessary_cast_fixable.rs

+3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ fn main() {
88
100 as f32;
99
100 as f64;
1010
100_i32 as f64;
11+
let _ = -100 as f32;
12+
let _ = -100 as f64;
13+
let _ = -100_i32 as f64;
1114
// Should not trigger
1215
#[rustfmt::skip]
1316
let v = vec!(1);

tests/ui/unnecessary_cast_fixable.stderr

+28-10
Original file line numberDiff line numberDiff line change
@@ -18,59 +18,77 @@ error: casting integer literal to `f64` is unnecessary
1818
LL | 100_i32 as f64;
1919
| ^^^^^^^^^^^^^^ help: try: `100_f64`
2020

21+
error: casting integer literal to `f32` is unnecessary
22+
--> $DIR/unnecessary_cast_fixable.rs:11:13
23+
|
24+
LL | let _ = -100 as f32;
25+
| ^^^^^^^^^^^ help: try: `-100_f32`
26+
27+
error: casting integer literal to `f64` is unnecessary
28+
--> $DIR/unnecessary_cast_fixable.rs:12:13
29+
|
30+
LL | let _ = -100 as f64;
31+
| ^^^^^^^^^^^ help: try: `-100_f64`
32+
33+
error: casting integer literal to `f64` is unnecessary
34+
--> $DIR/unnecessary_cast_fixable.rs:13:13
35+
|
36+
LL | let _ = -100_i32 as f64;
37+
| ^^^^^^^^^^^^^^^ help: try: `-100_f64`
38+
2139
error: casting integer literal to `u32` is unnecessary
22-
--> $DIR/unnecessary_cast_fixable.rs:22:5
40+
--> $DIR/unnecessary_cast_fixable.rs:25:5
2341
|
2442
LL | 1 as u32;
2543
| ^^^^^^^^ help: try: `1_u32`
2644

2745
error: casting integer literal to `i32` is unnecessary
28-
--> $DIR/unnecessary_cast_fixable.rs:23:5
46+
--> $DIR/unnecessary_cast_fixable.rs:26:5
2947
|
3048
LL | 0x10 as i32;
3149
| ^^^^^^^^^^^ help: try: `0x10_i32`
3250

3351
error: casting integer literal to `usize` is unnecessary
34-
--> $DIR/unnecessary_cast_fixable.rs:24:5
52+
--> $DIR/unnecessary_cast_fixable.rs:27:5
3553
|
3654
LL | 0b10 as usize;
3755
| ^^^^^^^^^^^^^ help: try: `0b10_usize`
3856

3957
error: casting integer literal to `u16` is unnecessary
40-
--> $DIR/unnecessary_cast_fixable.rs:25:5
58+
--> $DIR/unnecessary_cast_fixable.rs:28:5
4159
|
4260
LL | 0o73 as u16;
4361
| ^^^^^^^^^^^ help: try: `0o73_u16`
4462

4563
error: casting integer literal to `u32` is unnecessary
46-
--> $DIR/unnecessary_cast_fixable.rs:26:5
64+
--> $DIR/unnecessary_cast_fixable.rs:29:5
4765
|
4866
LL | 1_000_000_000 as u32;
4967
| ^^^^^^^^^^^^^^^^^^^^ help: try: `1_000_000_000_u32`
5068

5169
error: casting float literal to `f64` is unnecessary
52-
--> $DIR/unnecessary_cast_fixable.rs:28:5
70+
--> $DIR/unnecessary_cast_fixable.rs:31:5
5371
|
5472
LL | 1.0 as f64;
5573
| ^^^^^^^^^^ help: try: `1.0_f64`
5674

5775
error: casting float literal to `f32` is unnecessary
58-
--> $DIR/unnecessary_cast_fixable.rs:29:5
76+
--> $DIR/unnecessary_cast_fixable.rs:32:5
5977
|
6078
LL | 0.5 as f32;
6179
| ^^^^^^^^^^ help: try: `0.5_f32`
6280

6381
error: casting integer literal to `i32` is unnecessary
64-
--> $DIR/unnecessary_cast_fixable.rs:33:13
82+
--> $DIR/unnecessary_cast_fixable.rs:36:13
6583
|
6684
LL | let _ = -1 as i32;
6785
| ^^^^^^^^^ help: try: `-1_i32`
6886

6987
error: casting float literal to `f32` is unnecessary
70-
--> $DIR/unnecessary_cast_fixable.rs:34:13
88+
--> $DIR/unnecessary_cast_fixable.rs:37:13
7189
|
7290
LL | let _ = -1.0 as f32;
7391
| ^^^^^^^^^^^ help: try: `-1.0_f32`
7492

75-
error: aborting due to 12 previous errors
93+
error: aborting due to 15 previous errors
7694

0 commit comments

Comments
 (0)