1 use clippy_utils::diagnostics::span_lint_and_sugg;
2 use clippy_utils::numeric_literal::NumericLiteral;
3 use clippy_utils::source::snippet_opt;
4 use if_chain::if_chain;
5 use rustc_ast::{LitFloatType, LitIntType, LitKind};
6 use rustc_errors::Applicability;
7 use rustc_hir::def::Res;
8 use rustc_hir::{Expr, ExprKind, Lit, QPath, TyKind, UnOp};
9 use rustc_lint::{LateContext, LintContext};
10 use rustc_middle::lint::in_external_macro;
11 use rustc_middle::ty::{self, FloatTy, InferTy, Ty};
13 use super::UNNECESSARY_CAST;
15 pub(super) fn check<'tcx>(
16 cx: &LateContext<'tcx>,
18 cast_expr: &Expr<'tcx>,
22 // skip non-primitive type cast
24 if let ExprKind::Cast(_, cast_to) = expr.kind;
25 if let TyKind::Path(QPath::Resolved(_, path)) = &cast_to.kind;
26 if let Res::PrimTy(_) = path.res;
33 if let Some(lit) = get_numeric_literal(cast_expr) {
34 let literal_str = snippet_opt(cx, cast_expr.span).unwrap_or_default();
37 if let LitKind::Int(n, _) = lit.node;
38 if let Some(src) = snippet_opt(cx, cast_expr.span);
39 if cast_to.is_floating_point();
40 if let Some(num_lit) = NumericLiteral::from_lit_kind(&src, &lit.node);
41 let from_nbits = 128 - n.leading_zeros();
42 let to_nbits = fp_ty_mantissa_nbits(cast_to);
43 if from_nbits != 0 && to_nbits != 0 && from_nbits <= to_nbits && num_lit.is_decimal();
45 lint_unnecessary_cast(cx, expr, num_lit.integer, cast_from, cast_to);
51 LitKind::Int(_, LitIntType::Unsuffixed) if cast_to.is_integral() => {
52 lint_unnecessary_cast(cx, expr, &literal_str, cast_from, cast_to);
54 LitKind::Float(_, LitFloatType::Unsuffixed) if cast_to.is_floating_point() => {
55 lint_unnecessary_cast(cx, expr, &literal_str, cast_from, cast_to);
57 LitKind::Int(_, LitIntType::Unsuffixed) | LitKind::Float(_, LitFloatType::Unsuffixed) => {},
58 LitKind::Int(_, LitIntType::Signed(_) | LitIntType::Unsigned(_))
59 | LitKind::Float(_, LitFloatType::Suffixed(_))
60 if cast_from.kind() == cast_to.kind() =>
62 if let Some(src) = snippet_opt(cx, cast_expr.span) {
63 if let Some(num_lit) = NumericLiteral::from_lit_kind(&src, &lit.node) {
64 lint_unnecessary_cast(cx, expr, num_lit.integer, cast_from, cast_to);
69 if cast_from.kind() == cast_to.kind() && !in_external_macro(cx.sess(), expr.span) {
74 &format!("casting to the same type is unnecessary (`{cast_from}` -> `{cast_to}`)"),
77 Applicability::MachineApplicable,
88 fn lint_unnecessary_cast(cx: &LateContext<'_>, expr: &Expr<'_>, literal_str: &str, cast_from: Ty<'_>, cast_to: Ty<'_>) {
89 let literal_kind_name = if cast_from.is_integral() { "integer" } else { "float" };
91 let matchless = if literal_str.contains(['(', ')']) {
92 replaced_literal = literal_str.replace(['(', ')'], "");
101 &format!("casting {literal_kind_name} literal to `{cast_to}` is unnecessary"),
103 format!("{}_{cast_to}", matchless.trim_end_matches('.')),
104 Applicability::MachineApplicable,
108 fn get_numeric_literal<'e>(expr: &'e Expr<'e>) -> Option<&'e Lit> {
110 ExprKind::Lit(ref lit) => Some(lit),
111 ExprKind::Unary(UnOp::Neg, e) => {
112 if let ExprKind::Lit(ref lit) = e.kind {
122 /// Returns the mantissa bits wide of a fp type.
123 /// Will return 0 if the type is not a fp
124 fn fp_ty_mantissa_nbits(typ: Ty<'_>) -> u32 {
126 ty::Float(FloatTy::F32) => 23,
127 ty::Float(FloatTy::F64) | ty::Infer(InferTy::FloatVar(_)) => 52,