]> git.lizzy.rs Git - rust.git/blobdiff - clippy_lints/src/float_literal.rs
ast/hir: Rename field-related structures
[rust.git] / clippy_lints / src / float_literal.rs
index 30d4b25318f7fb1139b9b02a0e2c9d5c565c5194..8e256f346841947d6c075f110aab2345afaa6871 100644 (file)
@@ -1,13 +1,12 @@
-use crate::utils::span_lint_and_sugg;
-use crate::utils::sugg::format_numeric_literal;
+use crate::utils::{numeric_literal, span_lint_and_sugg};
 use if_chain::if_chain;
-use rustc::ty;
-use rustc_ast::ast::{FloatTy, LitFloatType, LitKind};
+use rustc_ast::ast::{self, LitFloatType, LitKind};
 use rustc_errors::Applicability;
 use rustc_hir as hir;
 use rustc_lint::{LateContext, LateLintPass};
+use rustc_middle::ty::{self, FloatTy};
 use rustc_session::{declare_lint_pass, declare_tool_lint};
-use std::{f32, f64, fmt};
+use std::fmt;
 
 declare_clippy_lint! {
     /// **What it does:** Checks for float literals with a precision greater
 
 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<'_>) {
+impl<'tcx> LateLintPass<'tcx> for FloatLiteral {
+    fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>) {
         if_chain! {
-            let ty = cx.tables.expr_ty(expr);
-            if let ty::Float(fty) = ty.kind;
+            let ty = cx.typeck_results().expr_ty(expr);
+            if let ty::Float(fty) = *ty.kind();
             if let hir::ExprKind::Lit(ref lit) = expr.kind;
             if let LitKind::Float(sym, lit_float_ty) = lit.node;
             then {
@@ -76,9 +75,9 @@ fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr<'_>)
                 let digits = count_digits(&sym_str);
                 let max = max_digits(fty);
                 let type_suffix = match lit_float_ty {
-                    LitFloatType::Suffixed(FloatTy::F32) => Some("f32"),
-                    LitFloatType::Suffixed(FloatTy::F64) => Some("f64"),
-                    _ => None
+                    LitFloatType::Suffixed(ast::FloatTy::F32) => Some("f32"),
+                    LitFloatType::Suffixed(ast::FloatTy::F64) => Some("f64"),
+                    LitFloatType::Unsuffixed => None
                 };
                 let (is_whole, mut float_str) = match fty {
                     FloatTy::F32 => {
@@ -109,7 +108,7 @@ fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr<'_>)
                             expr.span,
                             "literal cannot be represented as the underlying type without loss of precision",
                             "consider changing the type or replacing it with",
-                            format_numeric_literal(&float_str, type_suffix, true),
+                            numeric_literal::format(&float_str, type_suffix, true),
                             Applicability::MachineApplicable,
                         );
                     }
@@ -120,7 +119,7 @@ fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr<'_>)
                         expr.span,
                         "float has excessive precision",
                         "consider changing the type or truncating it to",
-                        format_numeric_literal(&float_str, type_suffix, true),
+                        numeric_literal::format(&float_str, type_suffix, true),
                         Applicability::MachineApplicable,
                     );
                 }
@@ -146,11 +145,7 @@ fn count_digits(s: &str) -> usize {
         .take_while(|c| *c != 'e' && *c != 'E')
         .fold(0, |count, c| {
             // leading zeros
-            if c == '0' && count == 0 {
-                count
-            } else {
-                count + 1
-            }
+            if c == '0' && count == 0 { count } else { count + 1 }
         })
 }