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