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