Skip to content

Commit 52c25e9

Browse files
committed
Auto merge of rust-lang#6895 - iobtl:reformat_unnecessary_cast, r=llogiq
replace span_lint with span_lint_and_sugg along with error message fixes: rust-lang#6874 changelog: none apologies if this may not be the most idiomatic way of doing it, any advice on changes (if any) would be greatly appreciated.
2 parents 781de34 + 1054eb0 commit 52c25e9

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

clippy_lints/src/casts/unnecessary_cast.rs

+14-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc_middle::ty::{self, FloatTy, InferTy, Ty};
77

88
use if_chain::if_chain;
99

10-
use crate::utils::{numeric_literal::NumericLiteral, snippet_opt, span_lint, span_lint_and_sugg};
10+
use crate::utils::{numeric_literal::NumericLiteral, snippet_opt, span_lint_and_sugg};
1111

1212
use super::UNNECESSARY_CAST;
1313

@@ -44,16 +44,28 @@ 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) {
49-
span_lint(
58+
span_lint_and_sugg(
5059
cx,
5160
UNNECESSARY_CAST,
5261
expr.span,
5362
&format!(
5463
"casting to the same type is unnecessary (`{}` -> `{}`)",
5564
cast_from, cast_to
5665
),
66+
"try",
67+
literal_str,
68+
Applicability::MachineApplicable,
5769
);
5870
return true;
5971
}

tests/ui/unnecessary_cast.stderr

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
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-
| ^^^^^^^^^^^
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-
| ^^^^^^^^^^^
13+
| ^^^^^^^^^^^ help: try: `1_f32`
1414

1515
error: casting to the same type is unnecessary (`bool` -> `bool`)
1616
--> $DIR/unnecessary_cast.rs:8:5
1717
|
1818
LL | false as bool;
19-
| ^^^^^^^^^^^^^
19+
| ^^^^^^^^^^^^^ help: try: `false`
2020

2121
error: aborting due to 3 previous errors
2222

0 commit comments

Comments
 (0)