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