1 use clippy_utils::diagnostics::span_lint_and_sugg;
2 use clippy_utils::numeric_literal;
3 use if_chain::if_chain;
4 use rustc_ast::ast::{self, LitFloatType, LitKind};
5 use rustc_errors::Applicability;
7 use rustc_lint::{LateContext, LateLintPass};
8 use rustc_middle::ty::{self, FloatTy};
9 use rustc_session::{declare_lint_pass, declare_tool_lint};
12 declare_clippy_lint! {
14 /// Checks for float literals with a precision greater
15 /// than that supported by the underlying type.
17 /// ### Why is this bad?
18 /// Rust will truncate the literal silently.
22 /// let v: f32 = 0.123_456_789_9;
23 /// println!("{}", v); // 0.123_456_789
28 /// let v: f64 = 0.123_456_789_9;
29 /// println!("{}", v); // 0.123_456_789_9
31 #[clippy::version = "pre 1.29.0"]
32 pub EXCESSIVE_PRECISION,
34 "excessive precision for float literal"
37 declare_clippy_lint! {
39 /// Checks for whole number float literals that
40 /// cannot be represented as the underlying type without loss.
42 /// ### Why is this bad?
43 /// Rust will silently lose precision during
44 /// conversion to a float.
49 /// let _: f32 = 16_777_217.0; // 16_777_216.0
52 /// let _: f32 = 16_777_216.0;
53 /// let _: f64 = 16_777_217.0;
55 #[clippy::version = "1.43.0"]
56 pub LOSSY_FLOAT_LITERAL,
58 "lossy whole number float literals"
61 declare_lint_pass!(FloatLiteral => [EXCESSIVE_PRECISION, LOSSY_FLOAT_LITERAL]);
63 impl<'tcx> LateLintPass<'tcx> for FloatLiteral {
64 fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>) {
65 let ty = cx.typeck_results().expr_ty(expr);
67 if let ty::Float(fty) = *ty.kind();
68 if let hir::ExprKind::Lit(ref lit) = expr.kind;
69 if let LitKind::Float(sym, lit_float_ty) = lit.node;
71 let sym_str = sym.as_str();
72 let formatter = FloatFormat::new(sym_str);
73 // Try to bail out if the float is for sure fine.
74 // If its within the 2 decimal digits of being out of precision we
75 // check if the parsed representation is the same as the string
76 // since we'll need the truncated string anyway.
77 let digits = count_digits(sym_str);
78 let max = max_digits(fty);
79 let type_suffix = match lit_float_ty {
80 LitFloatType::Suffixed(ast::FloatTy::F32) => Some("f32"),
81 LitFloatType::Suffixed(ast::FloatTy::F64) => Some("f64"),
82 LitFloatType::Unsuffixed => None
84 let (is_whole, mut float_str) = match fty {
86 let value = sym_str.parse::<f32>().unwrap();
88 (value.fract() == 0.0, formatter.format(value))
91 let value = sym_str.parse::<f64>().unwrap();
93 (value.fract() == 0.0, formatter.format(value))
97 if is_whole && !sym_str.contains(|c| c == 'e' || c == 'E') {
98 // Normalize the literal by stripping the fractional portion
99 if sym_str.split('.').next().unwrap() != float_str {
100 // If the type suffix is missing the suggestion would be
101 // incorrectly interpreted as an integer so adding a `.0`
102 // suffix to prevent that.
103 if type_suffix.is_none() {
104 float_str.push_str(".0");
111 "literal cannot be represented as the underlying type without loss of precision",
112 "consider changing the type or replacing it with",
113 numeric_literal::format(&float_str, type_suffix, true),
114 Applicability::MachineApplicable,
117 } else if digits > max as usize && float_str.len() < sym_str.len() {
122 "float has excessive precision",
123 "consider changing the type or truncating it to",
124 numeric_literal::format(&float_str, type_suffix, true),
125 Applicability::MachineApplicable,
134 fn max_digits(fty: FloatTy) -> u32 {
136 FloatTy::F32 => f32::DIGITS,
137 FloatTy::F64 => f64::DIGITS,
141 /// Counts the digits excluding leading zeros
143 fn count_digits(s: &str) -> usize {
144 // Note that s does not contain the f32/64 suffix, and underscores have been stripped
146 .filter(|c| *c != '-' && *c != '.')
147 .take_while(|c| *c != 'e' && *c != 'E')
148 .fold(0, |count, c| {
150 if c == '0' && count == 0 { count } else { count + 1 }
161 fn new(s: &str) -> Self {
163 .find_map(|x| match x {
164 'e' => Some(Self::LowerExp),
165 'E' => Some(Self::UpperExp),
168 .unwrap_or(Self::Normal)
170 fn format<T>(&self, f: T) -> String
172 T: fmt::UpperExp + fmt::LowerExp + fmt::Display,
175 Self::LowerExp => format!("{:e}", f),
176 Self::UpperExp => format!("{:E}", f),
177 Self::Normal => format!("{}", f),