Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 6 commits into from
Oct 24, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 34 additions & 9 deletions clippy_lints/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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 {
ebroto marked this conversation as resolved.
Show resolved Hide resolved
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) => {},
_ => {
Expand All @@ -1646,6 +1652,25 @@ impl<'tcx> LateLintPass<'tcx> for Casts {
}
}

fn show_unnecessary_cast<Num: Display>(
cx: &LateContext<'_>,
expr: &Expr<'_>,
num: Num,
ebroto marked this conversation as resolved.
Show resolved Hide resolved
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>,
Expand Down
1 change: 1 addition & 0 deletions tests/ui/eq_op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
54 changes: 27 additions & 27 deletions tests/ui/eq_op.stderr
Original file line number Diff line number Diff line change
@@ -1,163 +1,163 @@
error: equal expressions as operands to `==`
--> $DIR/eq_op.rs:11:5
--> $DIR/eq_op.rs:12:5
|
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;
| ^^^^^
Expand Down
11 changes: 9 additions & 2 deletions tests/ui/unnecessary_cast_fixable.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
11 changes: 9 additions & 2 deletions tests/ui/unnecessary_cast_fixable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

ebroto marked this conversation as resolved.
Show resolved Hide resolved
ebroto marked this conversation as resolved.
Show resolved Hide resolved
1.0 as f64;
0.5 as f32;

1.0 as u16;
}
32 changes: 31 additions & 1 deletion tests/ui/unnecessary_cast_fixable.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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`
ebroto marked this conversation as resolved.
Show resolved Hide resolved

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