Lint unnecessary int-to-int and float-to-float casts

This commit is contained in:
Geoffrey Copin 2020-10-16 00:22:35 +02:00
parent febd0086a5
commit bb0ce32423
6 changed files with 111 additions and 41 deletions

View File

@ -3,6 +3,7 @@
use std::borrow::Cow;
use std::cmp::Ordering;
use std::collections::BTreeMap;
use std::fmt::Display;
use if_chain::if_chain;
use rustc_ast::{FloatTy, IntTy, LitFloatType, LitIntType, LitKind, UintTy};
@ -1608,18 +1609,23 @@ 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 {
span_lint_and_sugg(
cx,
UNNECESSARY_CAST,
expr.span,
&format!("casting integer literal to `{}` is unnecessary", cast_to),
"try",
format!("{}_{}", n, cast_to),
Applicability::MachineApplicable,
);
show_unnecessary_cast(cx, expr, n , cast_from, cast_to);
return;
}
}
match lit.node {
LitKind::Int(num, LitIntType::Unsuffixed) if cast_to.is_integral() => {
show_unnecessary_cast(cx, expr, num, cast_from, cast_to);
return;
},
LitKind::Float(num, LitFloatType::Unsuffixed) if cast_to.is_floating_point() => {
show_unnecessary_cast(cx, expr, num, cast_from, cast_to);
return;
},
_ => (),
};
match lit.node {
LitKind::Int(_, LitIntType::Unsuffixed) | LitKind::Float(_, LitFloatType::Unsuffixed) => {},
_ => {
@ -1646,6 +1652,25 @@ impl<'tcx> LateLintPass<'tcx> for Casts {
}
}
fn show_unnecessary_cast<Num: Display>(
cx: &LateContext<'_>,
expr: &Expr<'_>,
num: Num,
cast_from: Ty<'_>,
cast_to: Ty<'_>,
) {
let literal_kind_name = if cast_from.is_integral() { "integer" } else { "float" };
span_lint_and_sugg(
cx,
UNNECESSARY_CAST,
expr.span,
&format!("casting {} literal to `{}` is unnecessary", literal_kind_name, cast_to),
"try",
format!("{}_{}", num, cast_to),
Applicability::MachineApplicable,
);
}
fn lint_numeric_casts<'tcx>(
cx: &LateContext<'tcx>,
expr: &Expr<'tcx>,

View File

@ -6,6 +6,7 @@
#[allow(clippy::no_effect, unused_variables, clippy::unnecessary_operation, clippy::short_circuit_statement)]
#[allow(clippy::nonminimal_bool)]
#[allow(unused)]
#[allow(clippy::unnecessary_cast)]
fn main() {
// simple values and comparisons
1 == 1;

View File

@ -1,5 +1,5 @@
error: equal expressions as operands to `==`
--> $DIR/eq_op.rs:11:5
--> $DIR/eq_op.rs:12:5
|
LL | 1 == 1;
| ^^^^^^
@ -7,157 +7,157 @@ LL | 1 == 1;
= note: `-D clippy::eq-op` implied by `-D warnings`
error: equal expressions as operands to `==`
--> $DIR/eq_op.rs:12:5
--> $DIR/eq_op.rs:13:5
|
LL | "no" == "no";
| ^^^^^^^^^^^^
error: equal expressions as operands to `!=`
--> $DIR/eq_op.rs:14:5
--> $DIR/eq_op.rs:15:5
|
LL | false != false;
| ^^^^^^^^^^^^^^
error: equal expressions as operands to `<`
--> $DIR/eq_op.rs:15:5
--> $DIR/eq_op.rs:16:5
|
LL | 1.5 < 1.5;
| ^^^^^^^^^
error: equal expressions as operands to `>=`
--> $DIR/eq_op.rs:16:5
--> $DIR/eq_op.rs:17:5
|
LL | 1u64 >= 1u64;
| ^^^^^^^^^^^^
error: equal expressions as operands to `&`
--> $DIR/eq_op.rs:19:5
--> $DIR/eq_op.rs:20:5
|
LL | (1 as u64) & (1 as u64);
| ^^^^^^^^^^^^^^^^^^^^^^^
error: equal expressions as operands to `^`
--> $DIR/eq_op.rs:20:5
--> $DIR/eq_op.rs:21:5
|
LL | 1 ^ ((((((1))))));
| ^^^^^^^^^^^^^^^^^
error: equal expressions as operands to `<`
--> $DIR/eq_op.rs:23:5
--> $DIR/eq_op.rs:24:5
|
LL | (-(2) < -(2));
| ^^^^^^^^^^^^^
error: equal expressions as operands to `==`
--> $DIR/eq_op.rs:24:5
--> $DIR/eq_op.rs:25:5
|
LL | ((1 + 1) & (1 + 1) == (1 + 1) & (1 + 1));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: equal expressions as operands to `&`
--> $DIR/eq_op.rs:24:6
--> $DIR/eq_op.rs:25:6
|
LL | ((1 + 1) & (1 + 1) == (1 + 1) & (1 + 1));
| ^^^^^^^^^^^^^^^^^
error: equal expressions as operands to `&`
--> $DIR/eq_op.rs:24:27
--> $DIR/eq_op.rs:25:27
|
LL | ((1 + 1) & (1 + 1) == (1 + 1) & (1 + 1));
| ^^^^^^^^^^^^^^^^^
error: equal expressions as operands to `==`
--> $DIR/eq_op.rs:25:5
--> $DIR/eq_op.rs:26:5
|
LL | (1 * 2) + (3 * 4) == 1 * 2 + 3 * 4;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: equal expressions as operands to `!=`
--> $DIR/eq_op.rs:28:5
--> $DIR/eq_op.rs:29:5
|
LL | ([1] != [1]);
| ^^^^^^^^^^^^
error: equal expressions as operands to `!=`
--> $DIR/eq_op.rs:29:5
--> $DIR/eq_op.rs:30:5
|
LL | ((1, 2) != (1, 2));
| ^^^^^^^^^^^^^^^^^^
error: equal expressions as operands to `==`
--> $DIR/eq_op.rs:33:5
--> $DIR/eq_op.rs:34:5
|
LL | 1 + 1 == 2;
| ^^^^^^^^^^
error: equal expressions as operands to `==`
--> $DIR/eq_op.rs:34:5
--> $DIR/eq_op.rs:35:5
|
LL | 1 - 1 == 0;
| ^^^^^^^^^^
error: equal expressions as operands to `-`
--> $DIR/eq_op.rs:34:5
--> $DIR/eq_op.rs:35:5
|
LL | 1 - 1 == 0;
| ^^^^^
error: equal expressions as operands to `-`
--> $DIR/eq_op.rs:36:5
--> $DIR/eq_op.rs:37:5
|
LL | 1 - 1;
| ^^^^^
error: equal expressions as operands to `/`
--> $DIR/eq_op.rs:37:5
--> $DIR/eq_op.rs:38:5
|
LL | 1 / 1;
| ^^^^^
error: equal expressions as operands to `&&`
--> $DIR/eq_op.rs:38:5
--> $DIR/eq_op.rs:39:5
|
LL | true && true;
| ^^^^^^^^^^^^
error: equal expressions as operands to `||`
--> $DIR/eq_op.rs:40:5
--> $DIR/eq_op.rs:41:5
|
LL | true || true;
| ^^^^^^^^^^^^
error: equal expressions as operands to `&&`
--> $DIR/eq_op.rs:46:5
--> $DIR/eq_op.rs:47:5
|
LL | a == b && b == a;
| ^^^^^^^^^^^^^^^^
error: equal expressions as operands to `&&`
--> $DIR/eq_op.rs:47:5
--> $DIR/eq_op.rs:48:5
|
LL | a != b && b != a;
| ^^^^^^^^^^^^^^^^
error: equal expressions as operands to `&&`
--> $DIR/eq_op.rs:48:5
--> $DIR/eq_op.rs:49:5
|
LL | a < b && b > a;
| ^^^^^^^^^^^^^^
error: equal expressions as operands to `&&`
--> $DIR/eq_op.rs:49:5
--> $DIR/eq_op.rs:50:5
|
LL | a <= b && b >= a;
| ^^^^^^^^^^^^^^^^
error: equal expressions as operands to `==`
--> $DIR/eq_op.rs:52:5
--> $DIR/eq_op.rs:53:5
|
LL | a == a;
| ^^^^^^
error: equal expressions as operands to `/`
--> $DIR/eq_op.rs:62:20
--> $DIR/eq_op.rs:63:20
|
LL | const D: u32 = A / A;
| ^^^^^

View File

@ -12,12 +12,19 @@ fn main() {
#[rustfmt::skip]
let v = vec!(1);
&v as &[i32];
1.0 as f64;
1 as u64;
0x10 as f32;
0o10 as f32;
0b10 as f32;
0x11 as f64;
0o11 as f64;
0b11 as f64;
1_u32;
16_i32;
2_usize;
1.0_f64;
0.5_f32;
1.0 as u16;
}

View File

@ -12,12 +12,19 @@ fn main() {
#[rustfmt::skip]
let v = vec!(1);
&v as &[i32];
1.0 as f64;
1 as u64;
0x10 as f32;
0o10 as f32;
0b10 as f32;
0x11 as f64;
0o11 as f64;
0b11 as f64;
1 as u32;
0x10 as i32;
0b10 as usize;
1.0 as f64;
0.5 as f32;
1.0 as u16;
}

View File

@ -18,5 +18,35 @@ error: casting integer literal to `f64` is unnecessary
LL | 100_i32 as f64;
| ^^^^^^^^^^^^^^ help: try: `100_f64`
error: aborting due to 3 previous errors
error: casting integer literal to `u32` is unnecessary
--> $DIR/unnecessary_cast_fixable.rs:22:5
|
LL | 1 as u32;
| ^^^^^^^^ help: try: `1_u32`
error: casting integer literal to `i32` is unnecessary
--> $DIR/unnecessary_cast_fixable.rs:23:5
|
LL | 0x10 as i32;
| ^^^^^^^^^^^ help: try: `16_i32`
error: casting integer literal to `usize` is unnecessary
--> $DIR/unnecessary_cast_fixable.rs:24:5
|
LL | 0b10 as usize;
| ^^^^^^^^^^^^^ help: try: `2_usize`
error: casting float literal to `f64` is unnecessary
--> $DIR/unnecessary_cast_fixable.rs:26:5
|
LL | 1.0 as f64;
| ^^^^^^^^^^ help: try: `1.0_f64`
error: casting float literal to `f32` is unnecessary
--> $DIR/unnecessary_cast_fixable.rs:27:5
|
LL | 0.5 as f32;
| ^^^^^^^^^^ help: try: `0.5_f32`
error: aborting due to 8 previous errors