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