]> git.lizzy.rs Git - rust.git/blob - tests/ui/missing_const_for_fn/cant_be_const.rs
Auto merge of #9684 - kraktus:ref_option_ref, r=xFrednet
[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 // aux-build:helper.rs
6 // aux-build:../../auxiliary/proc_macro_with_span.rs
7
8 #![warn(clippy::missing_const_for_fn)]
9 #![feature(start)]
10 #![feature(custom_inner_attributes)]
11
12 extern crate helper;
13 extern crate proc_macro_with_span;
14
15 use proc_macro_with_span::with_span;
16
17 struct Game;
18
19 // This should not be linted because it's already const
20 const fn already_const() -> i32 {
21     32
22 }
23
24 impl Game {
25     // This should not be linted because it's already const
26     pub const fn already_const() -> i32 {
27         32
28     }
29 }
30
31 // Allowing on this function, because it would lint, which we don't want in this case.
32 #[allow(clippy::missing_const_for_fn)]
33 fn random() -> u32 {
34     42
35 }
36
37 // We should not suggest to make this function `const` because `random()` is non-const
38 fn random_caller() -> u32 {
39     random()
40 }
41
42 static Y: u32 = 0;
43
44 // We should not suggest to make this function `const` because const functions are not allowed to
45 // refer to a static variable
46 fn get_y() -> u32 {
47     Y
48     //~^ ERROR E0013
49 }
50
51 // Don't lint entrypoint functions
52 #[start]
53 fn init(num: isize, something: *const *const u8) -> isize {
54     1
55 }
56
57 trait Foo {
58     // This should not be suggested to be made const
59     // (rustc doesn't allow const trait methods)
60     fn f() -> u32;
61
62     // This should not be suggested to be made const either
63     fn g() -> u32 {
64         33
65     }
66 }
67
68 // Don't lint in external macros (derive)
69 #[derive(PartialEq, Eq)]
70 struct Point(isize, isize);
71
72 impl std::ops::Add for Point {
73     type Output = Self;
74
75     // Don't lint in trait impls of derived methods
76     fn add(self, other: Self) -> Self {
77         Point(self.0 + other.0, self.1 + other.1)
78     }
79 }
80
81 mod with_drop {
82     pub struct A;
83     pub struct B;
84     impl Drop for A {
85         fn drop(&mut self) {}
86     }
87
88     impl A {
89         // This can not be const because the type implements `Drop`.
90         pub fn b(self) -> B {
91             B
92         }
93     }
94
95     impl B {
96         // This can not be const because `a` implements `Drop`.
97         pub fn a(self, a: A) -> B {
98             B
99         }
100     }
101 }
102
103 fn const_generic_params<T, const N: usize>(t: &[T; N]) -> &[T; N] {
104     t
105 }
106
107 fn const_generic_return<T, const N: usize>(t: &[T]) -> &[T; N] {
108     let p = t.as_ptr() as *const [T; N];
109
110     unsafe { &*p }
111 }
112
113 // Do not lint this because it calls a function whose constness is unstable.
114 fn unstably_const_fn() {
115     helper::unstably_const_fn()
116 }
117
118 mod const_fn_stabilized_after_msrv {
119     #![clippy::msrv = "1.46.0"]
120
121     // Do not lint this because `u8::is_ascii_digit` is stabilized as a const function in 1.47.0.
122     fn const_fn_stabilized_after_msrv(byte: u8) {
123         byte.is_ascii_digit();
124     }
125 }
126
127 with_span! {
128     span
129     fn dont_check_in_proc_macro() {}
130 }