2018-10-06 11:18:06 -05:00
|
|
|
// Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2018-09-15 02:21:58 -05:00
|
|
|
use crate::rustc::hir;
|
|
|
|
use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
|
2018-12-04 16:19:42 -06:00
|
|
|
use crate::rustc::ty;
|
2018-09-15 02:21:58 -05:00
|
|
|
use crate::rustc::{declare_tool_lint, lint_array};
|
2018-11-20 07:06:29 -06:00
|
|
|
use crate::rustc_errors::Applicability;
|
|
|
|
use crate::syntax::ast::*;
|
|
|
|
use crate::syntax_pos::symbol::Symbol;
|
|
|
|
use crate::utils::span_lint_and_sugg;
|
2018-07-19 03:00:54 -05:00
|
|
|
use if_chain::if_chain;
|
2018-04-12 19:42:57 -05:00
|
|
|
use std::f32;
|
|
|
|
use std::f64;
|
|
|
|
use std::fmt;
|
|
|
|
|
|
|
|
/// **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
|
2018-11-27 14:14:15 -06:00
|
|
|
/// let v: f32 = 0.123_456_789_9;
|
|
|
|
/// println!("{}", v); // 0.123_456_789
|
2018-04-12 19:42:57 -05:00
|
|
|
///
|
|
|
|
/// // Good
|
2018-11-27 14:14:15 -06:00
|
|
|
/// let v: f64 = 0.123_456_789_9;
|
|
|
|
/// println!("{}", v); // 0.123_456_789_9
|
2018-04-12 19:42:57 -05:00
|
|
|
/// ```
|
|
|
|
declare_clippy_lint! {
|
|
|
|
pub EXCESSIVE_PRECISION,
|
|
|
|
style,
|
|
|
|
"excessive precision for float literal"
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct ExcessivePrecision;
|
|
|
|
|
|
|
|
impl LintPass for ExcessivePrecision {
|
|
|
|
fn get_lints(&self) -> LintArray {
|
|
|
|
lint_array!(EXCESSIVE_PRECISION)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ExcessivePrecision {
|
|
|
|
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr) {
|
|
|
|
if_chain! {
|
|
|
|
let ty = cx.tables.expr_ty(expr);
|
2018-12-04 16:19:42 -06:00
|
|
|
if let ty::Float(fty) = ty.sty;
|
2018-07-12 02:30:57 -05:00
|
|
|
if let hir::ExprKind::Lit(ref lit) = expr.node;
|
2018-05-31 13:15:48 -05:00
|
|
|
if let LitKind::Float(sym, _) | LitKind::FloatUnsuffixed(sym) = lit.node;
|
2018-04-12 19:42:57 -05:00
|
|
|
if let Some(sugg) = self.check(sym, fty);
|
|
|
|
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 08:13:57 -06:00
|
|
|
Applicability::MachineApplicable,
|
2018-04-12 19:42:57 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ExcessivePrecision {
|
2018-07-25 13:02:52 -05:00
|
|
|
// None if nothing to lint, Some(suggestion) if lint necessary
|
2018-05-31 13:15:48 -05:00
|
|
|
fn check(&self, sym: Symbol, fty: FloatTy) -> Option<String> {
|
2018-04-12 19:42:57 -05:00
|
|
|
let max = max_digits(fty);
|
|
|
|
let sym_str = sym.as_str();
|
2018-05-02 13:40:52 -05:00
|
|
|
if dot_zero_exclusion(&sym_str) {
|
2018-11-27 14:14:15 -06:00
|
|
|
return None;
|
2018-05-02 13:40:52 -05:00
|
|
|
}
|
2018-04-12 19:42:57 -05: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 13:40:52 -05:00
|
|
|
let digits = count_digits(&sym_str);
|
2018-04-12 19:42:57 -05:00
|
|
|
if digits > max as usize {
|
2018-05-02 13:40:52 -05:00
|
|
|
let formatter = FloatFormat::new(&sym_str);
|
2018-05-31 13:15:48 -05:00
|
|
|
let sr = match fty {
|
2018-04-12 19:42:57 -05: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 {
|
2018-05-02 13:40:52 -05:00
|
|
|
let di = super::literal_representation::DigitInfo::new(&s, true);
|
|
|
|
Some(di.grouping_hint())
|
2018-04-12 19:42:57 -05:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-29 09:39:30 -05:00
|
|
|
/// Should we exclude the float because it has a `.0` or `.` suffix
|
2018-12-07 15:47:12 -06:00
|
|
|
/// Ex `1_000_000_000.0`
|
|
|
|
/// Ex `1_000_000_000.`
|
2018-05-02 13:40:52 -05:00
|
|
|
fn dot_zero_exclusion(s: &str) -> bool {
|
|
|
|
if let Some(after_dec) = s.split('.').nth(1) {
|
2018-11-27 14:14:15 -06:00
|
|
|
let mut decpart = after_dec.chars().take_while(|c| *c != 'e' || *c != 'E');
|
2018-05-02 13:40:52 -05:00
|
|
|
|
|
|
|
match decpart.next() {
|
|
|
|
Some('0') => decpart.count() == 0,
|
2018-09-29 09:39:30 -05:00
|
|
|
Some(_) => false,
|
|
|
|
None => true,
|
2018-05-02 13:40:52 -05:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-31 13:15:48 -05:00
|
|
|
fn max_digits(fty: FloatTy) -> u32 {
|
2018-04-12 19:42:57 -05:00
|
|
|
match fty {
|
|
|
|
FloatTy::F32 => f32::DIGITS,
|
|
|
|
FloatTy::F64 => f64::DIGITS,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-02 13:40:52 -05:00
|
|
|
/// Counts the digits excluding leading zeros
|
2018-04-12 19:42:57 -05:00
|
|
|
fn count_digits(s: &str) -> usize {
|
2018-10-07 13:38:20 -05:00
|
|
|
// Note that s does not contain the f32/64 suffix, and underscores have been stripped
|
2018-04-12 19:42:57 -05:00
|
|
|
s.chars()
|
2018-10-07 13:38:20 -05:00
|
|
|
.filter(|c| *c != '-' && *c != '.')
|
|
|
|
.take_while(|c| *c != 'e' && *c != 'E')
|
2018-04-12 19:42:57 -05:00
|
|
|
.fold(0, |count, c| {
|
|
|
|
// leading zeros
|
|
|
|
if c == '0' && count == 0 {
|
|
|
|
count
|
|
|
|
} else {
|
|
|
|
count + 1
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
enum FloatFormat {
|
|
|
|
LowerExp,
|
|
|
|
UpperExp,
|
|
|
|
Normal,
|
|
|
|
}
|
|
|
|
impl FloatFormat {
|
|
|
|
fn new(s: &str) -> Self {
|
|
|
|
s.chars()
|
|
|
|
.find_map(|x| match x {
|
|
|
|
'e' => Some(FloatFormat::LowerExp),
|
|
|
|
'E' => Some(FloatFormat::UpperExp),
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.unwrap_or(FloatFormat::Normal)
|
|
|
|
}
|
|
|
|
fn format<T>(&self, f: T) -> String
|
2018-11-27 14:14:15 -06:00
|
|
|
where
|
|
|
|
T: fmt::UpperExp + fmt::LowerExp + fmt::Display,
|
|
|
|
{
|
2018-04-12 19:42:57 -05:00
|
|
|
match self {
|
|
|
|
FloatFormat::LowerExp => format!("{:e}", f),
|
|
|
|
FloatFormat::UpperExp => format!("{:E}", f),
|
|
|
|
FloatFormat::Normal => format!("{}", f),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|