Skip to content

Commit fb70d73

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 2f27c6e commit fb70d73

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
@@ -1716,6 +1716,54 @@ impl<'tcx> LateLintPass<'tcx> for ImproperCTypesLint {
17161716
self.check_foreign_fn(cx, CItemKind::ExportedFunction, id, decl);
17171717
}
17181718
}
1719+
1720+
fn check_trait_item(&mut self, cx: &LateContext<'tcx>, tr_it: &hir::TraitItem<'tcx>) {
1721+
match tr_it.kind {
1722+
hir::TraitItemKind::Const(hir_ty, _) => {
1723+
let ty = cx.tcx.type_of(hir_ty.hir_id.owner.def_id).instantiate_identity();
1724+
self.check_type_for_external_abi_fnptr(cx, hir_ty, ty);
1725+
}
1726+
hir::TraitItemKind::Fn(sig, trait_fn) => {
1727+
match trait_fn {
1728+
// if the method is defined here,
1729+
// there is a matching ``LateLintPass::check_fn`` call,
1730+
// let's not redo that work
1731+
hir::TraitFn::Provided(_) => return,
1732+
hir::TraitFn::Required(_) => (),
1733+
}
1734+
let local_id = tr_it.owner_id.def_id;
1735+
if sig.header.abi.is_rustic_abi() {
1736+
self.check_fn_for_external_abi_fnptr(cx, local_id, sig.decl);
1737+
} else {
1738+
self.check_foreign_fn(cx, CItemKind::ExportedFunction, local_id, sig.decl);
1739+
}
1740+
}
1741+
hir::TraitItemKind::Type(_, ty_maybe) => {
1742+
if let Some(hir_ty) = ty_maybe {
1743+
let ty = cx.tcx.type_of(hir_ty.hir_id.owner.def_id).instantiate_identity();
1744+
self.check_type_for_external_abi_fnptr(cx, hir_ty, ty);
1745+
}
1746+
}
1747+
}
1748+
}
1749+
fn check_impl_item(&mut self, cx: &LateContext<'tcx>, im_it: &hir::ImplItem<'tcx>) {
1750+
// note: we do not skip these checks eventhough they might generate dupe warnings because:
1751+
// - the corresponding trait might be in another crate
1752+
// - the corresponding trait might have some templating involved, so only the impl has the full type information
1753+
match im_it.kind {
1754+
hir::ImplItemKind::Type(hir_ty) => {
1755+
let ty = cx.tcx.type_of(hir_ty.hir_id.owner.def_id).instantiate_identity();
1756+
self.check_type_for_external_abi_fnptr(cx, hir_ty, ty);
1757+
}
1758+
hir::ImplItemKind::Fn(_sig, _) => {
1759+
// see ``LateLintPass::check_fn``
1760+
}
1761+
hir::ImplItemKind::Const(hir_ty, _) => {
1762+
let ty = cx.tcx.type_of(hir_ty.hir_id.owner.def_id).instantiate_identity();
1763+
self.check_type_for_external_abi_fnptr(cx, hir_ty, ty);
1764+
}
1765+
}
1766+
}
17191767
}
17201768

17211769
declare_lint! {

0 commit comments

Comments
 (0)