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