From 1baa6cd591982471475895fc3256d5162a651628 Mon Sep 17 00:00:00 2001 From: koka <koka.code@gmail.com> Date: Sat, 19 Nov 2022 21:21:47 +0900 Subject: [PATCH] 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 0f6f36da820..cf34a57f91d 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::<f64>().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,