2018-11-20 13:06:29 +00:00
|
|
|
use crate::utils::span_lint_and_sugg;
|
2019-11-13 06:28:50 +00:00
|
|
|
use crate::utils::sugg::format_numeric_literal;
|
2018-07-19 08:00:54 +00:00
|
|
|
use if_chain::if_chain;
|
2020-01-11 11:37:08 +00:00
|
|
|
use rustc::lint::{LateContext, LateLintPass};
|
2018-12-29 15:04:45 +00:00
|
|
|
use rustc::ty;
|
|
|
|
use rustc_errors::Applicability;
|
2020-01-06 16:39:50 +00:00
|
|
|
use rustc_hir as hir;
|
2020-01-11 11:37:08 +00:00
|
|
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
2019-12-31 00:17:56 +00:00
|
|
|
use rustc_span::symbol::Symbol;
|
2018-04-13 00:42:57 +00:00
|
|
|
use std::f32;
|
|
|
|
use std::f64;
|
|
|
|
use std::fmt;
|
2018-12-29 15:04:45 +00:00
|
|
|
use syntax::ast::*;
|
2018-04-13 00:42:57 +00:00
|
|
|
|
|
|
|
declare_clippy_lint! {
|
2019-03-05 16:50:33 +00:00
|
|
|
/// **What it does:** Checks for float literals with a precision greater
|
|
|
|
/// than that supported by the underlying type
|
|
|
|
///
|
|
|
|
/// **Why is this bad?** Rust will truncate the literal silently.
|
|
|
|
///
|
|
|
|
/// **Known problems:** None.
|
|
|
|
///
|
|
|
|
/// **Example:**
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// // Bad
|
|
|
|
/// let v: f32 = 0.123_456_789_9;
|
|
|
|
/// println!("{}", v); // 0.123_456_789
|
|
|
|
///
|
|
|
|
/// // Good
|
|
|
|
/// let v: f64 = 0.123_456_789_9;
|
|
|
|
/// println!("{}", v); // 0.123_456_789_9
|
|
|
|
/// ```
|
2018-04-13 00:42:57 +00:00
|
|
|
pub EXCESSIVE_PRECISION,
|
|
|
|
style,
|
|
|
|
"excessive precision for float literal"
|
|
|
|
}
|
|
|
|
|
2019-04-08 20:43:55 +00:00
|
|
|
declare_lint_pass!(ExcessivePrecision => [EXCESSIVE_PRECISION]);
|
2018-04-13 00:42:57 +00:00
|
|
|
|
|
|
|
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ExcessivePrecision {
|
2019-12-27 07:12:26 +00:00
|
|
|
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr<'_>) {
|
2018-04-13 00:42:57 +00:00
|
|
|
if_chain! {
|
|
|
|
let ty = cx.tables.expr_ty(expr);
|
2019-09-26 09:03:36 +00:00
|
|
|
if let ty::Float(fty) = ty.kind;
|
2019-09-27 15:16:06 +00:00
|
|
|
if let hir::ExprKind::Lit(ref lit) = expr.kind;
|
2019-11-07 12:27:00 +00:00
|
|
|
if let LitKind::Float(sym, _) = lit.node;
|
2019-10-03 19:09:32 +00:00
|
|
|
if let Some(sugg) = Self::check(sym, fty);
|
2018-04-13 00:42:57 +00:00
|
|
|
then {
|
|
|
|
span_lint_and_sugg(
|
|
|
|
cx,
|
|
|
|
EXCESSIVE_PRECISION,
|
|
|
|
expr.span,
|
|
|
|
"float has excessive precision",
|
|
|
|
"consider changing the type or truncating it to",
|
|
|
|
sugg,
|
2018-11-27 14:13:57 +00:00
|
|
|
Applicability::MachineApplicable,
|
2018-04-13 00:42:57 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ExcessivePrecision {
|
2018-07-25 18:02:52 +00:00
|
|
|
// None if nothing to lint, Some(suggestion) if lint necessary
|
2019-09-18 06:37:41 +00:00
|
|
|
#[must_use]
|
2019-10-03 19:09:32 +00:00
|
|
|
fn check(sym: Symbol, fty: FloatTy) -> Option<String> {
|
2018-04-13 00:42:57 +00:00
|
|
|
let max = max_digits(fty);
|
|
|
|
let sym_str = sym.as_str();
|
2018-05-02 18:40:52 +00:00
|
|
|
if dot_zero_exclusion(&sym_str) {
|
2018-11-27 20:14:15 +00:00
|
|
|
return None;
|
2018-05-02 18:40:52 +00:00
|
|
|
}
|
2018-04-13 00:42:57 +00:00
|
|
|
// Try to bail out if the float is for sure fine.
|
|
|
|
// If its within the 2 decimal digits of being out of precision we
|
|
|
|
// check if the parsed representation is the same as the string
|
|
|
|
// since we'll need the truncated string anyway.
|
2018-05-02 18:40:52 +00:00
|
|
|
let digits = count_digits(&sym_str);
|
2018-04-13 00:42:57 +00:00
|
|
|
if digits > max as usize {
|
2018-05-02 18:40:52 +00:00
|
|
|
let formatter = FloatFormat::new(&sym_str);
|
2018-05-31 18:15:48 +00:00
|
|
|
let sr = match fty {
|
2018-04-13 00:42:57 +00:00
|
|
|
FloatTy::F32 => sym_str.parse::<f32>().map(|f| formatter.format(f)),
|
|
|
|
FloatTy::F64 => sym_str.parse::<f64>().map(|f| formatter.format(f)),
|
|
|
|
};
|
|
|
|
// We know this will parse since we are in LatePass
|
|
|
|
let s = sr.unwrap();
|
|
|
|
|
|
|
|
if sym_str == s {
|
|
|
|
None
|
|
|
|
} else {
|
2019-11-13 06:28:50 +00:00
|
|
|
Some(format_numeric_literal(&s, None, true))
|
2018-04-13 00:42:57 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-29 14:39:30 +00:00
|
|
|
/// Should we exclude the float because it has a `.0` or `.` suffix
|
2018-12-07 21:47:12 +00:00
|
|
|
/// Ex `1_000_000_000.0`
|
|
|
|
/// Ex `1_000_000_000.`
|
2019-09-18 06:37:41 +00:00
|
|
|
#[must_use]
|
2018-05-02 18:40:52 +00:00
|
|
|
fn dot_zero_exclusion(s: &str) -> bool {
|
2019-09-04 14:19:59 +00:00
|
|
|
s.split('.').nth(1).map_or(false, |after_dec| {
|
2018-11-27 20:14:15 +00:00
|
|
|
let mut decpart = after_dec.chars().take_while(|c| *c != 'e' || *c != 'E');
|
2018-05-02 18:40:52 +00:00
|
|
|
|
|
|
|
match decpart.next() {
|
|
|
|
Some('0') => decpart.count() == 0,
|
2018-09-29 14:39:30 +00:00
|
|
|
Some(_) => false,
|
|
|
|
None => true,
|
2018-05-02 18:40:52 +00:00
|
|
|
}
|
2019-09-04 14:19:59 +00:00
|
|
|
})
|
2018-05-02 18:40:52 +00:00
|
|
|
}
|
|
|
|
|
2019-09-18 06:37:41 +00:00
|
|
|
#[must_use]
|
2018-05-31 18:15:48 +00:00
|
|
|
fn max_digits(fty: FloatTy) -> u32 {
|
2018-04-13 00:42:57 +00:00
|
|
|
match fty {
|
|
|
|
FloatTy::F32 => f32::DIGITS,
|
|
|
|
FloatTy::F64 => f64::DIGITS,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-02 18:40:52 +00:00
|
|
|
/// Counts the digits excluding leading zeros
|
2019-09-18 06:37:41 +00:00
|
|
|
#[must_use]
|
2018-04-13 00:42:57 +00:00
|
|
|
fn count_digits(s: &str) -> usize {
|
2018-10-07 18:38:20 +00:00
|
|
|
// Note that s does not contain the f32/64 suffix, and underscores have been stripped
|
2018-04-13 00:42:57 +00:00
|
|
|
s.chars()
|
2018-10-07 18:38:20 +00:00
|
|
|
.filter(|c| *c != '-' && *c != '.')
|
|
|
|
.take_while(|c| *c != 'e' && *c != 'E')
|
2018-04-13 00:42:57 +00:00
|
|
|
.fold(0, |count, c| {
|
|
|
|
// leading zeros
|
|
|
|
if c == '0' && count == 0 {
|
|
|
|
count
|
|
|
|
} else {
|
|
|
|
count + 1
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
enum FloatFormat {
|
|
|
|
LowerExp,
|
|
|
|
UpperExp,
|
|
|
|
Normal,
|
|
|
|
}
|
|
|
|
impl FloatFormat {
|
2019-09-18 06:37:41 +00:00
|
|
|
#[must_use]
|
2018-04-13 00:42:57 +00:00
|
|
|
fn new(s: &str) -> Self {
|
|
|
|
s.chars()
|
|
|
|
.find_map(|x| match x {
|
2019-07-31 00:25:35 +00:00
|
|
|
'e' => Some(Self::LowerExp),
|
|
|
|
'E' => Some(Self::UpperExp),
|
2018-04-13 00:42:57 +00:00
|
|
|
_ => None,
|
|
|
|
})
|
2019-07-31 00:25:35 +00:00
|
|
|
.unwrap_or(Self::Normal)
|
2018-04-13 00:42:57 +00:00
|
|
|
}
|
|
|
|
fn format<T>(&self, f: T) -> String
|
2018-11-27 20:14:15 +00:00
|
|
|
where
|
|
|
|
T: fmt::UpperExp + fmt::LowerExp + fmt::Display,
|
|
|
|
{
|
2018-04-13 00:42:57 +00:00
|
|
|
match self {
|
2019-07-31 00:25:35 +00:00
|
|
|
Self::LowerExp => format!("{:e}", f),
|
|
|
|
Self::UpperExp => format!("{:E}", f),
|
|
|
|
Self::Normal => format!("{}", f),
|
2018-04-13 00:42:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|