]> git.lizzy.rs Git - rust.git/blobdiff - clippy_lints/src/default_numeric_fallback.rs
Add instructions to run from source
[rust.git] / clippy_lints / src / default_numeric_fallback.rs
index b6730afa4af7de788067d51971c7129aea9f92a7..a125376bffa9fa405089ba1cc483e3543496aee9 100644 (file)
@@ -1,18 +1,20 @@
+use clippy_utils::diagnostics::span_lint_and_sugg;
+use clippy_utils::source::snippet;
+use if_chain::if_chain;
 use rustc_ast::ast::{LitFloatType, LitIntType, LitKind};
+use rustc_errors::Applicability;
 use rustc_hir::{
     intravisit::{walk_expr, walk_stmt, NestedVisitorMap, Visitor},
     Body, Expr, ExprKind, HirId, Lit, Stmt, StmtKind,
 };
-use rustc_lint::{LateContext, LateLintPass};
+use rustc_lint::{LateContext, LateLintPass, LintContext};
 use rustc_middle::{
     hir::map::Map,
+    lint::in_external_macro,
     ty::{self, FloatTy, IntTy, PolyFnSig, Ty},
 };
 use rustc_session::{declare_lint_pass, declare_tool_lint};
-
-use if_chain::if_chain;
-
-use crate::utils::span_lint_and_help;
+use std::iter;
 
 declare_clippy_lint! {
     /// **What it does:** Checks for usage of unconstrained numeric literals which may cause default numeric fallback in type
@@ -27,7 +29,7 @@
     /// **Why is this bad?** For those who are very careful about types, default numeric fallback
     /// can be a pitfall that cause unexpected runtime behavior.
     ///
-    /// **Known problems:** None.
+    /// **Known problems:** This lint can only be allowed at the function level or above.
     ///
     /// **Example:**
     /// ```rust
@@ -64,7 +66,7 @@ struct NumericFallbackVisitor<'a, 'tcx> {
 impl<'a, 'tcx> NumericFallbackVisitor<'a, 'tcx> {
     fn new(cx: &'a LateContext<'tcx>) -> Self {
         Self {
-            ty_bounds: Vec::new(),
+            ty_bounds: vec![TyBound::Nothing],
             cx,
         }
     }
@@ -72,19 +74,28 @@ fn new(cx: &'a LateContext<'tcx>) -> Self {
     /// Check whether a passed literal has potential to cause fallback or not.
     fn check_lit(&self, lit: &Lit, lit_ty: Ty<'tcx>) {
         if_chain! {
+                if !in_external_macro(self.cx.sess(), lit.span);
                 if let Some(ty_bound) = self.ty_bounds.last();
                 if matches!(lit.node,
                             LitKind::Int(_, LitIntType::Unsuffixed) | LitKind::Float(_, LitFloatType::Unsuffixed));
-                if matches!(lit_ty.kind(), ty::Int(IntTy::I32) | ty::Float(FloatTy::F64));
                 if !ty_bound.is_integral();
                 then {
-                    span_lint_and_help(
+                    let suffix = match lit_ty.kind() {
+                        ty::Int(IntTy::I32) => "i32",
+                        ty::Float(FloatTy::F64) => "f64",
+                        // Default numeric fallback never results in other types.
+                        _ => return,
+                    };
+
+                    let sugg = format!("{}_{}", snippet(self.cx, lit.span, ""), suffix);
+                    span_lint_and_sugg(
                         self.cx,
                         DEFAULT_NUMERIC_FALLBACK,
                         lit.span,
                         "default numeric fallback might occur",
-                        None,
-                        "consider adding suffix to avoid default numeric fallback",
+                        "consider adding suffix",
+                        sugg,
+                        Applicability::MaybeIncorrect,
                     );
                 }
         }
@@ -99,7 +110,7 @@ fn visit_expr(&mut self, expr: &'tcx Expr<'_>) {
         match &expr.kind {
             ExprKind::Call(func, args) => {
                 if let Some(fn_sig) = fn_sig_opt(self.cx, func.hir_id) {
-                    for (expr, bound) in args.iter().zip(fn_sig.skip_binder().inputs().iter()) {
+                    for (expr, bound) in iter::zip(*args, fn_sig.skip_binder().inputs()) {
                         // Push found arg type, then visit arg.
                         self.ty_bounds.push(TyBound::Ty(bound));
                         self.visit_expr(expr);
@@ -112,7 +123,7 @@ fn visit_expr(&mut self, expr: &'tcx Expr<'_>) {
             ExprKind::MethodCall(_, _, args, _) => {
                 if let Some(def_id) = self.cx.typeck_results().type_dependent_def_id(expr.hir_id) {
                     let fn_sig = self.cx.tcx.fn_sig(def_id).skip_binder();
-                    for (expr, bound) in args.iter().zip(fn_sig.inputs().iter()) {
+                    for (expr, bound) in iter::zip(*args, fn_sig.inputs()) {
                         self.ty_bounds.push(TyBound::Ty(bound));
                         self.visit_expr(expr);
                         self.ty_bounds.pop();
@@ -121,6 +132,41 @@ fn visit_expr(&mut self, expr: &'tcx Expr<'_>) {
                 }
             },
 
+            ExprKind::Struct(_, fields, base) => {
+                let ty = self.cx.typeck_results().expr_ty(expr);
+                if_chain! {
+                    if let Some(adt_def) = ty.ty_adt_def();
+                    if adt_def.is_struct();
+                    if let Some(variant) = adt_def.variants.iter().next();
+                    then {
+                        let fields_def = &variant.fields;
+
+                        // Push field type then visit each field expr.
+                        for field in fields.iter() {
+                            let bound =
+                                fields_def
+                                    .iter()
+                                    .find_map(|f_def| {
+                                        if f_def.ident == field.ident
+                                            { Some(self.cx.tcx.type_of(f_def.did)) }
+                                        else { None }
+                                    });
+                            self.ty_bounds.push(bound.into());
+                            self.visit_expr(field.expr);
+                            self.ty_bounds.pop();
+                        }
+
+                        // Visit base with no bound.
+                        if let Some(base) = base {
+                            self.ty_bounds.push(TyBound::Nothing);
+                            self.visit_expr(base);
+                            self.ty_bounds.pop();
+                        }
+                        return;
+                    }
+                }
+            },
+
             ExprKind::Lit(lit) => {
                 let ty = self.cx.typeck_results().expr_ty(expr);
                 self.check_lit(lit, ty);
@@ -137,9 +183,9 @@ fn visit_stmt(&mut self, stmt: &'tcx Stmt<'_>) {
         match stmt.kind {
             StmtKind::Local(local) => {
                 if local.ty.is_some() {
-                    self.ty_bounds.push(TyBound::Any)
+                    self.ty_bounds.push(TyBound::Any);
                 } else {
-                    self.ty_bounds.push(TyBound::Nothing)
+                    self.ty_bounds.push(TyBound::Nothing);
                 }
             },
 
@@ -166,13 +212,13 @@ fn fn_sig_opt<'tcx>(cx: &LateContext<'tcx>, hir_id: HirId) -> Option<PolyFnSig<'
 }
 
 #[derive(Debug, Clone, Copy)]
-enum TyBound<'ctx> {
+enum TyBound<'tcx> {
     Any,
-    Ty(Ty<'ctx>),
+    Ty(Ty<'tcx>),
     Nothing,
 }
 
-impl<'ctx> TyBound<'ctx> {
+impl<'tcx> TyBound<'tcx> {
     fn is_integral(self) -> bool {
         match self {
             TyBound::Any => true,
@@ -181,3 +227,12 @@ fn is_integral(self) -> bool {
         }
     }
 }
+
+impl<'tcx> From<Option<Ty<'tcx>>> for TyBound<'tcx> {
+    fn from(v: Option<Ty<'tcx>>) -> Self {
+        match v {
+            Some(t) => TyBound::Ty(t),
+            None => TyBound::Nothing,
+        }
+    }
+}