]>
Commit | Line | Data |
---|---|---|
f20569fa | 1 | #![warn(clippy::missing_const_for_fn)] |
e8be2606 | 2 | #![allow(incomplete_features, clippy::let_and_return, clippy::missing_transmute_annotations)] |
fe692bf9 FG |
3 | #![feature(const_mut_refs)] |
4 | #![feature(const_trait_impl)] | |
f20569fa XL |
5 | |
6 | use std::mem::transmute; | |
7 | ||
8 | struct Game { | |
9 | guess: i32, | |
10 | } | |
11 | ||
12 | impl Game { | |
13 | // Could be const | |
14 | pub fn new() -> Self { | |
781aab86 FG |
15 | //~^ ERROR: this could be a `const fn` |
16 | //~| NOTE: `-D clippy::missing-const-for-fn` implied by `-D warnings` | |
f20569fa XL |
17 | Self { guess: 42 } |
18 | } | |
19 | ||
20 | fn const_generic_params<'a, T, const N: usize>(&self, b: &'a [T; N]) -> &'a [T; N] { | |
781aab86 | 21 | //~^ ERROR: this could be a `const fn` |
f20569fa XL |
22 | b |
23 | } | |
24 | } | |
25 | ||
26 | // Could be const | |
27 | fn one() -> i32 { | |
781aab86 | 28 | //~^ ERROR: this could be a `const fn` |
f20569fa XL |
29 | 1 |
30 | } | |
31 | ||
32 | // Could also be const | |
33 | fn two() -> i32 { | |
781aab86 | 34 | //~^ ERROR: this could be a `const fn` |
f20569fa XL |
35 | let abc = 2; |
36 | abc | |
37 | } | |
38 | ||
39 | // Could be const (since Rust 1.39) | |
40 | fn string() -> String { | |
781aab86 | 41 | //~^ ERROR: this could be a `const fn` |
f20569fa XL |
42 | String::new() |
43 | } | |
44 | ||
45 | // Could be const | |
46 | unsafe fn four() -> i32 { | |
781aab86 | 47 | //~^ ERROR: this could be a `const fn` |
f20569fa XL |
48 | 4 |
49 | } | |
50 | ||
51 | // Could also be const | |
52 | fn generic<T>(t: T) -> T { | |
781aab86 | 53 | //~^ ERROR: this could be a `const fn` |
f20569fa XL |
54 | t |
55 | } | |
56 | ||
57 | fn sub(x: u32) -> usize { | |
58 | unsafe { transmute(&x) } | |
59 | } | |
60 | ||
f20569fa | 61 | fn generic_arr<T: Copy>(t: [T; 1]) -> T { |
781aab86 | 62 | //~^ ERROR: this could be a `const fn` |
f20569fa XL |
63 | t[0] |
64 | } | |
65 | ||
66 | mod with_drop { | |
67 | pub struct A; | |
68 | pub struct B; | |
69 | impl Drop for A { | |
70 | fn drop(&mut self) {} | |
71 | } | |
72 | ||
73 | impl B { | |
74 | // This can be const, because `a` is passed by reference | |
75 | pub fn b(self, a: &A) -> B { | |
781aab86 | 76 | //~^ ERROR: this could be a `const fn` |
f20569fa XL |
77 | B |
78 | } | |
79 | } | |
80 | } | |
81 | ||
487cf647 | 82 | #[clippy::msrv = "1.47.0"] |
cdc7bbd5 | 83 | mod const_fn_stabilized_before_msrv { |
cdc7bbd5 XL |
84 | // This could be const because `u8::is_ascii_digit` is a stable const function in 1.47. |
85 | fn const_fn_stabilized_before_msrv(byte: u8) { | |
781aab86 | 86 | //~^ ERROR: this could be a `const fn` |
cdc7bbd5 XL |
87 | byte.is_ascii_digit(); |
88 | } | |
89 | } | |
90 | ||
487cf647 | 91 | #[clippy::msrv = "1.45"] |
2b03887a | 92 | fn msrv_1_45() -> i32 { |
2b03887a FG |
93 | 45 |
94 | } | |
95 | ||
487cf647 | 96 | #[clippy::msrv = "1.46"] |
2b03887a | 97 | fn msrv_1_46() -> i32 { |
781aab86 | 98 | //~^ ERROR: this could be a `const fn` |
2b03887a FG |
99 | 46 |
100 | } | |
101 | ||
f20569fa XL |
102 | // Should not be const |
103 | fn main() {} | |
fe692bf9 FG |
104 | |
105 | struct D; | |
106 | ||
107 | impl const Drop for D { | |
108 | fn drop(&mut self) { | |
109 | todo!(); | |
110 | } | |
111 | } | |
112 | ||
113 | // Lint this, since it can be dropped in const contexts | |
add651ee | 114 | // FIXME(effects) |
fe692bf9 | 115 | fn d(this: D) {} |
31ef2f64 FG |
116 | |
117 | mod msrv { | |
118 | struct Foo(*const u8, &'static u8); | |
119 | ||
120 | impl Foo { | |
121 | #[clippy::msrv = "1.58"] | |
122 | fn deref_ptr_can_be_const(self) -> usize { | |
123 | //~^ ERROR: this could be a `const fn` | |
124 | unsafe { *self.0 as usize } | |
125 | } | |
126 | ||
127 | fn deref_copied_val(self) -> usize { | |
128 | //~^ ERROR: this could be a `const fn` | |
129 | *self.1 as usize | |
130 | } | |
131 | } | |
132 | ||
133 | union Bar { | |
134 | val: u8, | |
135 | } | |
136 | ||
137 | #[clippy::msrv = "1.56"] | |
138 | fn union_access_can_be_const() { | |
139 | //~^ ERROR: this could be a `const fn` | |
140 | let bar = Bar { val: 1 }; | |
141 | let _ = unsafe { bar.val }; | |
142 | } | |
143 | } |