Skip to content

Commit 70c8010

Browse files
committed
[missing_const_for_fn]: fix FP when arg ty is impl trait alias ty
1 parent 0aac16e commit 70c8010

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

clippy_lints/src/missing_const_for_fn.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use rustc_hir::intravisit::FnKind;
88
use rustc_hir::{self as hir, Body, Constness, FnDecl, GenericParamKind};
99
use rustc_lint::{LateContext, LateLintPass};
1010
use rustc_middle::lint::in_external_macro;
11+
use rustc_middle::ty;
1112
use rustc_session::impl_lint_pass;
1213
use rustc_span::def_id::LocalDefId;
1314
use rustc_span::Span;
@@ -131,6 +132,10 @@ impl<'tcx> LateLintPass<'tcx> for MissingConstForFn {
131132
FnKind::Closure => return,
132133
}
133134

135+
if fn_inputs_has_impl_trait_ty(cx, def_id) {
136+
return;
137+
}
138+
134139
let hir_id = cx.tcx.local_def_id_to_hir_id(def_id);
135140

136141
// Const fns are not allowed as methods in a trait.
@@ -185,3 +190,17 @@ fn could_be_const_with_abi(cx: &LateContext<'_>, msrv: &Msrv, abi: Abi) -> bool
185190
_ => cx.tcx.features().const_extern_fn,
186191
}
187192
}
193+
194+
/// Return `true` when the given `def_id` is a function that has `impl Trait` ty as one of
195+
/// its parameter types.
196+
fn fn_inputs_has_impl_trait_ty(cx: &LateContext<'_>, def_id: LocalDefId) -> bool {
197+
let inputs = cx.tcx.fn_sig(def_id).instantiate_identity().inputs().skip_binder();
198+
inputs.iter().any(|input| {
199+
// NB: Other alias ty kind might missing default.
200+
// For example, an associate type alias declared as `type T: Fn();`
201+
// would cause ICE when `type_of` is called with it.
202+
matches!(
203+
input.kind(),
204+
ty::Alias(ty::AliasTyKind::Weak, alias_ty) if cx.tcx.type_of(alias_ty.def_id).skip_binder().is_impl_trait())
205+
})
206+
}

tests/ui/missing_const_for_fn/cant_be_const.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#![warn(clippy::missing_const_for_fn)]
99
#![feature(start)]
10+
#![feature(type_alias_impl_trait)]
1011

1112
extern crate helper;
1213
extern crate proc_macros;
@@ -190,3 +191,18 @@ mod with_extern {
190191
extern "system" fn system() {}
191192
extern "system-unwind" fn system_unwind() {}
192193
}
194+
195+
mod issue_13009 {
196+
type Foo = impl std::fmt::Debug;
197+
198+
fn foo(_: Foo) {
199+
let _: Foo = 1;
200+
}
201+
202+
// This does not related to the issue, but make sure this won't cause ICE.
203+
trait BarTrait {
204+
type Bar: std::fmt::Debug;
205+
206+
fn bar(_: Self::Bar);
207+
}
208+
}

0 commit comments

Comments
 (0)