]> git.lizzy.rs Git - rust.git/blobdiff - tests/ui/missing_const_for_fn/cant_be_const.rs
Fix missing_const_for_fn false positive
[rust.git] / tests / ui / missing_const_for_fn / cant_be_const.rs
index 5f00035b3ad361ecf22e1dac1f96c9cf93bef40c..f367279906fda13f992c8bd3bb715f556da6e7d6 100644 (file)
@@ -8,16 +8,22 @@
 struct Game;
 
 // This should not be linted because it's already const
-const fn already_const() -> i32 { 32 }
+const fn already_const() -> i32 {
+    32
+}
 
 impl Game {
     // This should not be linted because it's already const
-    pub const fn already_const() -> i32 { 32 }
+    pub const fn already_const() -> i32 {
+        32
+    }
 }
 
 // Allowing on this function, because it would lint, which we don't want in this case.
 #[allow(clippy::missing_const_for_fn)]
-fn random() -> u32 { 42 }
+fn random() -> u32 {
+    42
+}
 
 // We should not suggest to make this function `const` because `random()` is non-const
 fn random_caller() -> u32 {
@@ -30,21 +36,57 @@ fn random_caller() -> u32 {
 // refer to a static variable
 fn get_y() -> u32 {
     Y
-        //~^ ERROR E0013
+    //~^ ERROR E0013
 }
 
-// Also main should not be suggested to be made const
-fn main() {
-    // We should also be sure to not lint on closures
-    let add_one_v2 = |x: u32| -> u32 { x + 1 };
+// Don't lint entrypoint functions
+#[start]
+fn init(num: isize, something: *const *const u8) -> isize {
+    1
 }
 
 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
-#[start]
-fn init(num: isize, something: *const *const u8) -> isize { 1 }
+// Don't lint in external macros (derive)
+#[derive(PartialEq, Eq)]
+struct Point(isize, isize);
+
+impl std::ops::Add for Point {
+    type Output = Self;
+
+    // Don't lint in trait impls of derived methods
+    fn add(self, other: Self) -> Self {
+        Point(self.0 + other.0, self.1 + other.1)
+    }
+}
+
+mod with_drop {
+    pub struct A;
+    pub struct B;
+    impl Drop for A {
+        fn drop(&mut self) {}
+    }
+
+    impl A {
+        // This can not be const because the type implements `Drop`.
+        pub fn a(self) -> B {
+            B
+        }
+    }
+
+    impl B {
+        // This can not be const because `a` implements `Drop`.
+        pub fn a(self, a: A) -> B {
+            B
+        }
+    }
+}