about summary refs log tree commit diff
diff options
context:
space:
mode:
authorPhilipp Hansch <dev@phansch.net>2019-01-14 16:44:10 +0100
committerPhilipp Hansch <dev@phansch.net>2019-01-29 08:19:05 +0100
commitf9d65b6356abfc0503046232776cf6fdc43fb578 (patch)
tree3a30b10f9ad4cf5826d64a686598c7767a8dc97e
parent68cc4df551ea289e542910226cd1429ff2310dfa (diff)
downloadrust-f9d65b6356abfc0503046232776cf6fdc43fb578.tar.gz
rust-f9d65b6356abfc0503046232776cf6fdc43fb578.zip
Reorganize conditionals: Run faster checks first
-rw-r--r--clippy_lints/src/missing_const_for_fn.rs40
-rw-r--r--tests/ui/missing_const_for_fn/cant_be_const.rs7
-rw-r--r--tests/ui/missing_const_for_fn/could_be_const.stderr11
3 files changed, 39 insertions, 19 deletions
diff --git a/clippy_lints/src/missing_const_for_fn.rs b/clippy_lints/src/missing_const_for_fn.rs
index caa516acd24..3e5c81484d5 100644
--- a/clippy_lints/src/missing_const_for_fn.rs
+++ b/clippy_lints/src/missing_const_for_fn.rs
@@ -82,26 +82,32 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingConstForFn {
         span: Span,
         node_id: NodeId
     ) {
+        // Perform some preliminary checks that rule out constness on the Clippy side. This way we
+        // can skip the actual const check and return early.
+        match kind {
+            FnKind::ItemFn(name, _generics, header, _vis, attrs) => {
+                if !can_be_const_fn(&name.as_str(), header, attrs) {
+                    return;
+                }
+            },
+            FnKind::Method(ident, sig, _vis, attrs) => {
+                let header = sig.header;
+                let name = ident.name.as_str();
+                if !can_be_const_fn(&name, header, attrs) {
+                    return;
+                }
+            },
+            _ => return
+        }
+
         let def_id = cx.tcx.hir().local_def_id(node_id);
         let mir = cx.tcx.optimized_mir(def_id);
-        if let Err((span, err) = is_min_const_fn(cx.tcx, def_id, &mir) {
-            cx.tcx.sess.span_err(span, &err);
-        } else {
-            match kind {
-                FnKind::ItemFn(name, _generics, header, _vis, attrs) => {
-                    if !can_be_const_fn(&name.as_str(), header, attrs) {
-                        return;
-                    }
-                },
-                FnKind::Method(ident, sig, _vis, attrs) => {
-                    let header = sig.header;
-                    let name = ident.name.as_str();
-                    if !can_be_const_fn(&name, header, attrs) {
-                        return;
-                    }
-                },
-                _ => return
+
+        if let Err((span, err)) = is_min_const_fn(cx.tcx, def_id, &mir) {
+            if cx.tcx.is_min_const_fn(def_id) {
+                cx.tcx.sess.span_err(span, &err);
             }
+        } else {
             span_lint(cx, MISSING_CONST_FOR_FN, span, "this could be a const_fn");
         }
     }
diff --git a/tests/ui/missing_const_for_fn/cant_be_const.rs b/tests/ui/missing_const_for_fn/cant_be_const.rs
index 5f00035b3ad..cfaf01cf3c1 100644
--- a/tests/ui/missing_const_for_fn/cant_be_const.rs
+++ b/tests/ui/missing_const_for_fn/cant_be_const.rs
@@ -41,8 +41,13 @@ fn main() {
 
 trait Foo {
     // This should not be suggested to be made const
-    // (rustc restriction)
+    // (rustc doesn't allow const trait methods)
     fn f() -> u32;
+
+    // This should not be suggested to be made const either
+    fn g() -> u32 {
+        33
+    }
 }
 
 // Don't lint custom entrypoints either
diff --git a/tests/ui/missing_const_for_fn/could_be_const.stderr b/tests/ui/missing_const_for_fn/could_be_const.stderr
index 09350572e99..593f9cf810a 100644
--- a/tests/ui/missing_const_for_fn/could_be_const.stderr
+++ b/tests/ui/missing_const_for_fn/could_be_const.stderr
@@ -17,6 +17,15 @@ LL | fn one() -> i32 { 1 }
    | ^^^^^^^^^^^^^^^^^^^^^
 
 error: this could be a const_fn
+  --> $DIR/could_be_const.rs:23:1
+   |
+LL | / fn two() -> i32 {
+LL | |     let abc = 2;
+LL | |     abc
+LL | | }
+   | |_^
+
+error: this could be a const_fn
   --> $DIR/could_be_const.rs:30:1
    |
 LL | / fn string() -> String {
@@ -38,5 +47,5 @@ LL | |     t
 LL | | }
    | |_^
 
-error: aborting due to 5 previous errors
+error: aborting due to 6 previous errors