]> git.lizzy.rs Git - rust.git/commitdiff
Reorganize conditionals: Run faster checks first
authorPhilipp Hansch <dev@phansch.net>
Mon, 14 Jan 2019 15:44:10 +0000 (16:44 +0100)
committerPhilipp Hansch <dev@phansch.net>
Tue, 29 Jan 2019 07:19:05 +0000 (08:19 +0100)
clippy_lints/src/missing_const_for_fn.rs
tests/ui/missing_const_for_fn/cant_be_const.rs
tests/ui/missing_const_for_fn/could_be_const.stderr

index caa516acd24d13192250f68acec3c8770187bd45..3e5c81484d542eb07b37a821dfb085c9240e3ec0 100644 (file)
@@ -82,26 +82,32 @@ fn check_fn(
         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");
         }
     }
index 5f00035b3ad361ecf22e1dac1f96c9cf93bef40c..cfaf01cf3c11950e90473c1a4027544a4d04edd8 100644 (file)
@@ -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
index 09350572e9934b2bbda39bb7242d5b5f058cbf1e..593f9cf810ac665b1d9e5284d0b59174d8cc5091 100644 (file)
@@ -16,6 +16,15 @@ error: this could be a const_fn
 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
    |
@@ -38,5 +47,5 @@ LL | |     t
 LL | | }
    | |_^
 
-error: aborting due to 5 previous errors
+error: aborting due to 6 previous errors