1 use crate::utils::span_lint_and_sugg;
2 use if_chain::if_chain;
4 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
6 use rustc::{declare_tool_lint, lint_array};
7 use rustc_errors::Applicability;
12 use syntax_pos::symbol::Symbol;
14 declare_clippy_lint! {
15 /// **What it does:** Checks for float literals with a precision greater
16 /// than that supported by the underlying type
18 /// **Why is this bad?** Rust will truncate the literal silently.
20 /// **Known problems:** None.
26 /// let v: f32 = 0.123_456_789_9;
27 /// println!("{}", v); // 0.123_456_789
30 /// let v: f64 = 0.123_456_789_9;
31 /// println!("{}", v); // 0.123_456_789_9
33 pub EXCESSIVE_PRECISION,
35 "excessive precision for float literal"
38 pub struct ExcessivePrecision;
40 impl LintPass for ExcessivePrecision {
41 fn get_lints(&self) -> LintArray {
42 lint_array!(EXCESSIVE_PRECISION)
45 fn name(&self) -> &'static str {
50 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ExcessivePrecision {
51 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr) {
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);
63 "float has excessive precision",
64 "consider changing the type or truncating it to",
66 Applicability::MachineApplicable,
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) {
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);
89 FloatTy::F32 => sym_str.parse::<f32>().map(|f| formatter.format(f)),
90 FloatTy::F64 => sym_str.parse::<f64>().map(|f| formatter.format(f)),
92 // We know this will parse since we are in LatePass
98 let di = super::literal_representation::DigitInfo::new(&s, true);
99 Some(di.grouping_hint())
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');
114 match decpart.next() {
115 Some('0') => decpart.count() == 0,
124 fn max_digits(fty: FloatTy) -> u32 {
126 FloatTy::F32 => f32::DIGITS,
127 FloatTy::F64 => f64::DIGITS,
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
135 .filter(|c| *c != '-' && *c != '.')
136 .take_while(|c| *c != 'e' && *c != 'E')
137 .fold(0, |count, c| {
139 if c == '0' && count == 0 {
153 fn new(s: &str) -> Self {
155 .find_map(|x| match x {
156 'e' => Some(FloatFormat::LowerExp),
157 'E' => Some(FloatFormat::UpperExp),
160 .unwrap_or(FloatFormat::Normal)
162 fn format<T>(&self, f: T) -> String
164 T: fmt::UpperExp + fmt::LowerExp + fmt::Display,
167 FloatFormat::LowerExp => format!("{:e}", f),
168 FloatFormat::UpperExp => format!("{:E}", f),
169 FloatFormat::Normal => format!("{}", f),