]> git.lizzy.rs Git - rust.git/blobdiff - clippy_lints/src/replace_consts.rs
Auto merge of #4809 - iankronquist:patch-1, r=flip1995
[rust.git] / clippy_lints / src / replace_consts.rs
index c15eca15c1bb317f321dc3337c1d72f40c0b841e..66e1df72b25f41f4004af30428d5c06e54bace15 100644 (file)
@@ -1,14 +1,14 @@
 use crate::utils::{match_def_path, span_lint_and_sugg};
 use if_chain::if_chain;
-use rustc::hir;
-use rustc::hir::def::{DefKind, Res};
-use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
-use rustc::{declare_lint_pass, declare_tool_lint};
 use rustc_errors::Applicability;
+use rustc_hir::def::{DefKind, Res};
+use rustc_hir::*;
+use rustc_lint::{LateContext, LateLintPass};
+use rustc_session::{declare_lint_pass, declare_tool_lint};
 
 declare_clippy_lint! {
-    /// **What it does:** Checks for usage of `ATOMIC_X_INIT`, `ONCE_INIT`, and
-    /// `uX/iX::MIN/MAX`.
+    /// **What it does:** Checks for usage of standard library
+    /// `const`s that could be replaced by `const fn`s.
     ///
     /// **Why is this bad?** `const fn`s exist
     ///
     ///
     /// **Example:**
     /// ```rust
-    /// static FOO: AtomicIsize = ATOMIC_ISIZE_INIT;
+    /// let x = std::u32::MIN;
+    /// let y = std::u32::MAX;
     /// ```
     ///
     /// Could be written:
     ///
     /// ```rust
-    /// static FOO: AtomicIsize = AtomicIsize::new(0);
+    /// let x = u32::min_value();
+    /// let y = u32::max_value();
     /// ```
     pub REPLACE_CONSTS,
     pedantic,
 
 declare_lint_pass!(ReplaceConsts => [REPLACE_CONSTS]);
 
+fn in_pattern(cx: &LateContext<'_, '_>, expr: &Expr<'_>) -> bool {
+    let map = &cx.tcx.hir();
+    let parent_id = map.get_parent_node(expr.hir_id);
+
+    if let Some(node) = map.find(parent_id) {
+        if let Node::Pat(_) = node {
+            return true;
+        }
+    }
+
+    false
+}
+
 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ReplaceConsts {
-    fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr) {
+    fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr<'_>) {
         if_chain! {
-            if let hir::ExprKind::Path(ref qp) = expr.node;
+            if let ExprKind::Path(ref qp) = expr.kind;
             if let Res::Def(DefKind::Const, def_id) = cx.tables.qpath_res(qp, expr.hir_id);
+            // Do not lint within patterns as function calls are disallowed in them
+            if !in_pattern(cx, expr);
             then {
-                for (const_path, repl_snip) in &REPLACEMENTS {
+                for &(ref const_path, repl_snip) in &REPLACEMENTS {
                     if match_def_path(cx, def_id, const_path) {
                         span_lint_and_sugg(
                             cx,
@@ -56,9 +73,7 @@ fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr) {
     }
 }
 
-const REPLACEMENTS: [([&str; 3], &str); 25] = [
-    // Once
-    (["core", "sync", "ONCE_INIT"], "Once::new()"),
+const REPLACEMENTS: [([&str; 3], &str); 24] = [
     // Min
     (["core", "isize", "MIN"], "isize::min_value()"),
     (["core", "i8", "MIN"], "i8::min_value()"),