]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_lints/src/float_literal.rs
Auto merge of #72357 - ortem:new-dbg-pretty-printers, r=pnkfelix
[rust.git] / src / tools / clippy / clippy_lints / src / float_literal.rs
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;
5 use rustc_hir as hir;
6 use rustc_lint::{LateContext, LateLintPass};
7 use rustc_middle::ty;
8 use rustc_session::{declare_lint_pass, declare_tool_lint};
9 use std::fmt;
10
11 declare_clippy_lint! {
12     /// **What it does:** Checks for float literals with a precision greater
13     /// than that supported by the underlying type.
14     ///
15     /// **Why is this bad?** Rust will truncate the literal silently.
16     ///
17     /// **Known problems:** None.
18     ///
19     /// **Example:**
20     ///
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:** Checks for whole number float literals that
37     /// cannot be represented as the underlying type without loss.
38     ///
39     /// **Why is this bad?** Rust will silently lose precision during
40     /// conversion to a float.
41     ///
42     /// **Known problems:** None.
43     ///
44     /// **Example:**
45     ///
46     /// ```rust
47     /// // Bad
48     /// let _: f32 = 16_777_217.0; // 16_777_216.0
49     ///
50     /// // Good
51     /// let _: f32 = 16_777_216.0;
52     /// let _: f64 = 16_777_217.0;
53     /// ```
54     pub LOSSY_FLOAT_LITERAL,
55     restriction,
56     "lossy whole number float literals"
57 }
58
59 declare_lint_pass!(FloatLiteral => [EXCESSIVE_PRECISION, LOSSY_FLOAT_LITERAL]);
60
61 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for FloatLiteral {
62     fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr<'_>) {
63         if_chain! {
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;
68             then {
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"),
80                     LitFloatType::Unsuffixed => None
81                 };
82                 let (is_whole, mut float_str) = match fty {
83                     FloatTy::F32 => {
84                         let value = sym_str.parse::<f32>().unwrap();
85
86                         (value.fract() == 0.0, formatter.format(value))
87                     },
88                     FloatTy::F64 => {
89                         let value = sym_str.parse::<f64>().unwrap();
90
91                         (value.fract() == 0.0, formatter.format(value))
92                     },
93                 };
94
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");
103                         }
104
105                         span_lint_and_sugg(
106                             cx,
107                             LOSSY_FLOAT_LITERAL,
108                             expr.span,
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,
113                         );
114                     }
115                 } else if digits > max as usize && sym_str != float_str {
116                     span_lint_and_sugg(
117                         cx,
118                         EXCESSIVE_PRECISION,
119                         expr.span,
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,
124                     );
125                 }
126             }
127         }
128     }
129 }
130
131 #[must_use]
132 fn max_digits(fty: FloatTy) -> u32 {
133     match fty {
134         FloatTy::F32 => f32::DIGITS,
135         FloatTy::F64 => f64::DIGITS,
136     }
137 }
138
139 /// Counts the digits excluding leading zeros
140 #[must_use]
141 fn count_digits(s: &str) -> usize {
142     // Note that s does not contain the f32/64 suffix, and underscores have been stripped
143     s.chars()
144         .filter(|c| *c != '-' && *c != '.')
145         .take_while(|c| *c != 'e' && *c != 'E')
146         .fold(0, |count, c| {
147             // leading zeros
148             if c == '0' && count == 0 {
149                 count
150             } else {
151                 count + 1
152             }
153         })
154 }
155
156 enum FloatFormat {
157     LowerExp,
158     UpperExp,
159     Normal,
160 }
161 impl FloatFormat {
162     #[must_use]
163     fn new(s: &str) -> Self {
164         s.chars()
165             .find_map(|x| match x {
166                 'e' => Some(Self::LowerExp),
167                 'E' => Some(Self::UpperExp),
168                 _ => None,
169             })
170             .unwrap_or(Self::Normal)
171     }
172     fn format<T>(&self, f: T) -> String
173     where
174         T: fmt::UpperExp + fmt::LowerExp + fmt::Display,
175     {
176         match self {
177             Self::LowerExp => format!("{:e}", f),
178             Self::UpperExp => format!("{:E}", f),
179             Self::Normal => format!("{}", f),
180         }
181     }
182 }