Skip to content

Commit 3a71223

Browse files
committed
[missing_const_for_fn]: fix suggestions for fn with abi that requires const_extern_fn feature
1 parent a4bdab3 commit 3a71223

File tree

9 files changed

+168
-6
lines changed

9 files changed

+168
-6
lines changed

clippy_config/src/msrvs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ msrv_aliases! {
2525
1,68,0 { PATH_MAIN_SEPARATOR_STR }
2626
1,65,0 { LET_ELSE, POINTER_CAST_CONSTNESS }
2727
1,63,0 { CLONE_INTO }
28-
1,62,0 { BOOL_THEN_SOME, DEFAULT_ENUM_ATTRIBUTE }
28+
1,62,0 { BOOL_THEN_SOME, DEFAULT_ENUM_ATTRIBUTE, CONST_EXTERN_FN }
2929
1,59,0 { THREAD_LOCAL_INITIALIZER_CAN_BE_MADE_CONST }
3030
1,58,0 { FORMAT_ARGS_CAPTURE, PATTERN_TRAIT_CHAR_ARRAY, CONST_RAW_PTR_DEREF }
3131
1,56,0 { CONST_FN_UNION }

clippy_lints/src/missing_const_for_fn.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use rustc_middle::lint::in_external_macro;
1111
use rustc_session::impl_lint_pass;
1212
use rustc_span::def_id::LocalDefId;
1313
use rustc_span::Span;
14+
use rustc_target::spec::abi::Abi;
1415

1516
declare_clippy_lint! {
1617
/// ### What it does
@@ -115,7 +116,10 @@ impl<'tcx> LateLintPass<'tcx> for MissingConstForFn {
115116
.iter()
116117
.any(|param| matches!(param.kind, GenericParamKind::Const { .. }));
117118

118-
if already_const(header) || has_const_generic_params {
119+
if already_const(header)
120+
|| has_const_generic_params
121+
|| !could_be_const_with_abi(cx, &self.msrv, header.abi)
122+
{
119123
return;
120124
}
121125
},
@@ -171,3 +175,13 @@ impl<'tcx> LateLintPass<'tcx> for MissingConstForFn {
171175
fn already_const(header: hir::FnHeader) -> bool {
172176
header.constness == Constness::Const
173177
}
178+
179+
fn could_be_const_with_abi(cx: &LateContext<'_>, msrv: &Msrv, abi: Abi) -> bool {
180+
match abi {
181+
Abi::Rust => true,
182+
// `const extern "C"` was stablized after 1.62.0
183+
Abi::C { unwind: false } => msrv.meets(msrvs::CONST_EXTERN_FN),
184+
// Rest ABIs are still unstable and need the `const_extern_fn` feature enabled.
185+
_ => cx.tcx.features().const_extern_fn,
186+
}
187+
}

tests/ui/missing_const_for_fn/cant_be_const.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,4 +180,13 @@ mod msrv {
180180
unsafe { *self.1 as usize }
181181
}
182182
}
183+
184+
#[clippy::msrv = "1.61"]
185+
extern "C" fn c() {}
186+
}
187+
188+
mod with_extern {
189+
extern "C-unwind" fn c_unwind() {}
190+
extern "system" fn system() {}
191+
extern "system-unwind" fn system_unwind() {}
183192
}

tests/ui/missing_const_for_fn/could_be_const.fixed

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,21 @@ mod msrv {
140140
let bar = Bar { val: 1 };
141141
let _ = unsafe { bar.val };
142142
}
143+
144+
#[clippy::msrv = "1.62"]
145+
mod with_extern {
146+
const extern "C" fn c() {}
147+
//~^ ERROR: this could be a `const fn`
148+
149+
#[rustfmt::skip]
150+
const extern fn implicit_c() {}
151+
//~^ ERROR: this could be a `const fn`
152+
153+
// any item functions in extern block won't trigger this lint
154+
extern "C" {
155+
fn c_in_block();
156+
}
157+
}
143158
}
144159

145160
mod issue12677 {

tests/ui/missing_const_for_fn/could_be_const.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,21 @@ mod msrv {
140140
let bar = Bar { val: 1 };
141141
let _ = unsafe { bar.val };
142142
}
143+
144+
#[clippy::msrv = "1.62"]
145+
mod with_extern {
146+
extern "C" fn c() {}
147+
//~^ ERROR: this could be a `const fn`
148+
149+
#[rustfmt::skip]
150+
extern fn implicit_c() {}
151+
//~^ ERROR: this could be a `const fn`
152+
153+
// any item functions in extern block won't trigger this lint
154+
extern "C" {
155+
fn c_in_block();
156+
}
157+
}
143158
}
144159

145160
mod issue12677 {

tests/ui/missing_const_for_fn/could_be_const.stderr

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,29 @@ LL | const fn union_access_can_be_const() {
200200
| +++++
201201

202202
error: this could be a `const fn`
203-
--> tests/ui/missing_const_for_fn/could_be_const.rs:152:9
203+
--> tests/ui/missing_const_for_fn/could_be_const.rs:146:9
204+
|
205+
LL | extern "C" fn c() {}
206+
| ^^^^^^^^^^^^^^^^^^^^
207+
|
208+
help: make the function `const`
209+
|
210+
LL | const extern "C" fn c() {}
211+
| +++++
212+
213+
error: this could be a `const fn`
214+
--> tests/ui/missing_const_for_fn/could_be_const.rs:150:9
215+
|
216+
LL | extern fn implicit_c() {}
217+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
218+
|
219+
help: make the function `const`
220+
|
221+
LL | const extern fn implicit_c() {}
222+
| +++++
223+
224+
error: this could be a `const fn`
225+
--> tests/ui/missing_const_for_fn/could_be_const.rs:167:9
204226
|
205227
LL | / pub fn new(strings: Vec<String>) -> Self {
206228
LL | | Self { strings }
@@ -213,7 +235,7 @@ LL | pub const fn new(strings: Vec<String>) -> Self {
213235
| +++++
214236

215237
error: this could be a `const fn`
216-
--> tests/ui/missing_const_for_fn/could_be_const.rs:157:9
238+
--> tests/ui/missing_const_for_fn/could_be_const.rs:172:9
217239
|
218240
LL | / pub fn empty() -> Self {
219241
LL | | Self { strings: Vec::new() }
@@ -226,7 +248,7 @@ LL | pub const fn empty() -> Self {
226248
| +++++
227249

228250
error: this could be a `const fn`
229-
--> tests/ui/missing_const_for_fn/could_be_const.rs:168:9
251+
--> tests/ui/missing_const_for_fn/could_be_const.rs:183:9
230252
|
231253
LL | / pub fn new(text: String) -> Self {
232254
LL | | let vec = Vec::new();
@@ -239,5 +261,5 @@ help: make the function `const`
239261
LL | pub const fn new(text: String) -> Self {
240262
| +++++
241263

242-
error: aborting due to 17 previous errors
264+
error: aborting due to 19 previous errors
243265

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#![warn(clippy::missing_const_for_fn)]
2+
#![allow(unsupported_calling_conventions)]
3+
#![feature(const_extern_fn)]
4+
5+
const extern "C-unwind" fn c_unwind() {}
6+
//~^ ERROR: this could be a `const fn`
7+
const extern "system" fn system() {}
8+
//~^ ERROR: this could be a `const fn`
9+
const extern "system-unwind" fn system_unwind() {}
10+
//~^ ERROR: this could be a `const fn`
11+
pub const extern "stdcall" fn std_call() {}
12+
//~^ ERROR: this could be a `const fn`
13+
pub const extern "stdcall-unwind" fn std_call_unwind() {}
14+
//~^ ERROR: this could be a `const fn`
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#![warn(clippy::missing_const_for_fn)]
2+
#![allow(unsupported_calling_conventions)]
3+
#![feature(const_extern_fn)]
4+
5+
extern "C-unwind" fn c_unwind() {}
6+
//~^ ERROR: this could be a `const fn`
7+
extern "system" fn system() {}
8+
//~^ ERROR: this could be a `const fn`
9+
extern "system-unwind" fn system_unwind() {}
10+
//~^ ERROR: this could be a `const fn`
11+
pub extern "stdcall" fn std_call() {}
12+
//~^ ERROR: this could be a `const fn`
13+
pub extern "stdcall-unwind" fn std_call_unwind() {}
14+
//~^ ERROR: this could be a `const fn`
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
error: this could be a `const fn`
2+
--> tests/ui/missing_const_for_fn/could_be_const_with_const_extern_fn.rs:5:1
3+
|
4+
LL | extern "C-unwind" fn c_unwind() {}
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: `-D clippy::missing-const-for-fn` implied by `-D warnings`
8+
= help: to override `-D warnings` add `#[allow(clippy::missing_const_for_fn)]`
9+
help: make the function `const`
10+
|
11+
LL | const extern "C-unwind" fn c_unwind() {}
12+
| +++++
13+
14+
error: this could be a `const fn`
15+
--> tests/ui/missing_const_for_fn/could_be_const_with_const_extern_fn.rs:7:1
16+
|
17+
LL | extern "system" fn system() {}
18+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
19+
|
20+
help: make the function `const`
21+
|
22+
LL | const extern "system" fn system() {}
23+
| +++++
24+
25+
error: this could be a `const fn`
26+
--> tests/ui/missing_const_for_fn/could_be_const_with_const_extern_fn.rs:9:1
27+
|
28+
LL | extern "system-unwind" fn system_unwind() {}
29+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
30+
|
31+
help: make the function `const`
32+
|
33+
LL | const extern "system-unwind" fn system_unwind() {}
34+
| +++++
35+
36+
error: this could be a `const fn`
37+
--> tests/ui/missing_const_for_fn/could_be_const_with_const_extern_fn.rs:11:1
38+
|
39+
LL | pub extern "stdcall" fn std_call() {}
40+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
41+
|
42+
help: make the function `const`
43+
|
44+
LL | pub const extern "stdcall" fn std_call() {}
45+
| +++++
46+
47+
error: this could be a `const fn`
48+
--> tests/ui/missing_const_for_fn/could_be_const_with_const_extern_fn.rs:13:1
49+
|
50+
LL | pub extern "stdcall-unwind" fn std_call_unwind() {}
51+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
52+
|
53+
help: make the function `const`
54+
|
55+
LL | pub const extern "stdcall-unwind" fn std_call_unwind() {}
56+
| +++++
57+
58+
error: aborting due to 5 previous errors
59+

0 commit comments

Comments
 (0)