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