]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/excessive_precision.rs
rustup https://github.com/rust-lang/rust/pull/67455
[rust.git] / clippy_lints / src / excessive_precision.rs
1 use crate::utils::span_lint_and_sugg;
2 use crate::utils::sugg::format_numeric_literal;
3 use if_chain::if_chain;
4 use rustc::declare_lint_pass;
5 use rustc::hir;
6 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
7 use rustc::ty;
8 use rustc_errors::Applicability;
9 use rustc_session::declare_tool_lint;
10 use std::f32;
11 use std::f64;
12 use std::fmt;
13 use syntax::ast::*;
14 use syntax_pos::symbol::Symbol;
15
16 declare_clippy_lint! {
17     /// **What it does:** Checks for float literals with a precision greater
18     /// than that supported by the underlying type
19     ///
20     /// **Why is this bad?** Rust will truncate the literal silently.
21     ///
22     /// **Known problems:** None.
23     ///
24     /// **Example:**
25     ///
26     /// ```rust
27     /// // Bad
28     /// let v: f32 = 0.123_456_789_9;
29     /// println!("{}", v); //  0.123_456_789
30     ///
31     /// // Good
32     /// let v: f64 = 0.123_456_789_9;
33     /// println!("{}", v); //  0.123_456_789_9
34     /// ```
35     pub EXCESSIVE_PRECISION,
36     style,
37     "excessive precision for float literal"
38 }
39
40 declare_lint_pass!(ExcessivePrecision => [EXCESSIVE_PRECISION]);
41
42 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ExcessivePrecision {
43     fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr) {
44         if_chain! {
45             let ty = cx.tables.expr_ty(expr);
46             if let ty::Float(fty) = ty.kind;
47             if let hir::ExprKind::Lit(ref lit) = expr.kind;
48             if let LitKind::Float(sym, _) = lit.node;
49             if let Some(sugg) = Self::check(sym, fty);
50             then {
51                 span_lint_and_sugg(
52                     cx,
53                     EXCESSIVE_PRECISION,
54                     expr.span,
55                     "float has excessive precision",
56                     "consider changing the type or truncating it to",
57                     sugg,
58                     Applicability::MachineApplicable,
59                 );
60             }
61         }
62     }
63 }
64
65 impl ExcessivePrecision {
66     // None if nothing to lint, Some(suggestion) if lint necessary
67     #[must_use]
68     fn check(sym: Symbol, fty: FloatTy) -> Option<String> {
69         let max = max_digits(fty);
70         let sym_str = sym.as_str();
71         if dot_zero_exclusion(&sym_str) {
72             return None;
73         }
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         if digits > max as usize {
80             let formatter = FloatFormat::new(&sym_str);
81             let sr = match fty {
82                 FloatTy::F32 => sym_str.parse::<f32>().map(|f| formatter.format(f)),
83                 FloatTy::F64 => sym_str.parse::<f64>().map(|f| formatter.format(f)),
84             };
85             // We know this will parse since we are in LatePass
86             let s = sr.unwrap();
87
88             if sym_str == s {
89                 None
90             } else {
91                 Some(format_numeric_literal(&s, None, true))
92             }
93         } else {
94             None
95         }
96     }
97 }
98
99 /// Should we exclude the float because it has a `.0` or `.` suffix
100 /// Ex `1_000_000_000.0`
101 /// Ex `1_000_000_000.`
102 #[must_use]
103 fn dot_zero_exclusion(s: &str) -> bool {
104     s.split('.').nth(1).map_or(false, |after_dec| {
105         let mut decpart = after_dec.chars().take_while(|c| *c != 'e' || *c != 'E');
106
107         match decpart.next() {
108             Some('0') => decpart.count() == 0,
109             Some(_) => false,
110             None => true,
111         }
112     })
113 }
114
115 #[must_use]
116 fn max_digits(fty: FloatTy) -> u32 {
117     match fty {
118         FloatTy::F32 => f32::DIGITS,
119         FloatTy::F64 => f64::DIGITS,
120     }
121 }
122
123 /// Counts the digits excluding leading zeros
124 #[must_use]
125 fn count_digits(s: &str) -> usize {
126     // Note that s does not contain the f32/64 suffix, and underscores have been stripped
127     s.chars()
128         .filter(|c| *c != '-' && *c != '.')
129         .take_while(|c| *c != 'e' && *c != 'E')
130         .fold(0, |count, c| {
131             // leading zeros
132             if c == '0' && count == 0 {
133                 count
134             } else {
135                 count + 1
136             }
137         })
138 }
139
140 enum FloatFormat {
141     LowerExp,
142     UpperExp,
143     Normal,
144 }
145 impl FloatFormat {
146     #[must_use]
147     fn new(s: &str) -> Self {
148         s.chars()
149             .find_map(|x| match x {
150                 'e' => Some(Self::LowerExp),
151                 'E' => Some(Self::UpperExp),
152                 _ => None,
153             })
154             .unwrap_or(Self::Normal)
155     }
156     fn format<T>(&self, f: T) -> String
157     where
158         T: fmt::UpperExp + fmt::LowerExp + fmt::Display,
159     {
160         match self {
161             Self::LowerExp => format!("{:e}", f),
162             Self::UpperExp => format!("{:E}", f),
163             Self::Normal => format!("{}", f),
164         }
165     }
166 }