]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/excessive_precision.rs
Fix question_mark.rs
[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_lint_pass, declare_tool_lint};
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 declare_lint_pass!(ExcessivePrecision => [EXCESSIVE_PRECISION]);
39
40 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ExcessivePrecision {
41     fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr) {
42         if_chain! {
43             let ty = cx.tables.expr_ty(expr);
44             if let ty::Float(fty) = ty.sty;
45             if let hir::ExprKind::Lit(ref lit) = expr.node;
46             if let LitKind::Float(sym, _) | LitKind::FloatUnsuffixed(sym) = lit.node;
47             if let Some(sugg) = self.check(sym, fty);
48             then {
49                 span_lint_and_sugg(
50                     cx,
51                     EXCESSIVE_PRECISION,
52                     expr.span,
53                     "float has excessive precision",
54                     "consider changing the type or truncating it to",
55                     sugg,
56                     Applicability::MachineApplicable,
57                 );
58             }
59         }
60     }
61 }
62
63 impl ExcessivePrecision {
64     // None if nothing to lint, Some(suggestion) if lint necessary
65     fn check(self, sym: Symbol, fty: FloatTy) -> Option<String> {
66         let max = max_digits(fty);
67         let sym_str = sym.as_str();
68         if dot_zero_exclusion(&sym_str) {
69             return None;
70         }
71         // Try to bail out if the float is for sure fine.
72         // If its within the 2 decimal digits of being out of precision we
73         // check if the parsed representation is the same as the string
74         // since we'll need the truncated string anyway.
75         let digits = count_digits(&sym_str);
76         if digits > max as usize {
77             let formatter = FloatFormat::new(&sym_str);
78             let sr = match fty {
79                 FloatTy::F32 => sym_str.parse::<f32>().map(|f| formatter.format(f)),
80                 FloatTy::F64 => sym_str.parse::<f64>().map(|f| formatter.format(f)),
81             };
82             // We know this will parse since we are in LatePass
83             let s = sr.unwrap();
84
85             if sym_str == s {
86                 None
87             } else {
88                 let di = super::literal_representation::DigitInfo::new(&s, true);
89                 Some(di.grouping_hint())
90             }
91         } else {
92             None
93         }
94     }
95 }
96
97 /// Should we exclude the float because it has a `.0` or `.` suffix
98 /// Ex `1_000_000_000.0`
99 /// Ex `1_000_000_000.`
100 fn dot_zero_exclusion(s: &str) -> bool {
101     if let Some(after_dec) = s.split('.').nth(1) {
102         let mut decpart = after_dec.chars().take_while(|c| *c != 'e' || *c != 'E');
103
104         match decpart.next() {
105             Some('0') => decpart.count() == 0,
106             Some(_) => false,
107             None => true,
108         }
109     } else {
110         false
111     }
112 }
113
114 fn max_digits(fty: FloatTy) -> u32 {
115     match fty {
116         FloatTy::F32 => f32::DIGITS,
117         FloatTy::F64 => f64::DIGITS,
118     }
119 }
120
121 /// Counts the digits excluding leading zeros
122 fn count_digits(s: &str) -> usize {
123     // Note that s does not contain the f32/64 suffix, and underscores have been stripped
124     s.chars()
125         .filter(|c| *c != '-' && *c != '.')
126         .take_while(|c| *c != 'e' && *c != 'E')
127         .fold(0, |count, c| {
128             // leading zeros
129             if c == '0' && count == 0 {
130                 count
131             } else {
132                 count + 1
133             }
134         })
135 }
136
137 enum FloatFormat {
138     LowerExp,
139     UpperExp,
140     Normal,
141 }
142 impl FloatFormat {
143     fn new(s: &str) -> Self {
144         s.chars()
145             .find_map(|x| match x {
146                 'e' => Some(FloatFormat::LowerExp),
147                 'E' => Some(FloatFormat::UpperExp),
148                 _ => None,
149             })
150             .unwrap_or(FloatFormat::Normal)
151     }
152     fn format<T>(&self, f: T) -> String
153     where
154         T: fmt::UpperExp + fmt::LowerExp + fmt::Display,
155     {
156         match self {
157             FloatFormat::LowerExp => format!("{:e}", f),
158             FloatFormat::UpperExp => format!("{:E}", f),
159             FloatFormat::Normal => format!("{}", f),
160         }
161     }
162 }