Skip to content

Commit bff811b

Browse files
committed
extract common codes
Signed-off-by: TennyZhuang <[email protected]>
1 parent f204398 commit bff811b

File tree

1 file changed

+10
-18
lines changed

1 file changed

+10
-18
lines changed

clippy_lints/src/casts/unnecessary_cast.rs

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ pub(super) fn check<'tcx>(
3434
let cast_str = snippet_opt(cx, cast_expr.span).unwrap_or_default();
3535

3636
if let Some(lit) = get_numeric_literal(cast_expr) {
37-
let literal_str = cast_str;
37+
let literal_str = &cast_str;
3838

3939
if_chain! {
4040
if let LitKind::Int(n, _) = lit.node;
@@ -52,10 +52,12 @@ pub(super) fn check<'tcx>(
5252

5353
match lit.node {
5454
LitKind::Int(_, LitIntType::Unsuffixed) if cast_to.is_integral() => {
55-
lint_unnecessary_cast(cx, expr, &literal_str, cast_from, cast_to);
55+
lint_unnecessary_cast(cx, expr, literal_str, cast_from, cast_to);
56+
return true;
5657
},
5758
LitKind::Float(_, LitFloatType::Unsuffixed) if cast_to.is_floating_point() => {
58-
lint_unnecessary_cast(cx, expr, &literal_str, cast_from, cast_to);
59+
lint_unnecessary_cast(cx, expr, literal_str, cast_from, cast_to);
60+
return true;
5961
},
6062
LitKind::Int(_, LitIntType::Unsuffixed) | LitKind::Float(_, LitFloatType::Unsuffixed) => {},
6163
LitKind::Int(_, LitIntType::Signed(_) | LitIntType::Unsigned(_))
@@ -65,25 +67,15 @@ pub(super) fn check<'tcx>(
6567
if let Some(src) = snippet_opt(cx, cast_expr.span) {
6668
if let Some(num_lit) = NumericLiteral::from_lit_kind(&src, &lit.node) {
6769
lint_unnecessary_cast(cx, expr, num_lit.integer, cast_from, cast_to);
70+
return true;
6871
}
6972
}
7073
},
71-
_ => {
72-
if cast_from.kind() == cast_to.kind() && !in_external_macro(cx.sess(), expr.span) {
73-
span_lint_and_sugg(
74-
cx,
75-
UNNECESSARY_CAST,
76-
expr.span,
77-
&format!("casting to the same type is unnecessary (`{cast_from}` -> `{cast_to}`)"),
78-
"try",
79-
literal_str,
80-
Applicability::MachineApplicable,
81-
);
82-
return true;
83-
}
84-
},
74+
_ => {},
8575
}
86-
} else if cast_from.kind() == cast_to.kind() && !in_external_macro(cx.sess(), expr.span) {
76+
}
77+
78+
if cast_from.kind() == cast_to.kind() && !in_external_macro(cx.sess(), expr.span) {
8779
span_lint_and_sugg(
8880
cx,
8981
UNNECESSARY_CAST,

0 commit comments

Comments
 (0)