]>
Commit | Line | Data |
---|---|---|
f20569fa XL |
1 | use rustc_ast::{LitFloatType, LitIntType, LitKind}; |
2 | use rustc_errors::Applicability; | |
3 | use rustc_hir::{Expr, ExprKind, Lit, UnOp}; | |
4 | use rustc_lint::{LateContext, LintContext}; | |
5 | use rustc_middle::lint::in_external_macro; | |
6 | use rustc_middle::ty::{self, FloatTy, InferTy, Ty}; | |
7 | ||
8 | use if_chain::if_chain; | |
9 | ||
10 | use crate::utils::{numeric_literal::NumericLiteral, snippet_opt, span_lint, span_lint_and_sugg}; | |
11 | ||
12 | use super::UNNECESSARY_CAST; | |
13 | ||
14 | pub(super) fn check( | |
15 | cx: &LateContext<'_>, | |
16 | expr: &Expr<'_>, | |
17 | cast_expr: &Expr<'_>, | |
18 | cast_from: Ty<'_>, | |
19 | cast_to: Ty<'_>, | |
20 | ) -> bool { | |
21 | if let Some(lit) = get_numeric_literal(cast_expr) { | |
22 | let literal_str = snippet_opt(cx, cast_expr.span).unwrap_or_default(); | |
23 | ||
24 | if_chain! { | |
25 | if let LitKind::Int(n, _) = lit.node; | |
26 | if let Some(src) = snippet_opt(cx, lit.span); | |
27 | if cast_to.is_floating_point(); | |
28 | if let Some(num_lit) = NumericLiteral::from_lit_kind(&src, &lit.node); | |
29 | let from_nbits = 128 - n.leading_zeros(); | |
30 | let to_nbits = fp_ty_mantissa_nbits(cast_to); | |
31 | if from_nbits != 0 && to_nbits != 0 && from_nbits <= to_nbits && num_lit.is_decimal(); | |
32 | then { | |
33 | let literal_str = if is_unary_neg(cast_expr) { format!("-{}", num_lit.integer) } else { num_lit.integer.into() }; | |
34 | lint_unnecessary_cast(cx, expr, &literal_str, cast_from, cast_to); | |
35 | return true | |
36 | } | |
37 | } | |
38 | ||
39 | match lit.node { | |
40 | LitKind::Int(_, LitIntType::Unsuffixed) if cast_to.is_integral() => { | |
41 | lint_unnecessary_cast(cx, expr, &literal_str, cast_from, cast_to); | |
42 | }, | |
43 | LitKind::Float(_, LitFloatType::Unsuffixed) if cast_to.is_floating_point() => { | |
44 | lint_unnecessary_cast(cx, expr, &literal_str, cast_from, cast_to); | |
45 | }, | |
46 | LitKind::Int(_, LitIntType::Unsuffixed) | LitKind::Float(_, LitFloatType::Unsuffixed) => {}, | |
47 | _ => { | |
48 | if cast_from.kind() == cast_to.kind() && !in_external_macro(cx.sess(), expr.span) { | |
49 | span_lint( | |
50 | cx, | |
51 | UNNECESSARY_CAST, | |
52 | expr.span, | |
53 | &format!( | |
54 | "casting to the same type is unnecessary (`{}` -> `{}`)", | |
55 | cast_from, cast_to | |
56 | ), | |
57 | ); | |
58 | return true; | |
59 | } | |
60 | }, | |
61 | } | |
62 | } | |
63 | ||
64 | false | |
65 | } | |
66 | ||
67 | fn lint_unnecessary_cast(cx: &LateContext<'_>, expr: &Expr<'_>, literal_str: &str, cast_from: Ty<'_>, cast_to: Ty<'_>) { | |
68 | let literal_kind_name = if cast_from.is_integral() { "integer" } else { "float" }; | |
69 | span_lint_and_sugg( | |
70 | cx, | |
71 | UNNECESSARY_CAST, | |
72 | expr.span, | |
73 | &format!("casting {} literal to `{}` is unnecessary", literal_kind_name, cast_to), | |
74 | "try", | |
75 | format!("{}_{}", literal_str.trim_end_matches('.'), cast_to), | |
76 | Applicability::MachineApplicable, | |
77 | ); | |
78 | } | |
79 | ||
80 | fn get_numeric_literal<'e>(expr: &'e Expr<'e>) -> Option<&'e Lit> { | |
81 | match expr.kind { | |
82 | ExprKind::Lit(ref lit) => Some(lit), | |
83 | ExprKind::Unary(UnOp::Neg, e) => { | |
84 | if let ExprKind::Lit(ref lit) = e.kind { | |
85 | Some(lit) | |
86 | } else { | |
87 | None | |
88 | } | |
89 | }, | |
90 | _ => None, | |
91 | } | |
92 | } | |
93 | ||
94 | /// Returns the mantissa bits wide of a fp type. | |
95 | /// Will return 0 if the type is not a fp | |
96 | fn fp_ty_mantissa_nbits(typ: Ty<'_>) -> u32 { | |
97 | match typ.kind() { | |
98 | ty::Float(FloatTy::F32) => 23, | |
99 | ty::Float(FloatTy::F64) | ty::Infer(InferTy::FloatVar(_)) => 52, | |
100 | _ => 0, | |
101 | } | |
102 | } | |
103 | ||
104 | fn is_unary_neg(expr: &Expr<'_>) -> bool { | |
105 | matches!(expr.kind, ExprKind::Unary(UnOp::Neg, _)) | |
106 | } |