2021-03-25 13:29:11 -05:00
|
|
|
use clippy_utils::diagnostics::span_lint_and_sugg;
|
|
|
|
use clippy_utils::numeric_literal;
|
2018-07-19 03:00:54 -05:00
|
|
|
use if_chain::if_chain;
|
2020-12-12 08:32:45 -06:00
|
|
|
use rustc_ast::ast::{self, LitFloatType, LitKind};
|
2018-12-29 09:04:45 -06:00
|
|
|
use rustc_errors::Applicability;
|
2020-01-06 10:39:50 -06:00
|
|
|
use rustc_hir as hir;
|
2020-01-12 00:08:41 -06:00
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
2020-12-12 08:32:45 -06:00
|
|
|
use rustc_middle::ty::{self, FloatTy};
|
2020-01-11 05:37:08 -06:00
|
|
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
2020-04-07 16:44:24 -05:00
|
|
|
use std::fmt;
|
2018-04-12 19:42:57 -05:00
|
|
|
|
|
|
|
declare_clippy_lint! {
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### What it does
|
|
|
|
/// Checks for float literals with a precision greater
|
2020-02-17 01:48:10 -06:00
|
|
|
/// than that supported by the underlying type.
|
2019-03-05 10:50:33 -06:00
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Why is this bad?
|
|
|
|
/// Rust will truncate the literal silently.
|
2019-03-05 10:50:33 -06:00
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Example
|
2019-03-05 10:50:33 -06:00
|
|
|
/// ```rust
|
2020-02-20 20:32:06 -06:00
|
|
|
/// let v: f32 = 0.123_456_789_9;
|
|
|
|
/// println!("{}", v); // 0.123_456_789
|
2022-06-04 06:34:07 -05:00
|
|
|
/// ```
|
2019-03-05 10:50:33 -06:00
|
|
|
///
|
2022-06-04 06:34:07 -05:00
|
|
|
/// Use instead:
|
|
|
|
/// ```rust
|
2020-02-20 20:32:06 -06:00
|
|
|
/// let v: f64 = 0.123_456_789_9;
|
|
|
|
/// println!("{}", v); // 0.123_456_789_9
|
2019-03-05 10:50:33 -06:00
|
|
|
/// ```
|
2021-12-06 05:33:31 -06:00
|
|
|
#[clippy::version = "pre 1.29.0"]
|
2018-04-12 19:42:57 -05:00
|
|
|
pub EXCESSIVE_PRECISION,
|
2020-02-20 20:32:06 -06:00
|
|
|
style,
|
2018-04-12 19:42:57 -05:00
|
|
|
"excessive precision for float literal"
|
|
|
|
}
|
|
|
|
|
2020-02-20 20:32:06 -06:00
|
|
|
declare_clippy_lint! {
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### What it does
|
|
|
|
/// Checks for whole number float literals that
|
2020-02-20 20:32:06 -06:00
|
|
|
/// cannot be represented as the underlying type without loss.
|
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Why is this bad?
|
|
|
|
/// Rust will silently lose precision during
|
2020-02-20 20:32:06 -06:00
|
|
|
/// conversion to a float.
|
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Example
|
2020-02-20 20:32:06 -06:00
|
|
|
/// ```rust
|
|
|
|
/// let _: f32 = 16_777_217.0; // 16_777_216.0
|
2022-06-16 10:39:06 -05:00
|
|
|
/// ```
|
2020-02-20 20:32:06 -06:00
|
|
|
///
|
2022-06-16 10:39:06 -05:00
|
|
|
/// Use instead:
|
|
|
|
/// ```rust
|
2020-02-20 20:32:06 -06:00
|
|
|
/// let _: f32 = 16_777_216.0;
|
|
|
|
/// let _: f64 = 16_777_217.0;
|
|
|
|
/// ```
|
2021-12-06 05:33:31 -06:00
|
|
|
#[clippy::version = "1.43.0"]
|
2020-02-20 20:32:06 -06:00
|
|
|
pub LOSSY_FLOAT_LITERAL,
|
|
|
|
restriction,
|
|
|
|
"lossy whole number float literals"
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_lint_pass!(FloatLiteral => [EXCESSIVE_PRECISION, LOSSY_FLOAT_LITERAL]);
|
2018-04-12 19:42:57 -05:00
|
|
|
|
2020-06-25 15:41:36 -05:00
|
|
|
impl<'tcx> LateLintPass<'tcx> for FloatLiteral {
|
|
|
|
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>) {
|
2021-04-08 10:50:13 -05:00
|
|
|
let ty = cx.typeck_results().expr_ty(expr);
|
2018-04-12 19:42:57 -05:00
|
|
|
if_chain! {
|
2020-08-03 17:18:29 -05:00
|
|
|
if let ty::Float(fty) = *ty.kind();
|
2019-09-27 10:16:06 -05:00
|
|
|
if let hir::ExprKind::Lit(ref lit) = expr.kind;
|
2020-02-17 01:48:10 -06:00
|
|
|
if let LitKind::Float(sym, lit_float_ty) = lit.node;
|
2018-04-12 19:42:57 -05:00
|
|
|
then {
|
2020-02-17 01:48:10 -06:00
|
|
|
let sym_str = sym.as_str();
|
2021-12-14 21:39:23 -06:00
|
|
|
let formatter = FloatFormat::new(sym_str);
|
2020-02-17 01:48:10 -06: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.
|
2021-12-30 08:10:43 -06:00
|
|
|
let digits = count_digits(sym_str);
|
2020-02-17 01:48:10 -06:00
|
|
|
let max = max_digits(fty);
|
|
|
|
let type_suffix = match lit_float_ty {
|
2020-12-12 08:32:45 -06:00
|
|
|
LitFloatType::Suffixed(ast::FloatTy::F32) => Some("f32"),
|
|
|
|
LitFloatType::Suffixed(ast::FloatTy::F64) => Some("f64"),
|
2020-05-28 08:45:24 -05:00
|
|
|
LitFloatType::Unsuffixed => None
|
2020-02-17 01:48:10 -06:00
|
|
|
};
|
2020-02-20 20:32:06 -06:00
|
|
|
let (is_whole, mut float_str) = match fty {
|
|
|
|
FloatTy::F32 => {
|
|
|
|
let value = sym_str.parse::<f32>().unwrap();
|
|
|
|
|
|
|
|
(value.fract() == 0.0, formatter.format(value))
|
|
|
|
},
|
|
|
|
FloatTy::F64 => {
|
|
|
|
let value = sym_str.parse::<f64>().unwrap();
|
|
|
|
|
|
|
|
(value.fract() == 0.0, formatter.format(value))
|
|
|
|
},
|
|
|
|
};
|
2018-04-12 19:42:57 -05:00
|
|
|
|
2020-02-20 20:32:06 -06:00
|
|
|
if is_whole && !sym_str.contains(|c| c == 'e' || c == 'E') {
|
2020-02-17 01:48:10 -06:00
|
|
|
// Normalize the literal by stripping the fractional portion
|
|
|
|
if sym_str.split('.').next().unwrap() != float_str {
|
2020-02-20 20:32:06 -06:00
|
|
|
// If the type suffix is missing the suggestion would be
|
|
|
|
// incorrectly interpreted as an integer so adding a `.0`
|
|
|
|
// suffix to prevent that.
|
|
|
|
if type_suffix.is_none() {
|
|
|
|
float_str.push_str(".0");
|
|
|
|
}
|
|
|
|
|
2020-02-17 01:48:10 -06:00
|
|
|
span_lint_and_sugg(
|
|
|
|
cx,
|
2020-02-20 20:32:06 -06:00
|
|
|
LOSSY_FLOAT_LITERAL,
|
2020-02-17 01:48:10 -06:00
|
|
|
expr.span,
|
|
|
|
"literal cannot be represented as the underlying type without loss of precision",
|
|
|
|
"consider changing the type or replacing it with",
|
2020-03-03 10:58:37 -06:00
|
|
|
numeric_literal::format(&float_str, type_suffix, true),
|
2020-02-17 01:48:10 -06:00
|
|
|
Applicability::MachineApplicable,
|
|
|
|
);
|
|
|
|
}
|
2021-09-28 12:03:12 -05:00
|
|
|
} else if digits > max as usize && float_str.len() < sym_str.len() {
|
2020-02-17 01:48:10 -06:00
|
|
|
span_lint_and_sugg(
|
|
|
|
cx,
|
|
|
|
EXCESSIVE_PRECISION,
|
|
|
|
expr.span,
|
|
|
|
"float has excessive precision",
|
|
|
|
"consider changing the type or truncating it to",
|
2020-03-03 10:58:37 -06:00
|
|
|
numeric_literal::format(&float_str, type_suffix, true),
|
2020-02-17 01:48:10 -06:00
|
|
|
Applicability::MachineApplicable,
|
|
|
|
);
|
|
|
|
}
|
2018-04-12 19:42:57 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-18 01:37:41 -05:00
|
|
|
#[must_use]
|
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
|
2019-09-18 01:37:41 -05:00
|
|
|
#[must_use]
|
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
|
2021-03-12 08:30:50 -06:00
|
|
|
if c == '0' && count == 0 { count } else { count + 1 }
|
2018-04-12 19:42:57 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
enum FloatFormat {
|
|
|
|
LowerExp,
|
|
|
|
UpperExp,
|
|
|
|
Normal,
|
|
|
|
}
|
|
|
|
impl FloatFormat {
|
2019-09-18 01:37:41 -05:00
|
|
|
#[must_use]
|
2018-04-12 19:42:57 -05:00
|
|
|
fn new(s: &str) -> Self {
|
|
|
|
s.chars()
|
|
|
|
.find_map(|x| match x {
|
2019-07-30 19:25:35 -05:00
|
|
|
'e' => Some(Self::LowerExp),
|
|
|
|
'E' => Some(Self::UpperExp),
|
2018-04-12 19:42:57 -05:00
|
|
|
_ => None,
|
|
|
|
})
|
2019-07-30 19:25:35 -05:00
|
|
|
.unwrap_or(Self::Normal)
|
2018-04-12 19:42:57 -05:00
|
|
|
}
|
|
|
|
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 {
|
2022-10-06 02:44:38 -05:00
|
|
|
Self::LowerExp => format!("{f:e}"),
|
|
|
|
Self::UpperExp => format!("{f:E}"),
|
|
|
|
Self::Normal => format!("{f}"),
|
2018-04-12 19:42:57 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|