]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/float_literal.rs
Make docs more consistent
[rust.git] / 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     /// // 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     #[clippy::version = "1.43.0"]
56     pub LOSSY_FLOAT_LITERAL,
57     restriction,
58     "lossy whole number float literals"
59 }
60
61 declare_lint_pass!(FloatLiteral => [EXCESSIVE_PRECISION, LOSSY_FLOAT_LITERAL]);
62
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);
66         if_chain! {
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;
70             then {
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
83                 };
84                 let (is_whole, mut float_str) = match fty {
85                     FloatTy::F32 => {
86                         let value = sym_str.parse::<f32>().unwrap();
87
88                         (value.fract() == 0.0, formatter.format(value))
89                     },
90                     FloatTy::F64 => {
91                         let value = sym_str.parse::<f64>().unwrap();
92
93                         (value.fract() == 0.0, formatter.format(value))
94                     },
95                 };
96
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");
105                         }
106
107                         span_lint_and_sugg(
108                             cx,
109                             LOSSY_FLOAT_LITERAL,
110                             expr.span,
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,
115                         );
116                     }
117                 } else if digits > max as usize && float_str.len() < sym_str.len() {
118                     span_lint_and_sugg(
119                         cx,
120                         EXCESSIVE_PRECISION,
121                         expr.span,
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,
126                     );
127                 }
128             }
129         }
130     }
131 }
132
133 #[must_use]
134 fn max_digits(fty: FloatTy) -> u32 {
135     match fty {
136         FloatTy::F32 => f32::DIGITS,
137         FloatTy::F64 => f64::DIGITS,
138     }
139 }
140
141 /// Counts the digits excluding leading zeros
142 #[must_use]
143 fn count_digits(s: &str) -> usize {
144     // Note that s does not contain the f32/64 suffix, and underscores have been stripped
145     s.chars()
146         .filter(|c| *c != '-' && *c != '.')
147         .take_while(|c| *c != 'e' && *c != 'E')
148         .fold(0, |count, c| {
149             // leading zeros
150             if c == '0' && count == 0 { count } else { count + 1 }
151         })
152 }
153
154 enum FloatFormat {
155     LowerExp,
156     UpperExp,
157     Normal,
158 }
159 impl FloatFormat {
160     #[must_use]
161     fn new(s: &str) -> Self {
162         s.chars()
163             .find_map(|x| match x {
164                 'e' => Some(Self::LowerExp),
165                 'E' => Some(Self::UpperExp),
166                 _ => None,
167             })
168             .unwrap_or(Self::Normal)
169     }
170     fn format<T>(&self, f: T) -> String
171     where
172         T: fmt::UpperExp + fmt::LowerExp + fmt::Display,
173     {
174         match self {
175             Self::LowerExp => format!("{:e}", f),
176             Self::UpperExp => format!("{:E}", f),
177             Self::Normal => format!("{}", f),
178         }
179     }
180 }