From 921f4d317ec3498b0583a72ba1dfdfd84699c268 Mon Sep 17 00:00:00 2001 From: koka Date: Fri, 18 Nov 2022 21:23:16 +0900 Subject: [PATCH 1/2] Keep original literal notation in suggestion --- clippy_lints/src/unused_rounding.rs | 2 +- tests/ui/unused_rounding.fixed | 5 +++++ tests/ui/unused_rounding.rs | 5 +++++ tests/ui/unused_rounding.stderr | 8 +++++++- 4 files changed, 18 insertions(+), 2 deletions(-) diff --git a/clippy_lints/src/unused_rounding.rs b/clippy_lints/src/unused_rounding.rs index 316493729..0f6f36da8 100644 --- a/clippy_lints/src/unused_rounding.rs +++ b/clippy_lints/src/unused_rounding.rs @@ -36,7 +36,7 @@ fn is_useless_rounding(expr: &Expr) -> Option<(&str, String)> { && let ExprKind::Lit(spanned) = &receiver.kind && let LitKind::Float(symbol, ty) = spanned.kind { let f = symbol.as_str().parse::().unwrap(); - let f_str = symbol.to_string() + if let LitFloatType::Suffixed(ty) = ty { + let f_str = spanned.token_lit.symbol.to_string() + if let LitFloatType::Suffixed(ty) = ty { ty.name_str() } else { "" diff --git a/tests/ui/unused_rounding.fixed b/tests/ui/unused_rounding.fixed index 54f85806a..38fe6c34c 100644 --- a/tests/ui/unused_rounding.fixed +++ b/tests/ui/unused_rounding.fixed @@ -6,4 +6,9 @@ fn main() { let _ = 1.0f64; let _ = 1.00f32; let _ = 2e-54f64.floor(); + + // issue9866 + let _ = 3.3_f32.round(); + let _ = 3.3_f64.round(); + let _ = 3.0_f32; } diff --git a/tests/ui/unused_rounding.rs b/tests/ui/unused_rounding.rs index 8d007bc4a..a5cac64d0 100644 --- a/tests/ui/unused_rounding.rs +++ b/tests/ui/unused_rounding.rs @@ -6,4 +6,9 @@ fn main() { let _ = 1.0f64.floor(); let _ = 1.00f32.round(); let _ = 2e-54f64.floor(); + + // issue9866 + let _ = 3.3_f32.round(); + let _ = 3.3_f64.round(); + let _ = 3.0_f32.round(); } diff --git a/tests/ui/unused_rounding.stderr b/tests/ui/unused_rounding.stderr index 6cfb02e04..1eeb5d1de 100644 --- a/tests/ui/unused_rounding.stderr +++ b/tests/ui/unused_rounding.stderr @@ -18,5 +18,11 @@ error: used the `round` method with a whole number float LL | let _ = 1.00f32.round(); | ^^^^^^^^^^^^^^^ help: remove the `round` method call: `1.00f32` -error: aborting due to 3 previous errors +error: used the `round` method with a whole number float + --> $DIR/unused_rounding.rs:13:13 + | +LL | let _ = 3.0_f32.round(); + | ^^^^^^^^^^^^^^^ help: remove the `round` method call: `3.0_f32` + +error: aborting due to 4 previous errors From 1baa6cd591982471475895fc3256d5162a651628 Mon Sep 17 00:00:00 2001 From: koka Date: Sat, 19 Nov 2022 21:21:47 +0900 Subject: [PATCH 2/2] refac: grab a snip from receiver --- clippy_lints/src/unused_rounding.rs | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/clippy_lints/src/unused_rounding.rs b/clippy_lints/src/unused_rounding.rs index 0f6f36da8..cf34a57f9 100644 --- a/clippy_lints/src/unused_rounding.rs +++ b/clippy_lints/src/unused_rounding.rs @@ -1,5 +1,5 @@ -use clippy_utils::diagnostics::span_lint_and_sugg; -use rustc_ast::ast::{Expr, ExprKind, LitFloatType, LitKind}; +use clippy_utils::{diagnostics::span_lint_and_sugg, source::snippet}; +use rustc_ast::ast::{Expr, ExprKind, LitKind}; use rustc_errors::Applicability; use rustc_lint::{EarlyContext, EarlyLintPass}; use rustc_session::{declare_lint_pass, declare_tool_lint}; @@ -29,19 +29,15 @@ declare_clippy_lint! { } declare_lint_pass!(UnusedRounding => [UNUSED_ROUNDING]); -fn is_useless_rounding(expr: &Expr) -> Option<(&str, String)> { +fn is_useless_rounding<'a>(cx: &EarlyContext<'a>, expr: &'a Expr) -> Option<(&'a str, String)> { if let ExprKind::MethodCall(name_ident, receiver, _, _) = &expr.kind && let method_name = name_ident.ident.name.as_str() && (method_name == "ceil" || method_name == "round" || method_name == "floor") && let ExprKind::Lit(spanned) = &receiver.kind - && let LitKind::Float(symbol, ty) = spanned.kind { + && let LitKind::Float(symbol, _) = spanned.kind { let f = symbol.as_str().parse::().unwrap(); - let f_str = spanned.token_lit.symbol.to_string() + if let LitFloatType::Suffixed(ty) = ty { - ty.name_str() - } else { - "" - }; if f.fract() == 0.0 { + let f_str = snippet(cx, receiver.span, "..").to_string(); Some((method_name, f_str)) } else { None @@ -53,7 +49,7 @@ fn is_useless_rounding(expr: &Expr) -> Option<(&str, String)> { impl EarlyLintPass for UnusedRounding { fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) { - if let Some((method_name, float)) = is_useless_rounding(expr) { + if let Some((method_name, float)) = is_useless_rounding(cx, expr) { span_lint_and_sugg( cx, UNUSED_ROUNDING,