Skip to content

Commit 43ce0a9

Browse files
committed
Update and fix clippy tests
1 parent b86620d commit 43ce0a9

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
lines changed

clippy_lints/src/missing_const_for_fn.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use clippy_utils::qualify_min_const_fn::is_min_const_fn;
33
use clippy_utils::ty::has_drop;
44
use clippy_utils::{fn_has_unsatisfiable_preds, is_entrypoint_fn, meets_msrv, msrvs, trait_ref_of_method};
55
use rustc_hir as hir;
6+
use rustc_hir::def_id::CRATE_DEF_ID;
67
use rustc_hir::intravisit::FnKind;
78
use rustc_hir::{Body, Constness, FnDecl, GenericParamKind, HirId};
89
use rustc_lint::{LateContext, LateLintPass};
@@ -131,6 +132,18 @@ impl<'tcx> LateLintPass<'tcx> for MissingConstForFn {
131132
FnKind::Closure => return,
132133
}
133134

135+
// Const fns are not allowed as methods in a trait.
136+
{
137+
let parent = cx.tcx.hir().get_parent_item(hir_id);
138+
if parent != CRATE_DEF_ID {
139+
if let hir::Node::Item(item) = cx.tcx.hir().get_by_def_id(parent) {
140+
if let hir::ItemKind::Trait(..) = &item.kind {
141+
return;
142+
}
143+
}
144+
}
145+
}
146+
134147
let mir = cx.tcx.optimized_mir(def_id);
135148

136149
if let Err((span, err)) = is_min_const_fn(cx.tcx, mir, self.msrv.as_ref()) {

tests/ui/missing_const_for_fn/could_be_const.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,6 @@ fn sub(x: u32) -> usize {
4949
unsafe { transmute(&x) }
5050
}
5151

52-
// NOTE: This is currently not yet allowed to be const
53-
// Once implemented, Clippy should be able to suggest this as const, too.
5452
fn generic_arr<T: Copy>(t: [T; 1]) -> T {
5553
t[0]
5654
}

tests/ui/missing_const_for_fn/could_be_const.stderr

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,20 +58,28 @@ LL | | }
5858
| |_^
5959

6060
error: this could be a `const fn`
61-
--> $DIR/could_be_const.rs:67:9
61+
--> $DIR/could_be_const.rs:52:1
62+
|
63+
LL | / fn generic_arr<T: Copy>(t: [T; 1]) -> T {
64+
LL | | t[0]
65+
LL | | }
66+
| |_^
67+
68+
error: this could be a `const fn`
69+
--> $DIR/could_be_const.rs:65:9
6270
|
6371
LL | / pub fn b(self, a: &A) -> B {
6472
LL | | B
6573
LL | | }
6674
| |_________^
6775

6876
error: this could be a `const fn`
69-
--> $DIR/could_be_const.rs:77:5
77+
--> $DIR/could_be_const.rs:75:5
7078
|
7179
LL | / fn const_fn_stabilized_before_msrv(byte: u8) {
7280
LL | | byte.is_ascii_digit();
7381
LL | | }
7482
| |_____^
7583

76-
error: aborting due to 9 previous errors
84+
error: aborting due to 10 previous errors
7785

0 commit comments

Comments
 (0)