mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-22 20:53:21 +00:00
Separate out lint to check lossy whole number float literals
This commit is contained in:
parent
0d7ae7bd6f
commit
219c94d028
11 changed files with 214 additions and 145 deletions
|
@ -1207,6 +1207,7 @@ Released 2018-09-13
|
||||||
[`let_unit_value`]: https://rust-lang.github.io/rust-clippy/master/index.html#let_unit_value
|
[`let_unit_value`]: https://rust-lang.github.io/rust-clippy/master/index.html#let_unit_value
|
||||||
[`linkedlist`]: https://rust-lang.github.io/rust-clippy/master/index.html#linkedlist
|
[`linkedlist`]: https://rust-lang.github.io/rust-clippy/master/index.html#linkedlist
|
||||||
[`logic_bug`]: https://rust-lang.github.io/rust-clippy/master/index.html#logic_bug
|
[`logic_bug`]: https://rust-lang.github.io/rust-clippy/master/index.html#logic_bug
|
||||||
|
[`lossy_float_literal`]: https://rust-lang.github.io/rust-clippy/master/index.html#lossy_float_literal
|
||||||
[`main_recursion`]: https://rust-lang.github.io/rust-clippy/master/index.html#main_recursion
|
[`main_recursion`]: https://rust-lang.github.io/rust-clippy/master/index.html#main_recursion
|
||||||
[`manual_memcpy`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_memcpy
|
[`manual_memcpy`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_memcpy
|
||||||
[`manual_mul_add`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_mul_add
|
[`manual_mul_add`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_mul_add
|
||||||
|
|
|
@ -5,7 +5,7 @@
|
||||||
|
|
||||||
A collection of lints to catch common mistakes and improve your [Rust](https://github.com/rust-lang/rust) code.
|
A collection of lints to catch common mistakes and improve your [Rust](https://github.com/rust-lang/rust) code.
|
||||||
|
|
||||||
[There are 355 lints included in this crate!](https://rust-lang.github.io/rust-clippy/master/index.html)
|
[There are 356 lints included in this crate!](https://rust-lang.github.io/rust-clippy/master/index.html)
|
||||||
|
|
||||||
We have a bunch of lint categories to allow you to choose how much Clippy is supposed to ~~annoy~~ help you:
|
We have a bunch of lint categories to allow you to choose how much Clippy is supposed to ~~annoy~~ help you:
|
||||||
|
|
||||||
|
|
|
@ -13,8 +13,7 @@ declare_clippy_lint! {
|
||||||
/// **What it does:** Checks for float literals with a precision greater
|
/// **What it does:** Checks for float literals with a precision greater
|
||||||
/// than that supported by the underlying type.
|
/// than that supported by the underlying type.
|
||||||
///
|
///
|
||||||
/// **Why is this bad?** Rust will silently lose precision during conversion
|
/// **Why is this bad?** Rust will truncate the literal silently.
|
||||||
/// to a float.
|
|
||||||
///
|
///
|
||||||
/// **Known problems:** None.
|
/// **Known problems:** None.
|
||||||
///
|
///
|
||||||
|
@ -22,21 +21,45 @@ declare_clippy_lint! {
|
||||||
///
|
///
|
||||||
/// ```rust
|
/// ```rust
|
||||||
/// // Bad
|
/// // Bad
|
||||||
/// let a: f32 = 0.123_456_789_9; // 0.123_456_789
|
/// let v: f32 = 0.123_456_789_9;
|
||||||
/// let b: f32 = 16_777_217.0; // 16_777_216.0
|
/// println!("{}", v); // 0.123_456_789
|
||||||
///
|
///
|
||||||
/// // Good
|
/// // Good
|
||||||
/// let a: f64 = 0.123_456_789_9;
|
/// let v: f64 = 0.123_456_789_9;
|
||||||
/// let b: f64 = 16_777_216.0;
|
/// println!("{}", v); // 0.123_456_789_9
|
||||||
/// ```
|
/// ```
|
||||||
pub EXCESSIVE_PRECISION,
|
pub EXCESSIVE_PRECISION,
|
||||||
correctness,
|
style,
|
||||||
"excessive precision for float literal"
|
"excessive precision for float literal"
|
||||||
}
|
}
|
||||||
|
|
||||||
declare_lint_pass!(ExcessivePrecision => [EXCESSIVE_PRECISION]);
|
declare_clippy_lint! {
|
||||||
|
/// **What it does:** Checks for whole number float literals that
|
||||||
|
/// cannot be represented as the underlying type without loss.
|
||||||
|
///
|
||||||
|
/// **Why is this bad?** Rust will silently lose precision during
|
||||||
|
/// conversion to a float.
|
||||||
|
///
|
||||||
|
/// **Known problems:** None.
|
||||||
|
///
|
||||||
|
/// **Example:**
|
||||||
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// // Bad
|
||||||
|
/// let _: f32 = 16_777_217.0; // 16_777_216.0
|
||||||
|
///
|
||||||
|
/// // Good
|
||||||
|
/// let _: f32 = 16_777_216.0;
|
||||||
|
/// let _: f64 = 16_777_217.0;
|
||||||
|
/// ```
|
||||||
|
pub LOSSY_FLOAT_LITERAL,
|
||||||
|
restriction,
|
||||||
|
"lossy whole number float literals"
|
||||||
|
}
|
||||||
|
|
||||||
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ExcessivePrecision {
|
declare_lint_pass!(FloatLiteral => [EXCESSIVE_PRECISION, LOSSY_FLOAT_LITERAL]);
|
||||||
|
|
||||||
|
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for FloatLiteral {
|
||||||
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr<'_>) {
|
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr<'_>) {
|
||||||
if_chain! {
|
if_chain! {
|
||||||
let ty = cx.tables.expr_ty(expr);
|
let ty = cx.tables.expr_ty(expr);
|
||||||
|
@ -52,26 +75,41 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ExcessivePrecision {
|
||||||
// since we'll need the truncated string anyway.
|
// since we'll need the truncated string anyway.
|
||||||
let digits = count_digits(&sym_str);
|
let digits = count_digits(&sym_str);
|
||||||
let max = max_digits(fty);
|
let max = max_digits(fty);
|
||||||
let float_str = match fty {
|
|
||||||
FloatTy::F32 => sym_str.parse::<f32>().map(|f| formatter.format(f)),
|
|
||||||
FloatTy::F64 => sym_str.parse::<f64>().map(|f| formatter.format(f)),
|
|
||||||
}.unwrap();
|
|
||||||
let type_suffix = match lit_float_ty {
|
let type_suffix = match lit_float_ty {
|
||||||
LitFloatType::Suffixed(FloatTy::F32) => Some("f32"),
|
LitFloatType::Suffixed(FloatTy::F32) => Some("f32"),
|
||||||
LitFloatType::Suffixed(FloatTy::F64) => Some("f64"),
|
LitFloatType::Suffixed(FloatTy::F64) => Some("f64"),
|
||||||
_ => None
|
_ => None
|
||||||
};
|
};
|
||||||
|
let (is_whole, mut float_str) = match fty {
|
||||||
|
FloatTy::F32 => {
|
||||||
|
let value = sym_str.parse::<f32>().unwrap();
|
||||||
|
|
||||||
if is_whole_number(&sym_str, fty) {
|
(value.fract() == 0.0, formatter.format(value))
|
||||||
|
},
|
||||||
|
FloatTy::F64 => {
|
||||||
|
let value = sym_str.parse::<f64>().unwrap();
|
||||||
|
|
||||||
|
(value.fract() == 0.0, formatter.format(value))
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
if is_whole && !sym_str.contains(|c| c == 'e' || c == 'E') {
|
||||||
// Normalize the literal by stripping the fractional portion
|
// Normalize the literal by stripping the fractional portion
|
||||||
if sym_str.split('.').next().unwrap() != float_str {
|
if sym_str.split('.').next().unwrap() != float_str {
|
||||||
|
// 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");
|
||||||
|
}
|
||||||
|
|
||||||
span_lint_and_sugg(
|
span_lint_and_sugg(
|
||||||
cx,
|
cx,
|
||||||
EXCESSIVE_PRECISION,
|
LOSSY_FLOAT_LITERAL,
|
||||||
expr.span,
|
expr.span,
|
||||||
"literal cannot be represented as the underlying type without loss of precision",
|
"literal cannot be represented as the underlying type without loss of precision",
|
||||||
"consider changing the type or replacing it with",
|
"consider changing the type or replacing it with",
|
||||||
format_numeric_literal(format!("{}.0", float_str).as_str(), type_suffix, true),
|
format_numeric_literal(&float_str, type_suffix, true),
|
||||||
Applicability::MachineApplicable,
|
Applicability::MachineApplicable,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -91,15 +129,6 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ExcessivePrecision {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Checks whether a float literal is a whole number
|
|
||||||
#[must_use]
|
|
||||||
fn is_whole_number(sym_str: &str, fty: FloatTy) -> bool {
|
|
||||||
match fty {
|
|
||||||
FloatTy::F32 => sym_str.parse::<f32>().unwrap().fract() == 0.0,
|
|
||||||
FloatTy::F64 => sym_str.parse::<f64>().unwrap().fract() == 0.0,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[must_use]
|
#[must_use]
|
||||||
fn max_digits(fty: FloatTy) -> u32 {
|
fn max_digits(fty: FloatTy) -> u32 {
|
||||||
match fty {
|
match fty {
|
|
@ -205,10 +205,10 @@ pub mod escape;
|
||||||
pub mod eta_reduction;
|
pub mod eta_reduction;
|
||||||
pub mod eval_order_dependence;
|
pub mod eval_order_dependence;
|
||||||
pub mod excessive_bools;
|
pub mod excessive_bools;
|
||||||
pub mod excessive_precision;
|
|
||||||
pub mod exit;
|
pub mod exit;
|
||||||
pub mod explicit_write;
|
pub mod explicit_write;
|
||||||
pub mod fallible_impl_from;
|
pub mod fallible_impl_from;
|
||||||
|
pub mod float_literal;
|
||||||
pub mod format;
|
pub mod format;
|
||||||
pub mod formatting;
|
pub mod formatting;
|
||||||
pub mod functions;
|
pub mod functions;
|
||||||
|
@ -534,10 +534,11 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
|
||||||
&eval_order_dependence::EVAL_ORDER_DEPENDENCE,
|
&eval_order_dependence::EVAL_ORDER_DEPENDENCE,
|
||||||
&excessive_bools::FN_PARAMS_EXCESSIVE_BOOLS,
|
&excessive_bools::FN_PARAMS_EXCESSIVE_BOOLS,
|
||||||
&excessive_bools::STRUCT_EXCESSIVE_BOOLS,
|
&excessive_bools::STRUCT_EXCESSIVE_BOOLS,
|
||||||
&excessive_precision::EXCESSIVE_PRECISION,
|
|
||||||
&exit::EXIT,
|
&exit::EXIT,
|
||||||
&explicit_write::EXPLICIT_WRITE,
|
&explicit_write::EXPLICIT_WRITE,
|
||||||
&fallible_impl_from::FALLIBLE_IMPL_FROM,
|
&fallible_impl_from::FALLIBLE_IMPL_FROM,
|
||||||
|
&float_literal::EXCESSIVE_PRECISION,
|
||||||
|
&float_literal::LOSSY_FLOAT_LITERAL,
|
||||||
&format::USELESS_FORMAT,
|
&format::USELESS_FORMAT,
|
||||||
&formatting::POSSIBLE_MISSING_COMMA,
|
&formatting::POSSIBLE_MISSING_COMMA,
|
||||||
&formatting::SUSPICIOUS_ASSIGNMENT_FORMATTING,
|
&formatting::SUSPICIOUS_ASSIGNMENT_FORMATTING,
|
||||||
|
@ -836,7 +837,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
|
||||||
store.register_late_pass(|| box eq_op::EqOp);
|
store.register_late_pass(|| box eq_op::EqOp);
|
||||||
store.register_late_pass(|| box enum_glob_use::EnumGlobUse);
|
store.register_late_pass(|| box enum_glob_use::EnumGlobUse);
|
||||||
store.register_late_pass(|| box enum_clike::UnportableVariant);
|
store.register_late_pass(|| box enum_clike::UnportableVariant);
|
||||||
store.register_late_pass(|| box excessive_precision::ExcessivePrecision);
|
store.register_late_pass(|| box float_literal::FloatLiteral);
|
||||||
let verbose_bit_mask_threshold = conf.verbose_bit_mask_threshold;
|
let verbose_bit_mask_threshold = conf.verbose_bit_mask_threshold;
|
||||||
store.register_late_pass(move || box bit_mask::BitMask::new(verbose_bit_mask_threshold));
|
store.register_late_pass(move || box bit_mask::BitMask::new(verbose_bit_mask_threshold));
|
||||||
store.register_late_pass(|| box ptr::Ptr);
|
store.register_late_pass(|| box ptr::Ptr);
|
||||||
|
@ -1016,6 +1017,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
|
||||||
LintId::of(&dbg_macro::DBG_MACRO),
|
LintId::of(&dbg_macro::DBG_MACRO),
|
||||||
LintId::of(&else_if_without_else::ELSE_IF_WITHOUT_ELSE),
|
LintId::of(&else_if_without_else::ELSE_IF_WITHOUT_ELSE),
|
||||||
LintId::of(&exit::EXIT),
|
LintId::of(&exit::EXIT),
|
||||||
|
LintId::of(&float_literal::LOSSY_FLOAT_LITERAL),
|
||||||
LintId::of(&implicit_return::IMPLICIT_RETURN),
|
LintId::of(&implicit_return::IMPLICIT_RETURN),
|
||||||
LintId::of(&indexing_slicing::INDEXING_SLICING),
|
LintId::of(&indexing_slicing::INDEXING_SLICING),
|
||||||
LintId::of(&inherent_impl::MULTIPLE_INHERENT_IMPL),
|
LintId::of(&inherent_impl::MULTIPLE_INHERENT_IMPL),
|
||||||
|
@ -1159,8 +1161,8 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
|
||||||
LintId::of(&eta_reduction::REDUNDANT_CLOSURE),
|
LintId::of(&eta_reduction::REDUNDANT_CLOSURE),
|
||||||
LintId::of(&eval_order_dependence::DIVERGING_SUB_EXPRESSION),
|
LintId::of(&eval_order_dependence::DIVERGING_SUB_EXPRESSION),
|
||||||
LintId::of(&eval_order_dependence::EVAL_ORDER_DEPENDENCE),
|
LintId::of(&eval_order_dependence::EVAL_ORDER_DEPENDENCE),
|
||||||
LintId::of(&excessive_precision::EXCESSIVE_PRECISION),
|
|
||||||
LintId::of(&explicit_write::EXPLICIT_WRITE),
|
LintId::of(&explicit_write::EXPLICIT_WRITE),
|
||||||
|
LintId::of(&float_literal::EXCESSIVE_PRECISION),
|
||||||
LintId::of(&format::USELESS_FORMAT),
|
LintId::of(&format::USELESS_FORMAT),
|
||||||
LintId::of(&formatting::POSSIBLE_MISSING_COMMA),
|
LintId::of(&formatting::POSSIBLE_MISSING_COMMA),
|
||||||
LintId::of(&formatting::SUSPICIOUS_ASSIGNMENT_FORMATTING),
|
LintId::of(&formatting::SUSPICIOUS_ASSIGNMENT_FORMATTING),
|
||||||
|
@ -1386,6 +1388,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
|
||||||
LintId::of(&enum_variants::MODULE_INCEPTION),
|
LintId::of(&enum_variants::MODULE_INCEPTION),
|
||||||
LintId::of(&eq_op::OP_REF),
|
LintId::of(&eq_op::OP_REF),
|
||||||
LintId::of(&eta_reduction::REDUNDANT_CLOSURE),
|
LintId::of(&eta_reduction::REDUNDANT_CLOSURE),
|
||||||
|
LintId::of(&float_literal::EXCESSIVE_PRECISION),
|
||||||
LintId::of(&formatting::SUSPICIOUS_ASSIGNMENT_FORMATTING),
|
LintId::of(&formatting::SUSPICIOUS_ASSIGNMENT_FORMATTING),
|
||||||
LintId::of(&formatting::SUSPICIOUS_ELSE_FORMATTING),
|
LintId::of(&formatting::SUSPICIOUS_ELSE_FORMATTING),
|
||||||
LintId::of(&formatting::SUSPICIOUS_UNARY_OP_FORMATTING),
|
LintId::of(&formatting::SUSPICIOUS_UNARY_OP_FORMATTING),
|
||||||
|
@ -1566,7 +1569,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
|
||||||
LintId::of(&enum_clike::ENUM_CLIKE_UNPORTABLE_VARIANT),
|
LintId::of(&enum_clike::ENUM_CLIKE_UNPORTABLE_VARIANT),
|
||||||
LintId::of(&eq_op::EQ_OP),
|
LintId::of(&eq_op::EQ_OP),
|
||||||
LintId::of(&erasing_op::ERASING_OP),
|
LintId::of(&erasing_op::ERASING_OP),
|
||||||
LintId::of(&excessive_precision::EXCESSIVE_PRECISION),
|
|
||||||
LintId::of(&formatting::POSSIBLE_MISSING_COMMA),
|
LintId::of(&formatting::POSSIBLE_MISSING_COMMA),
|
||||||
LintId::of(&functions::NOT_UNSAFE_PTR_ARG_DEREF),
|
LintId::of(&functions::NOT_UNSAFE_PTR_ARG_DEREF),
|
||||||
LintId::of(&indexing_slicing::OUT_OF_BOUNDS_INDEXING),
|
LintId::of(&indexing_slicing::OUT_OF_BOUNDS_INDEXING),
|
||||||
|
|
|
@ -6,7 +6,7 @@ pub use lint::Lint;
|
||||||
pub use lint::LINT_LEVELS;
|
pub use lint::LINT_LEVELS;
|
||||||
|
|
||||||
// begin lint list, do not remove this comment, it’s used in `update_lints`
|
// begin lint list, do not remove this comment, it’s used in `update_lints`
|
||||||
pub const ALL_LINTS: [Lint; 355] = [
|
pub const ALL_LINTS: [Lint; 356] = [
|
||||||
Lint {
|
Lint {
|
||||||
name: "absurd_extreme_comparisons",
|
name: "absurd_extreme_comparisons",
|
||||||
group: "correctness",
|
group: "correctness",
|
||||||
|
@ -492,10 +492,10 @@ pub const ALL_LINTS: [Lint; 355] = [
|
||||||
},
|
},
|
||||||
Lint {
|
Lint {
|
||||||
name: "excessive_precision",
|
name: "excessive_precision",
|
||||||
group: "correctness",
|
group: "style",
|
||||||
desc: "excessive precision for float literal",
|
desc: "excessive precision for float literal",
|
||||||
deprecation: None,
|
deprecation: None,
|
||||||
module: "excessive_precision",
|
module: "float_literal",
|
||||||
},
|
},
|
||||||
Lint {
|
Lint {
|
||||||
name: "exit",
|
name: "exit",
|
||||||
|
@ -1001,6 +1001,13 @@ pub const ALL_LINTS: [Lint; 355] = [
|
||||||
deprecation: None,
|
deprecation: None,
|
||||||
module: "booleans",
|
module: "booleans",
|
||||||
},
|
},
|
||||||
|
Lint {
|
||||||
|
name: "lossy_float_literal",
|
||||||
|
group: "restriction",
|
||||||
|
desc: "lossy whole number float literals",
|
||||||
|
deprecation: None,
|
||||||
|
module: "float_literal",
|
||||||
|
},
|
||||||
Lint {
|
Lint {
|
||||||
name: "main_recursion",
|
name: "main_recursion",
|
||||||
group: "style",
|
group: "style",
|
||||||
|
|
|
@ -60,26 +60,4 @@ fn main() {
|
||||||
|
|
||||||
// issue #2840
|
// issue #2840
|
||||||
let num = 0.000_000_000_01e-10f64;
|
let num = 0.000_000_000_01e-10f64;
|
||||||
|
|
||||||
// Lossy whole-number float literals
|
|
||||||
let _: f32 = 16_777_216.0;
|
|
||||||
let _: f32 = 16_777_220.0;
|
|
||||||
let _: f32 = 16_777_220.0;
|
|
||||||
let _: f32 = 16_777_220.0;
|
|
||||||
let _ = 16_777_220.0_f32;
|
|
||||||
let _: f32 = -16_777_220.0;
|
|
||||||
let _: f64 = 9_007_199_254_740_992.0;
|
|
||||||
let _: f64 = 9_007_199_254_740_992.0;
|
|
||||||
let _: f64 = 9_007_199_254_740_992.0;
|
|
||||||
let _ = 9_007_199_254_740_992.0_f64;
|
|
||||||
let _: f64 = -9_007_199_254_740_992.0;
|
|
||||||
|
|
||||||
// Lossless whole number float literals
|
|
||||||
let _: f32 = 16_777_216.0;
|
|
||||||
let _: f32 = 16_777_218.0;
|
|
||||||
let _: f32 = 16_777_220.0;
|
|
||||||
let _: f32 = -16_777_216.0;
|
|
||||||
let _: f32 = -16_777_220.0;
|
|
||||||
let _: f64 = 9_007_199_254_740_992.0;
|
|
||||||
let _: f64 = -9_007_199_254_740_992.0;
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -60,26 +60,4 @@ fn main() {
|
||||||
|
|
||||||
// issue #2840
|
// issue #2840
|
||||||
let num = 0.000_000_000_01e-10f64;
|
let num = 0.000_000_000_01e-10f64;
|
||||||
|
|
||||||
// Lossy whole-number float literals
|
|
||||||
let _: f32 = 16_777_217.0;
|
|
||||||
let _: f32 = 16_777_219.0;
|
|
||||||
let _: f32 = 16_777_219.;
|
|
||||||
let _: f32 = 16_777_219.000;
|
|
||||||
let _ = 16_777_219f32;
|
|
||||||
let _: f32 = -16_777_219.0;
|
|
||||||
let _: f64 = 9_007_199_254_740_993.0;
|
|
||||||
let _: f64 = 9_007_199_254_740_993.;
|
|
||||||
let _: f64 = 9_007_199_254_740_993.000;
|
|
||||||
let _ = 9_007_199_254_740_993f64;
|
|
||||||
let _: f64 = -9_007_199_254_740_993.0;
|
|
||||||
|
|
||||||
// Lossless whole number float literals
|
|
||||||
let _: f32 = 16_777_216.0;
|
|
||||||
let _: f32 = 16_777_218.0;
|
|
||||||
let _: f32 = 16_777_220.0;
|
|
||||||
let _: f32 = -16_777_216.0;
|
|
||||||
let _: f32 = -16_777_220.0;
|
|
||||||
let _: f64 = 9_007_199_254_740_992.0;
|
|
||||||
let _: f64 = -9_007_199_254_740_992.0;
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -108,71 +108,5 @@ error: float has excessive precision
|
||||||
LL | let bad_bige32: f32 = 1.123_456_788_888E-10;
|
LL | let bad_bige32: f32 = 1.123_456_788_888E-10;
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^ help: consider changing the type or truncating it to: `1.123_456_8E-10`
|
| ^^^^^^^^^^^^^^^^^^^^^ help: consider changing the type or truncating it to: `1.123_456_8E-10`
|
||||||
|
|
||||||
error: literal cannot be represented as the underlying type without loss of precision
|
error: aborting due to 18 previous errors
|
||||||
--> $DIR/excessive_precision.rs:65:18
|
|
||||||
|
|
|
||||||
LL | let _: f32 = 16_777_217.0;
|
|
||||||
| ^^^^^^^^^^^^ help: consider changing the type or replacing it with: `16_777_216.0`
|
|
||||||
|
|
||||||
error: literal cannot be represented as the underlying type without loss of precision
|
|
||||||
--> $DIR/excessive_precision.rs:66:18
|
|
||||||
|
|
|
||||||
LL | let _: f32 = 16_777_219.0;
|
|
||||||
| ^^^^^^^^^^^^ help: consider changing the type or replacing it with: `16_777_220.0`
|
|
||||||
|
|
||||||
error: literal cannot be represented as the underlying type without loss of precision
|
|
||||||
--> $DIR/excessive_precision.rs:67:18
|
|
||||||
|
|
|
||||||
LL | let _: f32 = 16_777_219.;
|
|
||||||
| ^^^^^^^^^^^ help: consider changing the type or replacing it with: `16_777_220.0`
|
|
||||||
|
|
||||||
error: literal cannot be represented as the underlying type without loss of precision
|
|
||||||
--> $DIR/excessive_precision.rs:68:18
|
|
||||||
|
|
|
||||||
LL | let _: f32 = 16_777_219.000;
|
|
||||||
| ^^^^^^^^^^^^^^ help: consider changing the type or replacing it with: `16_777_220.0`
|
|
||||||
|
|
||||||
error: literal cannot be represented as the underlying type without loss of precision
|
|
||||||
--> $DIR/excessive_precision.rs:69:13
|
|
||||||
|
|
|
||||||
LL | let _ = 16_777_219f32;
|
|
||||||
| ^^^^^^^^^^^^^ help: consider changing the type or replacing it with: `16_777_220.0_f32`
|
|
||||||
|
|
||||||
error: literal cannot be represented as the underlying type without loss of precision
|
|
||||||
--> $DIR/excessive_precision.rs:70:19
|
|
||||||
|
|
|
||||||
LL | let _: f32 = -16_777_219.0;
|
|
||||||
| ^^^^^^^^^^^^ help: consider changing the type or replacing it with: `16_777_220.0`
|
|
||||||
|
|
||||||
error: literal cannot be represented as the underlying type without loss of precision
|
|
||||||
--> $DIR/excessive_precision.rs:71:18
|
|
||||||
|
|
|
||||||
LL | let _: f64 = 9_007_199_254_740_993.0;
|
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^^^ help: consider changing the type or replacing it with: `9_007_199_254_740_992.0`
|
|
||||||
|
|
||||||
error: literal cannot be represented as the underlying type without loss of precision
|
|
||||||
--> $DIR/excessive_precision.rs:72:18
|
|
||||||
|
|
|
||||||
LL | let _: f64 = 9_007_199_254_740_993.;
|
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^^ help: consider changing the type or replacing it with: `9_007_199_254_740_992.0`
|
|
||||||
|
|
||||||
error: literal cannot be represented as the underlying type without loss of precision
|
|
||||||
--> $DIR/excessive_precision.rs:73:18
|
|
||||||
|
|
|
||||||
LL | let _: f64 = 9_007_199_254_740_993.000;
|
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider changing the type or replacing it with: `9_007_199_254_740_992.0`
|
|
||||||
|
|
||||||
error: literal cannot be represented as the underlying type without loss of precision
|
|
||||||
--> $DIR/excessive_precision.rs:74:13
|
|
||||||
|
|
|
||||||
LL | let _ = 9_007_199_254_740_993f64;
|
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: consider changing the type or replacing it with: `9_007_199_254_740_992.0_f64`
|
|
||||||
|
|
||||||
error: literal cannot be represented as the underlying type without loss of precision
|
|
||||||
--> $DIR/excessive_precision.rs:75:19
|
|
||||||
|
|
|
||||||
LL | let _: f64 = -9_007_199_254_740_993.0;
|
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^^^ help: consider changing the type or replacing it with: `9_007_199_254_740_992.0`
|
|
||||||
|
|
||||||
error: aborting due to 29 previous errors
|
|
||||||
|
|
||||||
|
|
35
tests/ui/lossy_float_literal.fixed
Normal file
35
tests/ui/lossy_float_literal.fixed
Normal file
|
@ -0,0 +1,35 @@
|
||||||
|
// run-rustfix
|
||||||
|
#![warn(clippy::lossy_float_literal)]
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
// Lossy whole-number float literals
|
||||||
|
let _: f32 = 16_777_216.0;
|
||||||
|
let _: f32 = 16_777_220.0;
|
||||||
|
let _: f32 = 16_777_220.0;
|
||||||
|
let _: f32 = 16_777_220.0;
|
||||||
|
let _ = 16_777_220_f32;
|
||||||
|
let _: f32 = -16_777_220.0;
|
||||||
|
let _: f64 = 9_007_199_254_740_992.0;
|
||||||
|
let _: f64 = 9_007_199_254_740_992.0;
|
||||||
|
let _: f64 = 9_007_199_254_740_992.0;
|
||||||
|
let _ = 9_007_199_254_740_992_f64;
|
||||||
|
let _: f64 = -9_007_199_254_740_992.0;
|
||||||
|
|
||||||
|
// Lossless whole number float literals
|
||||||
|
let _: f32 = 16_777_216.0;
|
||||||
|
let _: f32 = 16_777_218.0;
|
||||||
|
let _: f32 = 16_777_220.0;
|
||||||
|
let _: f32 = -16_777_216.0;
|
||||||
|
let _: f32 = -16_777_220.0;
|
||||||
|
let _: f64 = 16_777_217.0;
|
||||||
|
let _: f64 = -16_777_217.0;
|
||||||
|
let _: f64 = 9_007_199_254_740_992.0;
|
||||||
|
let _: f64 = -9_007_199_254_740_992.0;
|
||||||
|
|
||||||
|
// Ignored whole number float literals
|
||||||
|
let _: f32 = 1e25;
|
||||||
|
let _: f32 = 1E25;
|
||||||
|
let _: f64 = 1e99;
|
||||||
|
let _: f64 = 1E99;
|
||||||
|
let _: f32 = 0.1;
|
||||||
|
}
|
35
tests/ui/lossy_float_literal.rs
Normal file
35
tests/ui/lossy_float_literal.rs
Normal file
|
@ -0,0 +1,35 @@
|
||||||
|
// run-rustfix
|
||||||
|
#![warn(clippy::lossy_float_literal)]
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
// Lossy whole-number float literals
|
||||||
|
let _: f32 = 16_777_217.0;
|
||||||
|
let _: f32 = 16_777_219.0;
|
||||||
|
let _: f32 = 16_777_219.;
|
||||||
|
let _: f32 = 16_777_219.000;
|
||||||
|
let _ = 16_777_219f32;
|
||||||
|
let _: f32 = -16_777_219.0;
|
||||||
|
let _: f64 = 9_007_199_254_740_993.0;
|
||||||
|
let _: f64 = 9_007_199_254_740_993.;
|
||||||
|
let _: f64 = 9_007_199_254_740_993.00;
|
||||||
|
let _ = 9_007_199_254_740_993f64;
|
||||||
|
let _: f64 = -9_007_199_254_740_993.0;
|
||||||
|
|
||||||
|
// Lossless whole number float literals
|
||||||
|
let _: f32 = 16_777_216.0;
|
||||||
|
let _: f32 = 16_777_218.0;
|
||||||
|
let _: f32 = 16_777_220.0;
|
||||||
|
let _: f32 = -16_777_216.0;
|
||||||
|
let _: f32 = -16_777_220.0;
|
||||||
|
let _: f64 = 16_777_217.0;
|
||||||
|
let _: f64 = -16_777_217.0;
|
||||||
|
let _: f64 = 9_007_199_254_740_992.0;
|
||||||
|
let _: f64 = -9_007_199_254_740_992.0;
|
||||||
|
|
||||||
|
// Ignored whole number float literals
|
||||||
|
let _: f32 = 1e25;
|
||||||
|
let _: f32 = 1E25;
|
||||||
|
let _: f64 = 1e99;
|
||||||
|
let _: f64 = 1E99;
|
||||||
|
let _: f32 = 0.1;
|
||||||
|
}
|
70
tests/ui/lossy_float_literal.stderr
Normal file
70
tests/ui/lossy_float_literal.stderr
Normal file
|
@ -0,0 +1,70 @@
|
||||||
|
error: literal cannot be represented as the underlying type without loss of precision
|
||||||
|
--> $DIR/lossy_float_literal.rs:6:18
|
||||||
|
|
|
||||||
|
LL | let _: f32 = 16_777_217.0;
|
||||||
|
| ^^^^^^^^^^^^ help: consider changing the type or replacing it with: `16_777_216.0`
|
||||||
|
|
|
||||||
|
= note: `-D clippy::lossy-float-literal` implied by `-D warnings`
|
||||||
|
|
||||||
|
error: literal cannot be represented as the underlying type without loss of precision
|
||||||
|
--> $DIR/lossy_float_literal.rs:7:18
|
||||||
|
|
|
||||||
|
LL | let _: f32 = 16_777_219.0;
|
||||||
|
| ^^^^^^^^^^^^ help: consider changing the type or replacing it with: `16_777_220.0`
|
||||||
|
|
||||||
|
error: literal cannot be represented as the underlying type without loss of precision
|
||||||
|
--> $DIR/lossy_float_literal.rs:8:18
|
||||||
|
|
|
||||||
|
LL | let _: f32 = 16_777_219.;
|
||||||
|
| ^^^^^^^^^^^ help: consider changing the type or replacing it with: `16_777_220.0`
|
||||||
|
|
||||||
|
error: literal cannot be represented as the underlying type without loss of precision
|
||||||
|
--> $DIR/lossy_float_literal.rs:9:18
|
||||||
|
|
|
||||||
|
LL | let _: f32 = 16_777_219.000;
|
||||||
|
| ^^^^^^^^^^^^^^ help: consider changing the type or replacing it with: `16_777_220.0`
|
||||||
|
|
||||||
|
error: literal cannot be represented as the underlying type without loss of precision
|
||||||
|
--> $DIR/lossy_float_literal.rs:10:13
|
||||||
|
|
|
||||||
|
LL | let _ = 16_777_219f32;
|
||||||
|
| ^^^^^^^^^^^^^ help: consider changing the type or replacing it with: `16_777_220_f32`
|
||||||
|
|
||||||
|
error: literal cannot be represented as the underlying type without loss of precision
|
||||||
|
--> $DIR/lossy_float_literal.rs:11:19
|
||||||
|
|
|
||||||
|
LL | let _: f32 = -16_777_219.0;
|
||||||
|
| ^^^^^^^^^^^^ help: consider changing the type or replacing it with: `16_777_220.0`
|
||||||
|
|
||||||
|
error: literal cannot be represented as the underlying type without loss of precision
|
||||||
|
--> $DIR/lossy_float_literal.rs:12:18
|
||||||
|
|
|
||||||
|
LL | let _: f64 = 9_007_199_254_740_993.0;
|
||||||
|
| ^^^^^^^^^^^^^^^^^^^^^^^ help: consider changing the type or replacing it with: `9_007_199_254_740_992.0`
|
||||||
|
|
||||||
|
error: literal cannot be represented as the underlying type without loss of precision
|
||||||
|
--> $DIR/lossy_float_literal.rs:13:18
|
||||||
|
|
|
||||||
|
LL | let _: f64 = 9_007_199_254_740_993.;
|
||||||
|
| ^^^^^^^^^^^^^^^^^^^^^^ help: consider changing the type or replacing it with: `9_007_199_254_740_992.0`
|
||||||
|
|
||||||
|
error: literal cannot be represented as the underlying type without loss of precision
|
||||||
|
--> $DIR/lossy_float_literal.rs:14:18
|
||||||
|
|
|
||||||
|
LL | let _: f64 = 9_007_199_254_740_993.00;
|
||||||
|
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: consider changing the type or replacing it with: `9_007_199_254_740_992.0`
|
||||||
|
|
||||||
|
error: literal cannot be represented as the underlying type without loss of precision
|
||||||
|
--> $DIR/lossy_float_literal.rs:15:13
|
||||||
|
|
|
||||||
|
LL | let _ = 9_007_199_254_740_993f64;
|
||||||
|
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: consider changing the type or replacing it with: `9_007_199_254_740_992_f64`
|
||||||
|
|
||||||
|
error: literal cannot be represented as the underlying type without loss of precision
|
||||||
|
--> $DIR/lossy_float_literal.rs:16:19
|
||||||
|
|
|
||||||
|
LL | let _: f64 = -9_007_199_254_740_993.0;
|
||||||
|
| ^^^^^^^^^^^^^^^^^^^^^^^ help: consider changing the type or replacing it with: `9_007_199_254_740_992.0`
|
||||||
|
|
||||||
|
error: aborting due to 11 previous errors
|
||||||
|
|
Loading…
Reference in a new issue