mirror of
https://github.com/rust-lang/rust-clippy
synced 2025-02-17 06:28:42 +00:00
Keep sign in int-to-float casts
This commit is contained in:
parent
30f80c3b8c
commit
d46edd9966
4 changed files with 48 additions and 12 deletions
|
@ -1236,6 +1236,13 @@ declare_clippy_lint! {
|
|||
/// let _ = 2i32 as i32;
|
||||
/// let _ = 0.5 as f32;
|
||||
/// ```
|
||||
///
|
||||
/// Better:
|
||||
///
|
||||
/// ```rust
|
||||
/// let _ = 2_i32;
|
||||
/// let _ = 0.5_f32;
|
||||
/// ```
|
||||
pub UNNECESSARY_CAST,
|
||||
complexity,
|
||||
"cast to the same type, e.g., `x as i32` where `x: i32`"
|
||||
|
@ -1612,7 +1619,8 @@ impl<'tcx> LateLintPass<'tcx> for Casts {
|
|||
let to_nbits = fp_ty_mantissa_nbits(cast_to);
|
||||
if from_nbits != 0 && to_nbits != 0 && from_nbits <= to_nbits && num_lit.is_decimal();
|
||||
then {
|
||||
show_unnecessary_cast(cx, expr, num_lit.integer, cast_from, cast_to);
|
||||
let literal_str = if is_unary_neg(ex) { format!("-{}", num_lit.integer) } else { num_lit.integer.into() };
|
||||
show_unnecessary_cast(cx, expr, &literal_str, cast_from, cast_to);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
@ -1624,7 +1632,7 @@ impl<'tcx> LateLintPass<'tcx> for Casts {
|
|||
LitKind::Float(_, LitFloatType::Unsuffixed) if cast_to.is_floating_point() => {
|
||||
show_unnecessary_cast(cx, expr, &literal_str, cast_from, cast_to);
|
||||
},
|
||||
LitKind::Int(_, LitIntType::Unsuffixed) | LitKind::Float(_, LitFloatType::Unsuffixed) => (),
|
||||
LitKind::Int(_, LitIntType::Unsuffixed) | LitKind::Float(_, LitFloatType::Unsuffixed) => {},
|
||||
_ => {
|
||||
if cast_from.kind() == cast_to.kind() && !in_external_macro(cx.sess(), expr.span) {
|
||||
span_lint(
|
||||
|
@ -1649,6 +1657,10 @@ impl<'tcx> LateLintPass<'tcx> for Casts {
|
|||
}
|
||||
}
|
||||
|
||||
fn is_unary_neg(expr: &Expr<'_>) -> bool {
|
||||
matches!(expr.kind, ExprKind::Unary(UnOp::UnNeg, _))
|
||||
}
|
||||
|
||||
fn get_numeric_literal<'e>(expr: &'e Expr<'e>) -> Option<&'e Lit> {
|
||||
match expr.kind {
|
||||
ExprKind::Lit(ref lit) => Some(lit),
|
||||
|
|
|
@ -8,6 +8,9 @@ fn main() {
|
|||
100_f32;
|
||||
100_f64;
|
||||
100_f64;
|
||||
let _ = -100_f32;
|
||||
let _ = -100_f64;
|
||||
let _ = -100_f64;
|
||||
// Should not trigger
|
||||
#[rustfmt::skip]
|
||||
let v = vec!(1);
|
||||
|
|
|
@ -8,6 +8,9 @@ fn main() {
|
|||
100 as f32;
|
||||
100 as f64;
|
||||
100_i32 as f64;
|
||||
let _ = -100 as f32;
|
||||
let _ = -100 as f64;
|
||||
let _ = -100_i32 as f64;
|
||||
// Should not trigger
|
||||
#[rustfmt::skip]
|
||||
let v = vec!(1);
|
||||
|
|
|
@ -18,59 +18,77 @@ error: casting integer literal to `f64` is unnecessary
|
|||
LL | 100_i32 as f64;
|
||||
| ^^^^^^^^^^^^^^ help: try: `100_f64`
|
||||
|
||||
error: casting integer literal to `f32` is unnecessary
|
||||
--> $DIR/unnecessary_cast_fixable.rs:11:13
|
||||
|
|
||||
LL | let _ = -100 as f32;
|
||||
| ^^^^^^^^^^^ help: try: `-100_f32`
|
||||
|
||||
error: casting integer literal to `f64` is unnecessary
|
||||
--> $DIR/unnecessary_cast_fixable.rs:12:13
|
||||
|
|
||||
LL | let _ = -100 as f64;
|
||||
| ^^^^^^^^^^^ help: try: `-100_f64`
|
||||
|
||||
error: casting integer literal to `f64` is unnecessary
|
||||
--> $DIR/unnecessary_cast_fixable.rs:13:13
|
||||
|
|
||||
LL | let _ = -100_i32 as f64;
|
||||
| ^^^^^^^^^^^^^^^ help: try: `-100_f64`
|
||||
|
||||
error: casting integer literal to `u32` is unnecessary
|
||||
--> $DIR/unnecessary_cast_fixable.rs:22:5
|
||||
--> $DIR/unnecessary_cast_fixable.rs:25:5
|
||||
|
|
||||
LL | 1 as u32;
|
||||
| ^^^^^^^^ help: try: `1_u32`
|
||||
|
||||
error: casting integer literal to `i32` is unnecessary
|
||||
--> $DIR/unnecessary_cast_fixable.rs:23:5
|
||||
--> $DIR/unnecessary_cast_fixable.rs:26:5
|
||||
|
|
||||
LL | 0x10 as i32;
|
||||
| ^^^^^^^^^^^ help: try: `0x10_i32`
|
||||
|
||||
error: casting integer literal to `usize` is unnecessary
|
||||
--> $DIR/unnecessary_cast_fixable.rs:24:5
|
||||
--> $DIR/unnecessary_cast_fixable.rs:27:5
|
||||
|
|
||||
LL | 0b10 as usize;
|
||||
| ^^^^^^^^^^^^^ help: try: `0b10_usize`
|
||||
|
||||
error: casting integer literal to `u16` is unnecessary
|
||||
--> $DIR/unnecessary_cast_fixable.rs:25:5
|
||||
--> $DIR/unnecessary_cast_fixable.rs:28:5
|
||||
|
|
||||
LL | 0o73 as u16;
|
||||
| ^^^^^^^^^^^ help: try: `0o73_u16`
|
||||
|
||||
error: casting integer literal to `u32` is unnecessary
|
||||
--> $DIR/unnecessary_cast_fixable.rs:26:5
|
||||
--> $DIR/unnecessary_cast_fixable.rs:29:5
|
||||
|
|
||||
LL | 1_000_000_000 as u32;
|
||||
| ^^^^^^^^^^^^^^^^^^^^ help: try: `1_000_000_000_u32`
|
||||
|
||||
error: casting float literal to `f64` is unnecessary
|
||||
--> $DIR/unnecessary_cast_fixable.rs:28:5
|
||||
--> $DIR/unnecessary_cast_fixable.rs:31:5
|
||||
|
|
||||
LL | 1.0 as f64;
|
||||
| ^^^^^^^^^^ help: try: `1.0_f64`
|
||||
|
||||
error: casting float literal to `f32` is unnecessary
|
||||
--> $DIR/unnecessary_cast_fixable.rs:29:5
|
||||
--> $DIR/unnecessary_cast_fixable.rs:32:5
|
||||
|
|
||||
LL | 0.5 as f32;
|
||||
| ^^^^^^^^^^ help: try: `0.5_f32`
|
||||
|
||||
error: casting integer literal to `i32` is unnecessary
|
||||
--> $DIR/unnecessary_cast_fixable.rs:33:13
|
||||
--> $DIR/unnecessary_cast_fixable.rs:36:13
|
||||
|
|
||||
LL | let _ = -1 as i32;
|
||||
| ^^^^^^^^^ help: try: `-1_i32`
|
||||
|
||||
error: casting float literal to `f32` is unnecessary
|
||||
--> $DIR/unnecessary_cast_fixable.rs:34:13
|
||||
--> $DIR/unnecessary_cast_fixable.rs:37:13
|
||||
|
|
||||
LL | let _ = -1.0 as f32;
|
||||
| ^^^^^^^^^^^ help: try: `-1.0_f32`
|
||||
|
||||
error: aborting due to 12 previous errors
|
||||
error: aborting due to 15 previous errors
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue