Skip to content

Commit 7e49122

Browse files
committedFeb 22, 2020
Auto merge of rust-lang#5216 - krishna-veerareddy:issue-5192-fp-const-fn, r=flip1995
Prevent `missing_const_for_fn` on functions with const generic params `const` functions cannot have const generic parameters so prevent the `missing_const_for_fn` lint from firing in that case. changelog: Fix false positive in `missing_const_for_fn` Fixes rust-lang#5192
2 parents e342047 + 0490798 commit 7e49122

File tree

4 files changed

+44
-14
lines changed

4 files changed

+44
-14
lines changed
 

‎clippy_lints/src/missing_const_for_fn.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ use crate::utils::{has_drop, is_entrypoint_fn, span_lint, trait_ref_of_method};
22
use rustc::lint::in_external_macro;
33
use rustc_hir as hir;
44
use rustc_hir::intravisit::FnKind;
5-
use rustc_hir::{Body, Constness, FnDecl, HirId};
5+
use rustc_hir::{Body, Constness, FnDecl, GenericParamKind, HirId};
66
use rustc_lint::{LateContext, LateLintPass};
77
use rustc_mir::transform::qualify_min_const_fn::is_min_const_fn;
88
use rustc_session::{declare_lint_pass, declare_tool_lint};
99
use rustc_span::Span;
1010
use rustc_typeck::hir_ty_to_ty;
11+
use std::matches;
1112

1213
declare_clippy_lint! {
1314
/// **What it does:**
@@ -90,8 +91,13 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingConstForFn {
9091
// Perform some preliminary checks that rule out constness on the Clippy side. This way we
9192
// can skip the actual const check and return early.
9293
match kind {
93-
FnKind::ItemFn(_, _, header, ..) => {
94-
if already_const(header) {
94+
FnKind::ItemFn(_, generics, header, ..) => {
95+
let has_const_generic_params = generics
96+
.params
97+
.iter()
98+
.any(|param| matches!(param.kind, GenericParamKind::Const{ .. }));
99+
100+
if already_const(header) || has_const_generic_params {
95101
return;
96102
}
97103
},

‎tests/ui/missing_const_for_fn/cant_be_const.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
//! The .stderr output of this test should be empty. Otherwise it's a bug somewhere.
44
55
#![warn(clippy::missing_const_for_fn)]
6-
#![feature(start)]
6+
#![allow(incomplete_features)]
7+
#![feature(start, const_generics)]
78

89
struct Game;
910

@@ -90,3 +91,13 @@ mod with_drop {
9091
}
9192
}
9293
}
94+
95+
fn const_generic_params<T, const N: usize>(t: &[T; N]) -> &[T; N] {
96+
t
97+
}
98+
99+
fn const_generic_return<T, const N: usize>(t: &[T]) -> &[T; N] {
100+
let p = t.as_ptr() as *const [T; N];
101+
102+
unsafe { &*p }
103+
}

‎tests/ui/missing_const_for_fn/could_be_const.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#![warn(clippy::missing_const_for_fn)]
2-
#![allow(clippy::let_and_return)]
2+
#![allow(incomplete_features, clippy::let_and_return)]
3+
#![feature(const_generics)]
34

45
use std::mem::transmute;
56

@@ -12,6 +13,10 @@ impl Game {
1213
pub fn new() -> Self {
1314
Self { guess: 42 }
1415
}
16+
17+
fn const_generic_params<'a, T, const N: usize>(&self, b: &'a [T; N]) -> &'a [T; N] {
18+
b
19+
}
1520
}
1621

1722
// Could be const
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: this could be a `const fn`
2-
--> $DIR/could_be_const.rs:12:5
2+
--> $DIR/could_be_const.rs:13:5
33
|
44
LL | / pub fn new() -> Self {
55
LL | | Self { guess: 42 }
@@ -9,15 +9,23 @@ LL | | }
99
= note: `-D clippy::missing-const-for-fn` implied by `-D warnings`
1010

1111
error: this could be a `const fn`
12-
--> $DIR/could_be_const.rs:18:1
12+
--> $DIR/could_be_const.rs:17:5
13+
|
14+
LL | / fn const_generic_params<'a, T, const N: usize>(&self, b: &'a [T; N]) -> &'a [T; N] {
15+
LL | | b
16+
LL | | }
17+
| |_____^
18+
19+
error: this could be a `const fn`
20+
--> $DIR/could_be_const.rs:23:1
1321
|
1422
LL | / fn one() -> i32 {
1523
LL | | 1
1624
LL | | }
1725
| |_^
1826

1927
error: this could be a `const fn`
20-
--> $DIR/could_be_const.rs:23:1
28+
--> $DIR/could_be_const.rs:28:1
2129
|
2230
LL | / fn two() -> i32 {
2331
LL | | let abc = 2;
@@ -26,44 +34,44 @@ LL | | }
2634
| |_^
2735

2836
error: this could be a `const fn`
29-
--> $DIR/could_be_const.rs:29:1
37+
--> $DIR/could_be_const.rs:34:1
3038
|
3139
LL | / fn string() -> String {
3240
LL | | String::new()
3341
LL | | }
3442
| |_^
3543

3644
error: this could be a `const fn`
37-
--> $DIR/could_be_const.rs:34:1
45+
--> $DIR/could_be_const.rs:39:1
3846
|
3947
LL | / unsafe fn four() -> i32 {
4048
LL | | 4
4149
LL | | }
4250
| |_^
4351

4452
error: this could be a `const fn`
45-
--> $DIR/could_be_const.rs:39:1
53+
--> $DIR/could_be_const.rs:44:1
4654
|
4755
LL | / fn generic<T>(t: T) -> T {
4856
LL | | t
4957
LL | | }
5058
| |_^
5159

5260
error: this could be a `const fn`
53-
--> $DIR/could_be_const.rs:43:1
61+
--> $DIR/could_be_const.rs:48:1
5462
|
5563
LL | / fn sub(x: u32) -> usize {
5664
LL | | unsafe { transmute(&x) }
5765
LL | | }
5866
| |_^
5967

6068
error: this could be a `const fn`
61-
--> $DIR/could_be_const.rs:62:9
69+
--> $DIR/could_be_const.rs:67:9
6270
|
6371
LL | / pub fn b(self, a: &A) -> B {
6472
LL | | B
6573
LL | | }
6674
| |_________^
6775

68-
error: aborting due to 8 previous errors
76+
error: aborting due to 9 previous errors
6977

0 commit comments

Comments
 (0)
Please sign in to comment.