]> git.lizzy.rs Git - rust.git/blob - tests/ui/missing_const_for_fn/cant_be_const.rs
Auto merge of #3844 - phansch:const_fn_external_macro, r=oli-obk
[rust.git] / tests / ui / missing_const_for_fn / cant_be_const.rs
1 //! False-positive tests to ensure we don't suggest `const` for things where it would cause a
2 //! compilation error.
3 //! The .stderr output of this test should be empty. Otherwise it's a bug somewhere.
4
5 #![warn(clippy::missing_const_for_fn)]
6 #![feature(start)]
7
8 struct Game;
9
10 // This should not be linted because it's already const
11 const fn already_const() -> i32 {
12     32
13 }
14
15 impl Game {
16     // This should not be linted because it's already const
17     pub const fn already_const() -> i32 {
18         32
19     }
20 }
21
22 // Allowing on this function, because it would lint, which we don't want in this case.
23 #[allow(clippy::missing_const_for_fn)]
24 fn random() -> u32 {
25     42
26 }
27
28 // We should not suggest to make this function `const` because `random()` is non-const
29 fn random_caller() -> u32 {
30     random()
31 }
32
33 static Y: u32 = 0;
34
35 // We should not suggest to make this function `const` because const functions are not allowed to
36 // refer to a static variable
37 fn get_y() -> u32 {
38     Y
39     //~^ ERROR E0013
40 }
41
42 // Don't lint entrypoint functions
43 #[start]
44 fn init(num: isize, something: *const *const u8) -> isize {
45     1
46 }
47
48 trait Foo {
49     // This should not be suggested to be made const
50     // (rustc doesn't allow const trait methods)
51     fn f() -> u32;
52
53     // This should not be suggested to be made const either
54     fn g() -> u32 {
55         33
56     }
57 }
58
59 // Don't lint in external macros (derive)
60 #[derive(PartialEq, Eq)]
61 struct Point(isize, isize);
62
63 impl std::ops::Add for Point {
64     type Output = Self;
65
66     // Don't lint in trait impls of derived methods
67     fn add(self, other: Self) -> Self {
68         Point(self.0 + other.0, self.1 + other.1)
69     }
70 }