From b5ea5c23b34129f5af82ccf5b9e5babd9bbc9285 Mon Sep 17 00:00:00 2001 From: Folkert de Vries Date: Thu, 29 Aug 2024 15:33:34 +0200 Subject: [PATCH] stabilize `const_extern_fn` --- clippy_config/src/msrvs.rs | 3 +- clippy_lints/src/missing_const_for_fn.rs | 10 +- .../ui/missing_const_for_fn/cant_be_const.rs | 6 -- .../missing_const_for_fn/could_be_const.fixed | 14 +++ .../ui/missing_const_for_fn/could_be_const.rs | 14 +++ .../could_be_const.stderr | 99 ++++++++++++++----- .../could_be_const_with_const_extern_fn.fixed | 14 --- .../could_be_const_with_const_extern_fn.rs | 14 --- ...could_be_const_with_const_extern_fn.stderr | 59 ----------- 9 files changed, 111 insertions(+), 122 deletions(-) delete mode 100644 tests/ui/missing_const_for_fn/could_be_const_with_const_extern_fn.fixed delete mode 100644 tests/ui/missing_const_for_fn/could_be_const_with_const_extern_fn.rs delete mode 100644 tests/ui/missing_const_for_fn/could_be_const_with_const_extern_fn.stderr diff --git a/clippy_config/src/msrvs.rs b/clippy_config/src/msrvs.rs index e17458b3310..a2b4b6e256f 100644 --- a/clippy_config/src/msrvs.rs +++ b/clippy_config/src/msrvs.rs @@ -17,6 +17,7 @@ macro_rules! msrv_aliases { // names may refer to stabilized feature flags or library items msrv_aliases! { + 1,83,0 { CONST_EXTERN_FN } 1,83,0 { CONST_FLOAT_BITS_CONV } 1,81,0 { LINT_REASONS_STABILIZATION } 1,80,0 { BOX_INTO_ITER} @@ -27,7 +28,7 @@ msrv_aliases! { 1,68,0 { PATH_MAIN_SEPARATOR_STR } 1,65,0 { LET_ELSE, POINTER_CAST_CONSTNESS } 1,63,0 { CLONE_INTO } - 1,62,0 { BOOL_THEN_SOME, DEFAULT_ENUM_ATTRIBUTE, CONST_EXTERN_FN } + 1,62,0 { BOOL_THEN_SOME, DEFAULT_ENUM_ATTRIBUTE, CONST_EXTERN_C_FN } 1,59,0 { THREAD_LOCAL_CONST_INIT } 1,58,0 { FORMAT_ARGS_CAPTURE, PATTERN_TRAIT_CHAR_ARRAY, CONST_RAW_PTR_DEREF } 1,56,0 { CONST_FN_UNION } diff --git a/clippy_lints/src/missing_const_for_fn.rs b/clippy_lints/src/missing_const_for_fn.rs index 052d738b2e7..859afe1b963 100644 --- a/clippy_lints/src/missing_const_for_fn.rs +++ b/clippy_lints/src/missing_const_for_fn.rs @@ -119,9 +119,7 @@ impl<'tcx> LateLintPass<'tcx> for MissingConstForFn { .iter() .any(|param| matches!(param.kind, GenericParamKind::Const { .. })); - if already_const(header) - || has_const_generic_params - || !could_be_const_with_abi(cx, &self.msrv, header.abi) + if already_const(header) || has_const_generic_params || !could_be_const_with_abi(&self.msrv, header.abi) { return; } @@ -183,13 +181,13 @@ fn already_const(header: hir::FnHeader) -> bool { header.constness == Constness::Const } -fn could_be_const_with_abi(cx: &LateContext<'_>, msrv: &Msrv, abi: Abi) -> bool { +fn could_be_const_with_abi(msrv: &Msrv, abi: Abi) -> bool { match abi { Abi::Rust => true, // `const extern "C"` was stabilized after 1.62.0 - Abi::C { unwind: false } => msrv.meets(msrvs::CONST_EXTERN_FN), + Abi::C { unwind: false } => msrv.meets(msrvs::CONST_EXTERN_C_FN), // Rest ABIs are still unstable and need the `const_extern_fn` feature enabled. - _ => cx.tcx.features().const_extern_fn, + _ => msrv.meets(msrvs::CONST_EXTERN_FN), } } diff --git a/tests/ui/missing_const_for_fn/cant_be_const.rs b/tests/ui/missing_const_for_fn/cant_be_const.rs index 2c6e1e92da0..ca323dcf173 100644 --- a/tests/ui/missing_const_for_fn/cant_be_const.rs +++ b/tests/ui/missing_const_for_fn/cant_be_const.rs @@ -186,12 +186,6 @@ mod msrv { extern "C" fn c() {} } -mod with_extern { - extern "C-unwind" fn c_unwind() {} - extern "system" fn system() {} - extern "system-unwind" fn system_unwind() {} -} - mod with_ty_alias { type Foo = impl std::fmt::Debug; diff --git a/tests/ui/missing_const_for_fn/could_be_const.fixed b/tests/ui/missing_const_for_fn/could_be_const.fixed index f54503ada97..0aef4d31fd9 100644 --- a/tests/ui/missing_const_for_fn/could_be_const.fixed +++ b/tests/ui/missing_const_for_fn/could_be_const.fixed @@ -1,5 +1,6 @@ #![warn(clippy::missing_const_for_fn)] #![allow(incomplete_features, clippy::let_and_return, clippy::missing_transmute_annotations)] +#![allow(unsupported_calling_conventions)] #![feature(const_mut_refs)] #![feature(const_trait_impl)] @@ -204,3 +205,16 @@ mod with_ty_alias { // in this test. const fn alias_ty_is_projection(bar: <() as FooTrait>::Foo) {} } + +mod extern_fn { + const extern "C-unwind" fn c_unwind() {} + //~^ ERROR: this could be a `const fn` + const extern "system" fn system() {} + //~^ ERROR: this could be a `const fn` + const extern "system-unwind" fn system_unwind() {} + //~^ ERROR: this could be a `const fn` + pub const extern "stdcall" fn std_call() {} + //~^ ERROR: this could be a `const fn` + pub const extern "stdcall-unwind" fn std_call_unwind() {} + //~^ ERROR: this could be a `const fn` +} diff --git a/tests/ui/missing_const_for_fn/could_be_const.rs b/tests/ui/missing_const_for_fn/could_be_const.rs index 2c229068e4d..4246494fe72 100644 --- a/tests/ui/missing_const_for_fn/could_be_const.rs +++ b/tests/ui/missing_const_for_fn/could_be_const.rs @@ -1,5 +1,6 @@ #![warn(clippy::missing_const_for_fn)] #![allow(incomplete_features, clippy::let_and_return, clippy::missing_transmute_annotations)] +#![allow(unsupported_calling_conventions)] #![feature(const_mut_refs)] #![feature(const_trait_impl)] @@ -204,3 +205,16 @@ mod with_ty_alias { // in this test. fn alias_ty_is_projection(bar: <() as FooTrait>::Foo) {} } + +mod extern_fn { + extern "C-unwind" fn c_unwind() {} + //~^ ERROR: this could be a `const fn` + extern "system" fn system() {} + //~^ ERROR: this could be a `const fn` + extern "system-unwind" fn system_unwind() {} + //~^ ERROR: this could be a `const fn` + pub extern "stdcall" fn std_call() {} + //~^ ERROR: this could be a `const fn` + pub extern "stdcall-unwind" fn std_call_unwind() {} + //~^ ERROR: this could be a `const fn` +} diff --git a/tests/ui/missing_const_for_fn/could_be_const.stderr b/tests/ui/missing_const_for_fn/could_be_const.stderr index fb4db703103..6bc71e29840 100644 --- a/tests/ui/missing_const_for_fn/could_be_const.stderr +++ b/tests/ui/missing_const_for_fn/could_be_const.stderr @@ -1,5 +1,5 @@ error: this could be a `const fn` - --> tests/ui/missing_const_for_fn/could_be_const.rs:14:5 + --> tests/ui/missing_const_for_fn/could_be_const.rs:15:5 | LL | / pub fn new() -> Self { LL | | @@ -16,7 +16,7 @@ LL | pub const fn new() -> Self { | +++++ error: this could be a `const fn` - --> tests/ui/missing_const_for_fn/could_be_const.rs:20:5 + --> tests/ui/missing_const_for_fn/could_be_const.rs:21:5 | LL | / fn const_generic_params<'a, T, const N: usize>(&self, b: &'a [T; N]) -> &'a [T; N] { LL | | @@ -30,7 +30,7 @@ LL | const fn const_generic_params<'a, T, const N: usize>(&self, b: &'a [T; | +++++ error: this could be a `const fn` - --> tests/ui/missing_const_for_fn/could_be_const.rs:27:1 + --> tests/ui/missing_const_for_fn/could_be_const.rs:28:1 | LL | / fn one() -> i32 { LL | | @@ -44,7 +44,7 @@ LL | const fn one() -> i32 { | +++++ error: this could be a `const fn` - --> tests/ui/missing_const_for_fn/could_be_const.rs:33:1 + --> tests/ui/missing_const_for_fn/could_be_const.rs:34:1 | LL | / fn two() -> i32 { LL | | @@ -59,7 +59,7 @@ LL | const fn two() -> i32 { | +++++ error: this could be a `const fn` - --> tests/ui/missing_const_for_fn/could_be_const.rs:40:1 + --> tests/ui/missing_const_for_fn/could_be_const.rs:41:1 | LL | / fn string() -> String { LL | | @@ -73,7 +73,7 @@ LL | const fn string() -> String { | +++++ error: this could be a `const fn` - --> tests/ui/missing_const_for_fn/could_be_const.rs:46:1 + --> tests/ui/missing_const_for_fn/could_be_const.rs:47:1 | LL | / unsafe fn four() -> i32 { LL | | @@ -87,7 +87,7 @@ LL | const unsafe fn four() -> i32 { | +++++ error: this could be a `const fn` - --> tests/ui/missing_const_for_fn/could_be_const.rs:52:1 + --> tests/ui/missing_const_for_fn/could_be_const.rs:53:1 | LL | / fn generic(t: T) -> T { LL | | @@ -101,7 +101,7 @@ LL | const fn generic(t: T) -> T { | +++++ error: this could be a `const fn` - --> tests/ui/missing_const_for_fn/could_be_const.rs:61:1 + --> tests/ui/missing_const_for_fn/could_be_const.rs:62:1 | LL | / fn generic_arr(t: [T; 1]) -> T { LL | | @@ -115,7 +115,7 @@ LL | const fn generic_arr(t: [T; 1]) -> T { | +++++ error: this could be a `const fn` - --> tests/ui/missing_const_for_fn/could_be_const.rs:75:9 + --> tests/ui/missing_const_for_fn/could_be_const.rs:76:9 | LL | / pub fn b(self, a: &A) -> B { LL | | @@ -129,7 +129,7 @@ LL | pub const fn b(self, a: &A) -> B { | +++++ error: this could be a `const fn` - --> tests/ui/missing_const_for_fn/could_be_const.rs:85:5 + --> tests/ui/missing_const_for_fn/could_be_const.rs:86:5 | LL | / fn const_fn_stabilized_before_msrv(byte: u8) { LL | | @@ -143,7 +143,7 @@ LL | const fn const_fn_stabilized_before_msrv(byte: u8) { | +++++ error: this could be a `const fn` - --> tests/ui/missing_const_for_fn/could_be_const.rs:97:1 + --> tests/ui/missing_const_for_fn/could_be_const.rs:98:1 | LL | / fn msrv_1_46() -> i32 { LL | | @@ -157,7 +157,7 @@ LL | const fn msrv_1_46() -> i32 { | +++++ error: this could be a `const fn` - --> tests/ui/missing_const_for_fn/could_be_const.rs:117:1 + --> tests/ui/missing_const_for_fn/could_be_const.rs:118:1 | LL | fn d(this: D) {} | ^^^^^^^^^^^^^^^^ @@ -168,7 +168,7 @@ LL | const fn d(this: D) {} | +++++ error: this could be a `const fn` - --> tests/ui/missing_const_for_fn/could_be_const.rs:125:9 + --> tests/ui/missing_const_for_fn/could_be_const.rs:126:9 | LL | / fn deref_ptr_can_be_const(self) -> usize { LL | | @@ -182,7 +182,7 @@ LL | const fn deref_ptr_can_be_const(self) -> usize { | +++++ error: this could be a `const fn` - --> tests/ui/missing_const_for_fn/could_be_const.rs:130:9 + --> tests/ui/missing_const_for_fn/could_be_const.rs:131:9 | LL | / fn deref_copied_val(self) -> usize { LL | | @@ -196,7 +196,7 @@ LL | const fn deref_copied_val(self) -> usize { | +++++ error: this could be a `const fn` - --> tests/ui/missing_const_for_fn/could_be_const.rs:141:5 + --> tests/ui/missing_const_for_fn/could_be_const.rs:142:5 | LL | / fn union_access_can_be_const() { LL | | @@ -211,7 +211,7 @@ LL | const fn union_access_can_be_const() { | +++++ error: this could be a `const fn` - --> tests/ui/missing_const_for_fn/could_be_const.rs:149:9 + --> tests/ui/missing_const_for_fn/could_be_const.rs:150:9 | LL | extern "C" fn c() {} | ^^^^^^^^^^^^^^^^^^^^ @@ -222,7 +222,7 @@ LL | const extern "C" fn c() {} | +++++ error: this could be a `const fn` - --> tests/ui/missing_const_for_fn/could_be_const.rs:153:9 + --> tests/ui/missing_const_for_fn/could_be_const.rs:154:9 | LL | extern fn implicit_c() {} | ^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -233,7 +233,7 @@ LL | const extern fn implicit_c() {} | +++++ error: this could be a `const fn` - --> tests/ui/missing_const_for_fn/could_be_const.rs:170:9 + --> tests/ui/missing_const_for_fn/could_be_const.rs:171:9 | LL | / pub fn new(strings: Vec) -> Self { LL | | Self { strings } @@ -246,7 +246,7 @@ LL | pub const fn new(strings: Vec) -> Self { | +++++ error: this could be a `const fn` - --> tests/ui/missing_const_for_fn/could_be_const.rs:175:9 + --> tests/ui/missing_const_for_fn/could_be_const.rs:176:9 | LL | / pub fn empty() -> Self { LL | | Self { strings: Vec::new() } @@ -259,7 +259,7 @@ LL | pub const fn empty() -> Self { | +++++ error: this could be a `const fn` - --> tests/ui/missing_const_for_fn/could_be_const.rs:186:9 + --> tests/ui/missing_const_for_fn/could_be_const.rs:187:9 | LL | / pub fn new(text: String) -> Self { LL | | let vec = Vec::new(); @@ -273,7 +273,7 @@ LL | pub const fn new(text: String) -> Self { | +++++ error: this could be a `const fn` - --> tests/ui/missing_const_for_fn/could_be_const.rs:205:5 + --> tests/ui/missing_const_for_fn/could_be_const.rs:206:5 | LL | fn alias_ty_is_projection(bar: <() as FooTrait>::Foo) {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -283,5 +283,60 @@ help: make the function `const` LL | const fn alias_ty_is_projection(bar: <() as FooTrait>::Foo) {} | +++++ -error: aborting due to 21 previous errors +error: this could be a `const fn` + --> tests/ui/missing_const_for_fn/could_be_const.rs:210:5 + | +LL | extern "C-unwind" fn c_unwind() {} + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: make the function `const` + | +LL | const extern "C-unwind" fn c_unwind() {} + | +++++ + +error: this could be a `const fn` + --> tests/ui/missing_const_for_fn/could_be_const.rs:212:5 + | +LL | extern "system" fn system() {} + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: make the function `const` + | +LL | const extern "system" fn system() {} + | +++++ + +error: this could be a `const fn` + --> tests/ui/missing_const_for_fn/could_be_const.rs:214:5 + | +LL | extern "system-unwind" fn system_unwind() {} + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: make the function `const` + | +LL | const extern "system-unwind" fn system_unwind() {} + | +++++ + +error: this could be a `const fn` + --> tests/ui/missing_const_for_fn/could_be_const.rs:216:5 + | +LL | pub extern "stdcall" fn std_call() {} + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: make the function `const` + | +LL | pub const extern "stdcall" fn std_call() {} + | +++++ + +error: this could be a `const fn` + --> tests/ui/missing_const_for_fn/could_be_const.rs:218:5 + | +LL | pub extern "stdcall-unwind" fn std_call_unwind() {} + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: make the function `const` + | +LL | pub const extern "stdcall-unwind" fn std_call_unwind() {} + | +++++ + +error: aborting due to 26 previous errors diff --git a/tests/ui/missing_const_for_fn/could_be_const_with_const_extern_fn.fixed b/tests/ui/missing_const_for_fn/could_be_const_with_const_extern_fn.fixed deleted file mode 100644 index c103db536ab..00000000000 --- a/tests/ui/missing_const_for_fn/could_be_const_with_const_extern_fn.fixed +++ /dev/null @@ -1,14 +0,0 @@ -#![warn(clippy::missing_const_for_fn)] -#![allow(unsupported_calling_conventions)] -#![feature(const_extern_fn)] - -const extern "C-unwind" fn c_unwind() {} -//~^ ERROR: this could be a `const fn` -const extern "system" fn system() {} -//~^ ERROR: this could be a `const fn` -const extern "system-unwind" fn system_unwind() {} -//~^ ERROR: this could be a `const fn` -pub const extern "stdcall" fn std_call() {} -//~^ ERROR: this could be a `const fn` -pub const extern "stdcall-unwind" fn std_call_unwind() {} -//~^ ERROR: this could be a `const fn` diff --git a/tests/ui/missing_const_for_fn/could_be_const_with_const_extern_fn.rs b/tests/ui/missing_const_for_fn/could_be_const_with_const_extern_fn.rs deleted file mode 100644 index 0f7020ae559..00000000000 --- a/tests/ui/missing_const_for_fn/could_be_const_with_const_extern_fn.rs +++ /dev/null @@ -1,14 +0,0 @@ -#![warn(clippy::missing_const_for_fn)] -#![allow(unsupported_calling_conventions)] -#![feature(const_extern_fn)] - -extern "C-unwind" fn c_unwind() {} -//~^ ERROR: this could be a `const fn` -extern "system" fn system() {} -//~^ ERROR: this could be a `const fn` -extern "system-unwind" fn system_unwind() {} -//~^ ERROR: this could be a `const fn` -pub extern "stdcall" fn std_call() {} -//~^ ERROR: this could be a `const fn` -pub extern "stdcall-unwind" fn std_call_unwind() {} -//~^ ERROR: this could be a `const fn` diff --git a/tests/ui/missing_const_for_fn/could_be_const_with_const_extern_fn.stderr b/tests/ui/missing_const_for_fn/could_be_const_with_const_extern_fn.stderr deleted file mode 100644 index 036094a367b..00000000000 --- a/tests/ui/missing_const_for_fn/could_be_const_with_const_extern_fn.stderr +++ /dev/null @@ -1,59 +0,0 @@ -error: this could be a `const fn` - --> tests/ui/missing_const_for_fn/could_be_const_with_const_extern_fn.rs:5:1 - | -LL | extern "C-unwind" fn c_unwind() {} - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | - = note: `-D clippy::missing-const-for-fn` implied by `-D warnings` - = help: to override `-D warnings` add `#[allow(clippy::missing_const_for_fn)]` -help: make the function `const` - | -LL | const extern "C-unwind" fn c_unwind() {} - | +++++ - -error: this could be a `const fn` - --> tests/ui/missing_const_for_fn/could_be_const_with_const_extern_fn.rs:7:1 - | -LL | extern "system" fn system() {} - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | -help: make the function `const` - | -LL | const extern "system" fn system() {} - | +++++ - -error: this could be a `const fn` - --> tests/ui/missing_const_for_fn/could_be_const_with_const_extern_fn.rs:9:1 - | -LL | extern "system-unwind" fn system_unwind() {} - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | -help: make the function `const` - | -LL | const extern "system-unwind" fn system_unwind() {} - | +++++ - -error: this could be a `const fn` - --> tests/ui/missing_const_for_fn/could_be_const_with_const_extern_fn.rs:11:1 - | -LL | pub extern "stdcall" fn std_call() {} - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | -help: make the function `const` - | -LL | pub const extern "stdcall" fn std_call() {} - | +++++ - -error: this could be a `const fn` - --> tests/ui/missing_const_for_fn/could_be_const_with_const_extern_fn.rs:13:1 - | -LL | pub extern "stdcall-unwind" fn std_call_unwind() {} - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | -help: make the function `const` - | -LL | pub const extern "stdcall-unwind" fn std_call_unwind() {} - | +++++ - -error: aborting due to 5 previous errors -