Skip to content

Commit e59850a

Browse files
committed
ImproperCTypes: also check in traits
Add new areas that are checked by ImproperCTypes lints: Function declarations(*) and definitions in traits and impls *) from the perspective of an FFI boundary, those are actually definitions
1 parent e7db8b8 commit e59850a

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

compiler/rustc_lint/src/types/improper_ctypes.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1947,4 +1947,52 @@ impl<'tcx> LateLintPass<'tcx> for ImproperCTypesLint {
19471947
self.check_foreign_fn(cx, CItemKind::ExportedFunction, id, decl);
19481948
}
19491949
}
1950+
1951+
fn check_trait_item(&mut self, cx: &LateContext<'tcx>, tr_it: &hir::TraitItem<'tcx>) {
1952+
match tr_it.kind {
1953+
hir::TraitItemKind::Const(hir_ty, _) => {
1954+
let ty = cx.tcx.type_of(hir_ty.hir_id.owner.def_id).instantiate_identity();
1955+
self.check_type_for_external_abi_fnptr(cx, hir_ty, ty);
1956+
}
1957+
hir::TraitItemKind::Fn(sig, trait_fn) => {
1958+
match trait_fn {
1959+
// if the method is defined here,
1960+
// there is a matching ``LateLintPass::check_fn`` call,
1961+
// let's not redo that work
1962+
hir::TraitFn::Provided(_) => return,
1963+
hir::TraitFn::Required(_) => (),
1964+
}
1965+
let local_id = tr_it.owner_id.def_id;
1966+
if sig.header.abi.is_rustic_abi() {
1967+
self.check_fn_for_external_abi_fnptr(cx, local_id, sig.decl);
1968+
} else {
1969+
self.check_foreign_fn(cx, CItemKind::ExportedFunction, local_id, sig.decl);
1970+
}
1971+
}
1972+
hir::TraitItemKind::Type(_, ty_maybe) => {
1973+
if let Some(hir_ty) = ty_maybe {
1974+
let ty = cx.tcx.type_of(hir_ty.hir_id.owner.def_id).instantiate_identity();
1975+
self.check_type_for_external_abi_fnptr(cx, hir_ty, ty);
1976+
}
1977+
}
1978+
}
1979+
}
1980+
fn check_impl_item(&mut self, cx: &LateContext<'tcx>, im_it: &hir::ImplItem<'tcx>) {
1981+
// note: we do not skip these checks eventhough they might generate dupe warnings because:
1982+
// - the corresponding trait might be in another crate
1983+
// - the corresponding trait might have some templating involved, so only the impl has the full type information
1984+
match im_it.kind {
1985+
hir::ImplItemKind::Type(hir_ty) => {
1986+
let ty = cx.tcx.type_of(hir_ty.hir_id.owner.def_id).instantiate_identity();
1987+
self.check_type_for_external_abi_fnptr(cx, hir_ty, ty);
1988+
}
1989+
hir::ImplItemKind::Fn(_sig, _) => {
1990+
// see ``LateLintPass::check_fn``
1991+
}
1992+
hir::ImplItemKind::Const(hir_ty, _) => {
1993+
let ty = cx.tcx.type_of(hir_ty.hir_id.owner.def_id).instantiate_identity();
1994+
self.check_type_for_external_abi_fnptr(cx, hir_ty, ty);
1995+
}
1996+
}
1997+
}
19501998
}

0 commit comments

Comments
 (0)