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::declare_lint_pass;
6 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
8 use rustc_errors::Applicability;
9 use rustc_session::declare_tool_lint;
14 use syntax_pos::symbol::Symbol;
16 declare_clippy_lint! {
17 /// **What it does:** Checks for float literals with a precision greater
18 /// than that supported by the underlying type
20 /// **Why is this bad?** Rust will truncate the literal silently.
22 /// **Known problems:** None.
28 /// let v: f32 = 0.123_456_789_9;
29 /// println!("{}", v); // 0.123_456_789
32 /// let v: f64 = 0.123_456_789_9;
33 /// println!("{}", v); // 0.123_456_789_9
35 pub EXCESSIVE_PRECISION,
37 "excessive precision for float literal"
40 declare_lint_pass!(ExcessivePrecision => [EXCESSIVE_PRECISION]);
42 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ExcessivePrecision {
43 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr) {
45 let ty = cx.tables.expr_ty(expr);
46 if let ty::Float(fty) = ty.kind;
47 if let hir::ExprKind::Lit(ref lit) = expr.kind;
48 if let LitKind::Float(sym, _) = lit.node;
49 if let Some(sugg) = Self::check(sym, fty);
55 "float has excessive precision",
56 "consider changing the type or truncating it to",
58 Applicability::MachineApplicable,
65 impl ExcessivePrecision {
66 // None if nothing to lint, Some(suggestion) if lint necessary
68 fn check(sym: Symbol, fty: FloatTy) -> Option<String> {
69 let max = max_digits(fty);
70 let sym_str = sym.as_str();
71 if dot_zero_exclusion(&sym_str) {
74 // Try to bail out if the float is for sure fine.
75 // If its within the 2 decimal digits of being out of precision we
76 // check if the parsed representation is the same as the string
77 // since we'll need the truncated string anyway.
78 let digits = count_digits(&sym_str);
79 if digits > max as usize {
80 let formatter = FloatFormat::new(&sym_str);
82 FloatTy::F32 => sym_str.parse::<f32>().map(|f| formatter.format(f)),
83 FloatTy::F64 => sym_str.parse::<f64>().map(|f| formatter.format(f)),
85 // We know this will parse since we are in LatePass
91 Some(format_numeric_literal(&s, None, true))
99 /// Should we exclude the float because it has a `.0` or `.` suffix
100 /// Ex `1_000_000_000.0`
101 /// Ex `1_000_000_000.`
103 fn dot_zero_exclusion(s: &str) -> bool {
104 s.split('.').nth(1).map_or(false, |after_dec| {
105 let mut decpart = after_dec.chars().take_while(|c| *c != 'e' || *c != 'E');
107 match decpart.next() {
108 Some('0') => decpart.count() == 0,
116 fn max_digits(fty: FloatTy) -> u32 {
118 FloatTy::F32 => f32::DIGITS,
119 FloatTy::F64 => f64::DIGITS,
123 /// Counts the digits excluding leading zeros
125 fn count_digits(s: &str) -> usize {
126 // Note that s does not contain the f32/64 suffix, and underscores have been stripped
128 .filter(|c| *c != '-' && *c != '.')
129 .take_while(|c| *c != 'e' && *c != 'E')
130 .fold(0, |count, c| {
132 if c == '0' && count == 0 {
147 fn new(s: &str) -> Self {
149 .find_map(|x| match x {
150 'e' => Some(Self::LowerExp),
151 'E' => Some(Self::UpperExp),
154 .unwrap_or(Self::Normal)
156 fn format<T>(&self, f: T) -> String
158 T: fmt::UpperExp + fmt::LowerExp + fmt::Display,
161 Self::LowerExp => format!("{:e}", f),
162 Self::UpperExp => format!("{:E}", f),
163 Self::Normal => format!("{}", f),