]> git.lizzy.rs Git - rust.git/blob - tests/ui/missing_const_for_fn/cant_be_const.rs
5f00035b3ad361ecf22e1dac1f96c9cf93bef40c
[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 { 32 }
12
13 impl Game {
14     // This should not be linted because it's already const
15     pub const fn already_const() -> i32 { 32 }
16 }
17
18 // Allowing on this function, because it would lint, which we don't want in this case.
19 #[allow(clippy::missing_const_for_fn)]
20 fn random() -> u32 { 42 }
21
22 // We should not suggest to make this function `const` because `random()` is non-const
23 fn random_caller() -> u32 {
24     random()
25 }
26
27 static Y: u32 = 0;
28
29 // We should not suggest to make this function `const` because const functions are not allowed to
30 // refer to a static variable
31 fn get_y() -> u32 {
32     Y
33         //~^ ERROR E0013
34 }
35
36 // Also main should not be suggested to be made const
37 fn main() {
38     // We should also be sure to not lint on closures
39     let add_one_v2 = |x: u32| -> u32 { x + 1 };
40 }
41
42 trait Foo {
43     // This should not be suggested to be made const
44     // (rustc restriction)
45     fn f() -> u32;
46 }
47
48 // Don't lint custom entrypoints either
49 #[start]
50 fn init(num: isize, something: *const *const u8) -> isize { 1 }