Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit b5ea5c2

Browse files
committed
stabilize const_extern_fn
1 parent 7cccef8 commit b5ea5c2

File tree

9 files changed

+111
-122
lines changed

9 files changed

+111
-122
lines changed

clippy_config/src/msrvs.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ macro_rules! msrv_aliases {
1717

1818
// names may refer to stabilized feature flags or library items
1919
msrv_aliases! {
20+
1,83,0 { CONST_EXTERN_FN }
2021
1,83,0 { CONST_FLOAT_BITS_CONV }
2122
1,81,0 { LINT_REASONS_STABILIZATION }
2223
1,80,0 { BOX_INTO_ITER}
@@ -27,7 +28,7 @@ msrv_aliases! {
2728
1,68,0 { PATH_MAIN_SEPARATOR_STR }
2829
1,65,0 { LET_ELSE, POINTER_CAST_CONSTNESS }
2930
1,63,0 { CLONE_INTO }
30-
1,62,0 { BOOL_THEN_SOME, DEFAULT_ENUM_ATTRIBUTE, CONST_EXTERN_FN }
31+
1,62,0 { BOOL_THEN_SOME, DEFAULT_ENUM_ATTRIBUTE, CONST_EXTERN_C_FN }
3132
1,59,0 { THREAD_LOCAL_CONST_INIT }
3233
1,58,0 { FORMAT_ARGS_CAPTURE, PATTERN_TRAIT_CHAR_ARRAY, CONST_RAW_PTR_DEREF }
3334
1,56,0 { CONST_FN_UNION }

clippy_lints/src/missing_const_for_fn.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,7 @@ impl<'tcx> LateLintPass<'tcx> for MissingConstForFn {
119119
.iter()
120120
.any(|param| matches!(param.kind, GenericParamKind::Const { .. }));
121121

122-
if already_const(header)
123-
|| has_const_generic_params
124-
|| !could_be_const_with_abi(cx, &self.msrv, header.abi)
122+
if already_const(header) || has_const_generic_params || !could_be_const_with_abi(&self.msrv, header.abi)
125123
{
126124
return;
127125
}
@@ -183,13 +181,13 @@ fn already_const(header: hir::FnHeader) -> bool {
183181
header.constness == Constness::Const
184182
}
185183

186-
fn could_be_const_with_abi(cx: &LateContext<'_>, msrv: &Msrv, abi: Abi) -> bool {
184+
fn could_be_const_with_abi(msrv: &Msrv, abi: Abi) -> bool {
187185
match abi {
188186
Abi::Rust => true,
189187
// `const extern "C"` was stabilized after 1.62.0
190-
Abi::C { unwind: false } => msrv.meets(msrvs::CONST_EXTERN_FN),
188+
Abi::C { unwind: false } => msrv.meets(msrvs::CONST_EXTERN_C_FN),
191189
// Rest ABIs are still unstable and need the `const_extern_fn` feature enabled.
192-
_ => cx.tcx.features().const_extern_fn,
190+
_ => msrv.meets(msrvs::CONST_EXTERN_FN),
193191
}
194192
}
195193

tests/ui/missing_const_for_fn/cant_be_const.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -186,12 +186,6 @@ mod msrv {
186186
extern "C" fn c() {}
187187
}
188188

189-
mod with_extern {
190-
extern "C-unwind" fn c_unwind() {}
191-
extern "system" fn system() {}
192-
extern "system-unwind" fn system_unwind() {}
193-
}
194-
195189
mod with_ty_alias {
196190
type Foo = impl std::fmt::Debug;
197191

tests/ui/missing_const_for_fn/could_be_const.fixed

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#![warn(clippy::missing_const_for_fn)]
22
#![allow(incomplete_features, clippy::let_and_return, clippy::missing_transmute_annotations)]
3+
#![allow(unsupported_calling_conventions)]
34
#![feature(const_mut_refs)]
45
#![feature(const_trait_impl)]
56

@@ -204,3 +205,16 @@ mod with_ty_alias {
204205
// in this test.
205206
const fn alias_ty_is_projection(bar: <() as FooTrait>::Foo) {}
206207
}
208+
209+
mod extern_fn {
210+
const extern "C-unwind" fn c_unwind() {}
211+
//~^ ERROR: this could be a `const fn`
212+
const extern "system" fn system() {}
213+
//~^ ERROR: this could be a `const fn`
214+
const extern "system-unwind" fn system_unwind() {}
215+
//~^ ERROR: this could be a `const fn`
216+
pub const extern "stdcall" fn std_call() {}
217+
//~^ ERROR: this could be a `const fn`
218+
pub const extern "stdcall-unwind" fn std_call_unwind() {}
219+
//~^ ERROR: this could be a `const fn`
220+
}

tests/ui/missing_const_for_fn/could_be_const.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#![warn(clippy::missing_const_for_fn)]
22
#![allow(incomplete_features, clippy::let_and_return, clippy::missing_transmute_annotations)]
3+
#![allow(unsupported_calling_conventions)]
34
#![feature(const_mut_refs)]
45
#![feature(const_trait_impl)]
56

@@ -204,3 +205,16 @@ mod with_ty_alias {
204205
// in this test.
205206
fn alias_ty_is_projection(bar: <() as FooTrait>::Foo) {}
206207
}
208+
209+
mod extern_fn {
210+
extern "C-unwind" fn c_unwind() {}
211+
//~^ ERROR: this could be a `const fn`
212+
extern "system" fn system() {}
213+
//~^ ERROR: this could be a `const fn`
214+
extern "system-unwind" fn system_unwind() {}
215+
//~^ ERROR: this could be a `const fn`
216+
pub extern "stdcall" fn std_call() {}
217+
//~^ ERROR: this could be a `const fn`
218+
pub extern "stdcall-unwind" fn std_call_unwind() {}
219+
//~^ ERROR: this could be a `const fn`
220+
}

tests/ui/missing_const_for_fn/could_be_const.stderr

Lines changed: 77 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: this could be a `const fn`
2-
--> tests/ui/missing_const_for_fn/could_be_const.rs:14:5
2+
--> tests/ui/missing_const_for_fn/could_be_const.rs:15:5
33
|
44
LL | / pub fn new() -> Self {
55
LL | |
@@ -16,7 +16,7 @@ LL | pub const fn new() -> Self {
1616
| +++++
1717

1818
error: this could be a `const fn`
19-
--> tests/ui/missing_const_for_fn/could_be_const.rs:20:5
19+
--> tests/ui/missing_const_for_fn/could_be_const.rs:21:5
2020
|
2121
LL | / fn const_generic_params<'a, T, const N: usize>(&self, b: &'a [T; N]) -> &'a [T; N] {
2222
LL | |
@@ -30,7 +30,7 @@ LL | const fn const_generic_params<'a, T, const N: usize>(&self, b: &'a [T;
3030
| +++++
3131

3232
error: this could be a `const fn`
33-
--> tests/ui/missing_const_for_fn/could_be_const.rs:27:1
33+
--> tests/ui/missing_const_for_fn/could_be_const.rs:28:1
3434
|
3535
LL | / fn one() -> i32 {
3636
LL | |
@@ -44,7 +44,7 @@ LL | const fn one() -> i32 {
4444
| +++++
4545

4646
error: this could be a `const fn`
47-
--> tests/ui/missing_const_for_fn/could_be_const.rs:33:1
47+
--> tests/ui/missing_const_for_fn/could_be_const.rs:34:1
4848
|
4949
LL | / fn two() -> i32 {
5050
LL | |
@@ -59,7 +59,7 @@ LL | const fn two() -> i32 {
5959
| +++++
6060

6161
error: this could be a `const fn`
62-
--> tests/ui/missing_const_for_fn/could_be_const.rs:40:1
62+
--> tests/ui/missing_const_for_fn/could_be_const.rs:41:1
6363
|
6464
LL | / fn string() -> String {
6565
LL | |
@@ -73,7 +73,7 @@ LL | const fn string() -> String {
7373
| +++++
7474

7575
error: this could be a `const fn`
76-
--> tests/ui/missing_const_for_fn/could_be_const.rs:46:1
76+
--> tests/ui/missing_const_for_fn/could_be_const.rs:47:1
7777
|
7878
LL | / unsafe fn four() -> i32 {
7979
LL | |
@@ -87,7 +87,7 @@ LL | const unsafe fn four() -> i32 {
8787
| +++++
8888

8989
error: this could be a `const fn`
90-
--> tests/ui/missing_const_for_fn/could_be_const.rs:52:1
90+
--> tests/ui/missing_const_for_fn/could_be_const.rs:53:1
9191
|
9292
LL | / fn generic<T>(t: T) -> T {
9393
LL | |
@@ -101,7 +101,7 @@ LL | const fn generic<T>(t: T) -> T {
101101
| +++++
102102

103103
error: this could be a `const fn`
104-
--> tests/ui/missing_const_for_fn/could_be_const.rs:61:1
104+
--> tests/ui/missing_const_for_fn/could_be_const.rs:62:1
105105
|
106106
LL | / fn generic_arr<T: Copy>(t: [T; 1]) -> T {
107107
LL | |
@@ -115,7 +115,7 @@ LL | const fn generic_arr<T: Copy>(t: [T; 1]) -> T {
115115
| +++++
116116

117117
error: this could be a `const fn`
118-
--> tests/ui/missing_const_for_fn/could_be_const.rs:75:9
118+
--> tests/ui/missing_const_for_fn/could_be_const.rs:76:9
119119
|
120120
LL | / pub fn b(self, a: &A) -> B {
121121
LL | |
@@ -129,7 +129,7 @@ LL | pub const fn b(self, a: &A) -> B {
129129
| +++++
130130

131131
error: this could be a `const fn`
132-
--> tests/ui/missing_const_for_fn/could_be_const.rs:85:5
132+
--> tests/ui/missing_const_for_fn/could_be_const.rs:86:5
133133
|
134134
LL | / fn const_fn_stabilized_before_msrv(byte: u8) {
135135
LL | |
@@ -143,7 +143,7 @@ LL | const fn const_fn_stabilized_before_msrv(byte: u8) {
143143
| +++++
144144

145145
error: this could be a `const fn`
146-
--> tests/ui/missing_const_for_fn/could_be_const.rs:97:1
146+
--> tests/ui/missing_const_for_fn/could_be_const.rs:98:1
147147
|
148148
LL | / fn msrv_1_46() -> i32 {
149149
LL | |
@@ -157,7 +157,7 @@ LL | const fn msrv_1_46() -> i32 {
157157
| +++++
158158

159159
error: this could be a `const fn`
160-
--> tests/ui/missing_const_for_fn/could_be_const.rs:117:1
160+
--> tests/ui/missing_const_for_fn/could_be_const.rs:118:1
161161
|
162162
LL | fn d(this: D) {}
163163
| ^^^^^^^^^^^^^^^^
@@ -168,7 +168,7 @@ LL | const fn d(this: D) {}
168168
| +++++
169169

170170
error: this could be a `const fn`
171-
--> tests/ui/missing_const_for_fn/could_be_const.rs:125:9
171+
--> tests/ui/missing_const_for_fn/could_be_const.rs:126:9
172172
|
173173
LL | / fn deref_ptr_can_be_const(self) -> usize {
174174
LL | |
@@ -182,7 +182,7 @@ LL | const fn deref_ptr_can_be_const(self) -> usize {
182182
| +++++
183183

184184
error: this could be a `const fn`
185-
--> tests/ui/missing_const_for_fn/could_be_const.rs:130:9
185+
--> tests/ui/missing_const_for_fn/could_be_const.rs:131:9
186186
|
187187
LL | / fn deref_copied_val(self) -> usize {
188188
LL | |
@@ -196,7 +196,7 @@ LL | const fn deref_copied_val(self) -> usize {
196196
| +++++
197197

198198
error: this could be a `const fn`
199-
--> tests/ui/missing_const_for_fn/could_be_const.rs:141:5
199+
--> tests/ui/missing_const_for_fn/could_be_const.rs:142:5
200200
|
201201
LL | / fn union_access_can_be_const() {
202202
LL | |
@@ -211,7 +211,7 @@ LL | const fn union_access_can_be_const() {
211211
| +++++
212212

213213
error: this could be a `const fn`
214-
--> tests/ui/missing_const_for_fn/could_be_const.rs:149:9
214+
--> tests/ui/missing_const_for_fn/could_be_const.rs:150:9
215215
|
216216
LL | extern "C" fn c() {}
217217
| ^^^^^^^^^^^^^^^^^^^^
@@ -222,7 +222,7 @@ LL | const extern "C" fn c() {}
222222
| +++++
223223

224224
error: this could be a `const fn`
225-
--> tests/ui/missing_const_for_fn/could_be_const.rs:153:9
225+
--> tests/ui/missing_const_for_fn/could_be_const.rs:154:9
226226
|
227227
LL | extern fn implicit_c() {}
228228
| ^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -233,7 +233,7 @@ LL | const extern fn implicit_c() {}
233233
| +++++
234234

235235
error: this could be a `const fn`
236-
--> tests/ui/missing_const_for_fn/could_be_const.rs:170:9
236+
--> tests/ui/missing_const_for_fn/could_be_const.rs:171:9
237237
|
238238
LL | / pub fn new(strings: Vec<String>) -> Self {
239239
LL | | Self { strings }
@@ -246,7 +246,7 @@ LL | pub const fn new(strings: Vec<String>) -> Self {
246246
| +++++
247247

248248
error: this could be a `const fn`
249-
--> tests/ui/missing_const_for_fn/could_be_const.rs:175:9
249+
--> tests/ui/missing_const_for_fn/could_be_const.rs:176:9
250250
|
251251
LL | / pub fn empty() -> Self {
252252
LL | | Self { strings: Vec::new() }
@@ -259,7 +259,7 @@ LL | pub const fn empty() -> Self {
259259
| +++++
260260

261261
error: this could be a `const fn`
262-
--> tests/ui/missing_const_for_fn/could_be_const.rs:186:9
262+
--> tests/ui/missing_const_for_fn/could_be_const.rs:187:9
263263
|
264264
LL | / pub fn new(text: String) -> Self {
265265
LL | | let vec = Vec::new();
@@ -273,7 +273,7 @@ LL | pub const fn new(text: String) -> Self {
273273
| +++++
274274

275275
error: this could be a `const fn`
276-
--> tests/ui/missing_const_for_fn/could_be_const.rs:205:5
276+
--> tests/ui/missing_const_for_fn/could_be_const.rs:206:5
277277
|
278278
LL | fn alias_ty_is_projection(bar: <() as FooTrait>::Foo) {}
279279
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -283,5 +283,60 @@ help: make the function `const`
283283
LL | const fn alias_ty_is_projection(bar: <() as FooTrait>::Foo) {}
284284
| +++++
285285

286-
error: aborting due to 21 previous errors
286+
error: this could be a `const fn`
287+
--> tests/ui/missing_const_for_fn/could_be_const.rs:210:5
288+
|
289+
LL | extern "C-unwind" fn c_unwind() {}
290+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
291+
|
292+
help: make the function `const`
293+
|
294+
LL | const extern "C-unwind" fn c_unwind() {}
295+
| +++++
296+
297+
error: this could be a `const fn`
298+
--> tests/ui/missing_const_for_fn/could_be_const.rs:212:5
299+
|
300+
LL | extern "system" fn system() {}
301+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
302+
|
303+
help: make the function `const`
304+
|
305+
LL | const extern "system" fn system() {}
306+
| +++++
307+
308+
error: this could be a `const fn`
309+
--> tests/ui/missing_const_for_fn/could_be_const.rs:214:5
310+
|
311+
LL | extern "system-unwind" fn system_unwind() {}
312+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
313+
|
314+
help: make the function `const`
315+
|
316+
LL | const extern "system-unwind" fn system_unwind() {}
317+
| +++++
318+
319+
error: this could be a `const fn`
320+
--> tests/ui/missing_const_for_fn/could_be_const.rs:216:5
321+
|
322+
LL | pub extern "stdcall" fn std_call() {}
323+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
324+
|
325+
help: make the function `const`
326+
|
327+
LL | pub const extern "stdcall" fn std_call() {}
328+
| +++++
329+
330+
error: this could be a `const fn`
331+
--> tests/ui/missing_const_for_fn/could_be_const.rs:218:5
332+
|
333+
LL | pub extern "stdcall-unwind" fn std_call_unwind() {}
334+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
335+
|
336+
help: make the function `const`
337+
|
338+
LL | pub const extern "stdcall-unwind" fn std_call_unwind() {}
339+
| +++++
340+
341+
error: aborting due to 26 previous errors
287342

tests/ui/missing_const_for_fn/could_be_const_with_const_extern_fn.fixed

Lines changed: 0 additions & 14 deletions
This file was deleted.

tests/ui/missing_const_for_fn/could_be_const_with_const_extern_fn.rs

Lines changed: 0 additions & 14 deletions
This file was deleted.

0 commit comments

Comments
 (0)