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;
6 use rustc_hir as hir;
7 use rustc_lint::{LateContext, LateLintPass};
8 use rustc_middle::ty::{self, FloatTy};
9 use rustc_session::{declare_lint_pass, declare_tool_lint};
10 use std::fmt;
11
12 declare_clippy_lint! {
13 /// ### What it does
14 /// Checks for float literals with a precision greater
15 /// than that supported by the underlying type.
16 ///
17 /// ### Why is this bad?
18 /// Rust will truncate the literal silently.
19 ///
20 /// ### Example
21 /// ```rust
22 /// // Bad
23 /// let v: f32 = 0.123_456_789_9;
24 /// println!("{}", v); // 0.123_456_789
25 ///
26 /// // Good
27 /// let v: f64 = 0.123_456_789_9;
28 /// println!("{}", v); // 0.123_456_789_9
29 /// ```
30 pub EXCESSIVE_PRECISION,
31 style,
32 "excessive precision for float literal"
33 }
34
35 declare_clippy_lint! {
36 /// ### What it does
37 /// Checks for whole number float literals that
38 /// cannot be represented as the underlying type without loss.
39 ///
40 /// ### Why is this bad?
41 /// Rust will silently lose precision during
42 /// conversion to a float.
43 ///
44 /// ### Example
45 /// ```rust
46 /// // Bad
47 /// let _: f32 = 16_777_217.0; // 16_777_216.0
48 ///
49 /// // Good
50 /// let _: f32 = 16_777_216.0;
51 /// let _: f64 = 16_777_217.0;
52 /// ```
53 pub LOSSY_FLOAT_LITERAL,
54 restriction,
55 "lossy whole number float literals"
56 }
57
58 declare_lint_pass!(FloatLiteral => [EXCESSIVE_PRECISION, LOSSY_FLOAT_LITERAL]);
59
60 impl<'tcx> LateLintPass<'tcx> for FloatLiteral {
check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>)61 fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>) {
62 let ty = cx.typeck_results().expr_ty(expr);
63 if_chain! {
64 if let ty::Float(fty) = *ty.kind();
65 if let hir::ExprKind::Lit(ref lit) = expr.kind;
66 if let LitKind::Float(sym, lit_float_ty) = lit.node;
67 then {
68 let sym_str = sym.as_str();
69 let formatter = FloatFormat::new(&sym_str);
70 // Try to bail out if the float is for sure fine.
71 // If its within the 2 decimal digits of being out of precision we
72 // check if the parsed representation is the same as the string
73 // since we'll need the truncated string anyway.
74 let digits = count_digits(&sym_str);
75 let max = max_digits(fty);
76 let type_suffix = match lit_float_ty {
77 LitFloatType::Suffixed(ast::FloatTy::F32) => Some("f32"),
78 LitFloatType::Suffixed(ast::FloatTy::F64) => Some("f64"),
79 LitFloatType::Unsuffixed => None
80 };
81 let (is_whole, mut float_str) = match fty {
82 FloatTy::F32 => {
83 let value = sym_str.parse::<f32>().unwrap();
84
85 (value.fract() == 0.0, formatter.format(value))
86 },
87 FloatTy::F64 => {
88 let value = sym_str.parse::<f64>().unwrap();
89
90 (value.fract() == 0.0, formatter.format(value))
91 },
92 };
93
94 if is_whole && !sym_str.contains(|c| c == 'e' || c == 'E') {
95 // Normalize the literal by stripping the fractional portion
96 if sym_str.split('.').next().unwrap() != float_str {
97 // If the type suffix is missing the suggestion would be
98 // incorrectly interpreted as an integer so adding a `.0`
99 // suffix to prevent that.
100 if type_suffix.is_none() {
101 float_str.push_str(".0");
102 }
103
104 span_lint_and_sugg(
105 cx,
106 LOSSY_FLOAT_LITERAL,
107 expr.span,
108 "literal cannot be represented as the underlying type without loss of precision",
109 "consider changing the type or replacing it with",
110 numeric_literal::format(&float_str, type_suffix, true),
111 Applicability::MachineApplicable,
112 );
113 }
114 } else if digits > max as usize && float_str.len() < sym_str.len() {
115 span_lint_and_sugg(
116 cx,
117 EXCESSIVE_PRECISION,
118 expr.span,
119 "float has excessive precision",
120 "consider changing the type or truncating it to",
121 numeric_literal::format(&float_str, type_suffix, true),
122 Applicability::MachineApplicable,
123 );
124 }
125 }
126 }
127 }
128 }
129
130 #[must_use]
max_digits(fty: FloatTy) -> u32131 fn max_digits(fty: FloatTy) -> u32 {
132 match fty {
133 FloatTy::F32 => f32::DIGITS,
134 FloatTy::F64 => f64::DIGITS,
135 }
136 }
137
138 /// Counts the digits excluding leading zeros
139 #[must_use]
count_digits(s: &str) -> usize140 fn count_digits(s: &str) -> usize {
141 // Note that s does not contain the f32/64 suffix, and underscores have been stripped
142 s.chars()
143 .filter(|c| *c != '-' && *c != '.')
144 .take_while(|c| *c != 'e' && *c != 'E')
145 .fold(0, |count, c| {
146 // leading zeros
147 if c == '0' && count == 0 { count } else { count + 1 }
148 })
149 }
150
151 enum FloatFormat {
152 LowerExp,
153 UpperExp,
154 Normal,
155 }
156 impl FloatFormat {
157 #[must_use]
new(s: &str) -> Self158 fn new(s: &str) -> Self {
159 s.chars()
160 .find_map(|x| match x {
161 'e' => Some(Self::LowerExp),
162 'E' => Some(Self::UpperExp),
163 _ => None,
164 })
165 .unwrap_or(Self::Normal)
166 }
format<T>(&self, f: T) -> String where T: fmt::UpperExp + fmt::LowerExp + fmt::Display,167 fn format<T>(&self, f: T) -> String
168 where
169 T: fmt::UpperExp + fmt::LowerExp + fmt::Display,
170 {
171 match self {
172 Self::LowerExp => format!("{:e}", f),
173 Self::UpperExp => format!("{:E}", f),
174 Self::Normal => format!("{}", f),
175 }
176 }
177 }
178