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