1 use crate::utils::{numeric_literal, span_lint_and_sugg};
2 use if_chain::if_chain;
3 use rustc_ast::ast::{FloatTy, LitFloatType, LitKind};
4 use rustc_errors::Applicability;
6 use rustc_lint::{LateContext, LateLintPass};
8 use rustc_session::{declare_lint_pass, declare_tool_lint};
11 declare_clippy_lint! {
12 /// **What it does:** Checks for float literals with a precision greater
13 /// than that supported by the underlying type.
15 /// **Why is this bad?** Rust will truncate the literal silently.
17 /// **Known problems:** None.
23 /// let v: f32 = 0.123_456_789_9;
24 /// println!("{}", v); // 0.123_456_789
27 /// let v: f64 = 0.123_456_789_9;
28 /// println!("{}", v); // 0.123_456_789_9
30 pub EXCESSIVE_PRECISION,
32 "excessive precision for float literal"
35 declare_clippy_lint! {
36 /// **What it does:** Checks for whole number float literals that
37 /// cannot be represented as the underlying type without loss.
39 /// **Why is this bad?** Rust will silently lose precision during
40 /// conversion to a float.
42 /// **Known problems:** None.
48 /// let _: f32 = 16_777_217.0; // 16_777_216.0
51 /// let _: f32 = 16_777_216.0;
52 /// let _: f64 = 16_777_217.0;
54 pub LOSSY_FLOAT_LITERAL,
56 "lossy whole number float literals"
59 declare_lint_pass!(FloatLiteral => [EXCESSIVE_PRECISION, LOSSY_FLOAT_LITERAL]);
61 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for FloatLiteral {
62 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr<'_>) {
64 let ty = cx.tables.expr_ty(expr);
65 if let ty::Float(fty) = ty.kind;
66 if let hir::ExprKind::Lit(ref lit) = expr.kind;
67 if let LitKind::Float(sym, lit_float_ty) = lit.node;
69 let sym_str = sym.as_str();
70 let formatter = FloatFormat::new(&sym_str);
71 // Try to bail out if the float is for sure fine.
72 // If its within the 2 decimal digits of being out of precision we
73 // check if the parsed representation is the same as the string
74 // since we'll need the truncated string anyway.
75 let digits = count_digits(&sym_str);
76 let max = max_digits(fty);
77 let type_suffix = match lit_float_ty {
78 LitFloatType::Suffixed(FloatTy::F32) => Some("f32"),
79 LitFloatType::Suffixed(FloatTy::F64) => Some("f64"),
82 let (is_whole, mut float_str) = match fty {
84 let value = sym_str.parse::<f32>().unwrap();
86 (value.fract() == 0.0, formatter.format(value))
89 let value = sym_str.parse::<f64>().unwrap();
91 (value.fract() == 0.0, formatter.format(value))
95 if is_whole && !sym_str.contains(|c| c == 'e' || c == 'E') {
96 // Normalize the literal by stripping the fractional portion
97 if sym_str.split('.').next().unwrap() != float_str {
98 // If the type suffix is missing the suggestion would be
99 // incorrectly interpreted as an integer so adding a `.0`
100 // suffix to prevent that.
101 if type_suffix.is_none() {
102 float_str.push_str(".0");
109 "literal cannot be represented as the underlying type without loss of precision",
110 "consider changing the type or replacing it with",
111 numeric_literal::format(&float_str, type_suffix, true),
112 Applicability::MachineApplicable,
115 } else if digits > max as usize && sym_str != float_str {
120 "float has excessive precision",
121 "consider changing the type or truncating it to",
122 numeric_literal::format(&float_str, type_suffix, true),
123 Applicability::MachineApplicable,
132 fn max_digits(fty: FloatTy) -> u32 {
134 FloatTy::F32 => f32::DIGITS,
135 FloatTy::F64 => f64::DIGITS,
139 /// Counts the digits excluding leading zeros
141 fn count_digits(s: &str) -> usize {
142 // Note that s does not contain the f32/64 suffix, and underscores have been stripped
144 .filter(|c| *c != '-' && *c != '.')
145 .take_while(|c| *c != 'e' && *c != 'E')
146 .fold(0, |count, c| {
148 if c == '0' && count == 0 {
163 fn new(s: &str) -> Self {
165 .find_map(|x| match x {
166 'e' => Some(Self::LowerExp),
167 'E' => Some(Self::UpperExp),
170 .unwrap_or(Self::Normal)
172 fn format<T>(&self, f: T) -> String
174 T: fmt::UpperExp + fmt::LowerExp + fmt::Display,
177 Self::LowerExp => format!("{:e}", f),
178 Self::UpperExp => format!("{:E}", f),
179 Self::Normal => format!("{}", f),