Skip to content

Commit 1054eb0

Browse files
committed
use lint_unnecessary_cast for literals, suggest _ if not present
1 parent 6d2236f commit 1054eb0

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

clippy_lints/src/casts/unnecessary_cast.rs

+9
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,15 @@ pub(super) fn check(
4444
lint_unnecessary_cast(cx, expr, &literal_str, cast_from, cast_to);
4545
},
4646
LitKind::Int(_, LitIntType::Unsuffixed) | LitKind::Float(_, LitFloatType::Unsuffixed) => {},
47+
LitKind::Int(_, LitIntType::Signed(_) | LitIntType::Unsigned(_))
48+
| LitKind::Float(_, LitFloatType::Suffixed(_))
49+
if cast_from.kind() == cast_to.kind() =>
50+
{
51+
if let Some(src) = snippet_opt(cx, lit.span) {
52+
let num_lit = NumericLiteral::from_lit_kind(&src, &lit.node).unwrap();
53+
lint_unnecessary_cast(cx, expr, num_lit.integer, cast_from, cast_to);
54+
}
55+
},
4756
_ => {
4857
if cast_from.kind() == cast_to.kind() && !in_external_macro(cx.sess(), expr.span) {
4958
span_lint_and_sugg(

tests/ui/unnecessary_cast.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
error: casting to the same type is unnecessary (`i32` -> `i32`)
1+
error: casting integer literal to `i32` is unnecessary
22
--> $DIR/unnecessary_cast.rs:6:5
33
|
44
LL | 1i32 as i32;
5-
| ^^^^^^^^^^^ help: try: `1i32`
5+
| ^^^^^^^^^^^ help: try: `1_i32`
66
|
77
= note: `-D clippy::unnecessary-cast` implied by `-D warnings`
88

9-
error: casting to the same type is unnecessary (`f32` -> `f32`)
9+
error: casting float literal to `f32` is unnecessary
1010
--> $DIR/unnecessary_cast.rs:7:5
1111
|
1212
LL | 1f32 as f32;
13-
| ^^^^^^^^^^^ help: try: `1f32`
13+
| ^^^^^^^^^^^ help: try: `1_f32`
1414

1515
error: casting to the same type is unnecessary (`bool` -> `bool`)
1616
--> $DIR/unnecessary_cast.rs:8:5

0 commit comments

Comments
 (0)