|
1 | 1 | use clippy_utils::diagnostics::span_lint_and_sugg; |
| 2 | +use clippy_utils::source::snippet; |
2 | 3 | use rustc_ast::ast::{Expr, ExprKind, MethodCall}; |
3 | 4 | use rustc_errors::Applicability; |
4 | 5 | use rustc_lint::{EarlyContext, EarlyLintPass}; |
@@ -29,30 +30,24 @@ declare_clippy_lint! { |
29 | 30 | } |
30 | 31 | declare_lint_pass!(UnusedRounding => [UNUSED_ROUNDING]); |
31 | 32 |
|
32 | | -fn is_useless_rounding(expr: &Expr) -> Option<(&str, String)> { |
| 33 | +fn is_useless_rounding<'a>(cx: &EarlyContext<'_>, expr: &'a Expr) -> Option<(&'a str, String)> { |
33 | 34 | if let ExprKind::MethodCall(box MethodCall { seg:name_ident, receiver, .. }) = &expr.kind |
34 | 35 | && let method_name = name_ident.ident.name.as_str() |
35 | 36 | && (method_name == "ceil" || method_name == "round" || method_name == "floor") |
36 | 37 | && let ExprKind::Lit(token_lit) = &receiver.kind |
37 | | - && token_lit.is_semantic_float() { |
38 | | - let mut f_str = token_lit.symbol.to_string(); |
39 | | - let f = f_str.trim_end_matches('_').parse::<f64>().unwrap(); |
40 | | - if let Some(suffix) = token_lit.suffix { |
41 | | - f_str.push_str(suffix.as_str()); |
42 | | - } |
43 | | - if f.fract() == 0.0 { |
44 | | - Some((method_name, f_str)) |
45 | | - } else { |
46 | | - None |
47 | | - } |
| 38 | + && token_lit.is_semantic_float() |
| 39 | + && let Ok(f) = token_lit.symbol.as_str().replace('_', "").parse::<f64>() { |
| 40 | + (f.fract() == 0.0).then(|| |
| 41 | + (method_name, snippet(cx, receiver.span, "..").to_string()) |
| 42 | + ) |
48 | 43 | } else { |
49 | 44 | None |
50 | 45 | } |
51 | 46 | } |
52 | 47 |
|
53 | 48 | impl EarlyLintPass for UnusedRounding { |
54 | 49 | fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) { |
55 | | - if let Some((method_name, float)) = is_useless_rounding(expr) { |
| 50 | + if let Some((method_name, float)) = is_useless_rounding(cx, expr) { |
56 | 51 | span_lint_and_sugg( |
57 | 52 | cx, |
58 | 53 | UNUSED_ROUNDING, |
|
0 commit comments