about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMichael Wright <mikerite@lavabit.com>2019-02-01 06:32:16 +0200
committerMichael Wright <mikerite@lavabit.com>2019-02-01 06:32:16 +0200
commitb6c3a6a09f339079aa1213226aa43b65a3fe0fe9 (patch)
tree9b1feeead33668f31368eb1ff111a7c6390bd3a7
parent488cdebd2619dd031256813b4069b46377c016e8 (diff)
downloadrust-b6c3a6a09f339079aa1213226aa43b65a3fe0fe9.tar.gz
rust-b6c3a6a09f339079aa1213226aa43b65a3fe0fe9.zip
Move `max_value` handling to consts module
-rw-r--r--clippy_lints/src/consts.rs28
-rw-r--r--clippy_lints/src/types.rs21
2 files changed, 29 insertions, 20 deletions
diff --git a/clippy_lints/src/consts.rs b/clippy_lints/src/consts.rs
index 832fb875286..f56dc3aed69 100644
--- a/clippy_lints/src/consts.rs
+++ b/clippy_lints/src/consts.rs
@@ -1,6 +1,7 @@
 #![allow(clippy::float_cmp)]
 
-use crate::utils::{clip, sext, unsext};
+use crate::utils::{clip, get_def_path, sext, unsext};
+use if_chain::if_chain;
 use rustc::hir::def::Def;
 use rustc::hir::*;
 use rustc::lint::LateContext;
@@ -234,6 +235,31 @@ impl<'c, 'cc> ConstEvalLateContext<'c, 'cc> {
                 UnDeref => Some(o),
             }),
             ExprKind::Binary(op, ref left, ref right) => self.binop(op, left, right),
+            ExprKind::Call(ref callee, ref args) => {
+                // We only handle a few const functions for now
+                if_chain! {
+                    if args.is_empty();
+                    if let ExprKind::Path(qpath) = &callee.node;
+                    let def = self.tables.qpath_def(qpath, callee.hir_id);
+                    if let Some(def_id) = def.opt_def_id();
+                    let def_path = get_def_path(self.tcx, def_id);
+                    if let &["core", "num", impl_ty, "max_value"] = &def_path[..];
+                    then {
+                       let value = match impl_ty {
+                           "<impl i8>" => i8::max_value() as u128,
+                           "<impl i16>" => i16::max_value() as u128,
+                           "<impl i32>" => i32::max_value() as u128,
+                           "<impl i64>" => i64::max_value() as u128,
+                           "<impl i128>" => i128::max_value() as u128,
+                           _ => return None,
+                       };
+                       Some(Constant::Int(value))
+                    }
+                    else {
+                        None
+                    }
+                }
+            },
             // TODO: add other expressions
             _ => None,
         }
diff --git a/clippy_lints/src/types.rs b/clippy_lints/src/types.rs
index 3c0171ce8c6..2c48e06e371 100644
--- a/clippy_lints/src/types.rs
+++ b/clippy_lints/src/types.rs
@@ -4,8 +4,8 @@ use crate::consts::{constant, Constant};
 use crate::reexport::*;
 use crate::utils::paths;
 use crate::utils::{
-    clip, comparisons, differing_macro_contexts, get_def_path, higher, in_constant, in_macro, int_bits,
-    last_path_segment, match_def_path, match_path, multispan_sugg, opt_def_id, same_tys, sext, snippet, snippet_opt,
+    clip, comparisons, differing_macro_contexts, higher, in_constant, in_macro, int_bits, last_path_segment,
+    match_def_path, match_path, multispan_sugg, opt_def_id, same_tys, sext, snippet, snippet_opt,
     snippet_with_applicability, span_help_and_lint, span_lint, span_lint_and_sugg, span_lint_and_then, unsext,
     AbsolutePathBuffer,
 };
@@ -1018,23 +1018,6 @@ fn check_loss_of_sign(cx: &LateContext<'_, '_>, expr: &Expr, op: &Expr, cast_fro
         }
     }
 
-    // don't lint for max_value const fns
-    if_chain! {
-        if let ExprKind::Call(callee, args) = &op.node;
-        if args.is_empty();
-        if let ExprKind::Path(qpath) = &callee.node;
-        let def = cx.tables.qpath_def(qpath, callee.hir_id);
-        if let Some(def_id) = def.opt_def_id();
-        let def_path = get_def_path(cx.tcx, def_id);
-        if let &["core", "num", impl_ty, "max_value"] = &def_path[..];
-        then {
-           if let "<impl i8>" | "<impl i16>" | "<impl i32>" |
-                  "<impl i64>" | "<impl i128>" = impl_ty {
-               return;
-           }
-        }
-    }
-
     span_lint(
         cx,
         CAST_SIGN_LOSS,