Skip to content

Commit 2227422

Browse files
committed
lint: refactor to make logic a bit cleaner
Signed-off-by: David Wood <[email protected]>
1 parent f1e2879 commit 2227422

File tree

1 file changed

+27
-29
lines changed

1 file changed

+27
-29
lines changed

compiler/rustc_lint/src/types.rs

+27-29
Original file line numberDiff line numberDiff line change
@@ -1381,43 +1381,36 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
13811381

13821382
/// Check if a function's argument types and result type are "ffi-safe".
13831383
///
1384-
/// Argument types and the result type are checked for functions with external ABIs.
1385-
/// For functions with internal ABIs, argument types and the result type are walked to find
1386-
/// fn-ptr types that have external ABIs, as these still need checked.
1387-
fn check_maybe_foreign_fn(
1388-
&mut self,
1389-
abi: SpecAbi,
1390-
def_id: LocalDefId,
1391-
decl: &'tcx hir::FnDecl<'_>,
1392-
) {
1384+
/// For a external ABI function, argument types and the result type are walked to find fn-ptr
1385+
/// types that have external ABIs, as these still need checked.
1386+
fn check_fn(&mut self, def_id: LocalDefId, decl: &'tcx hir::FnDecl<'_>) {
13931387
let sig = self.cx.tcx.fn_sig(def_id).subst_identity();
13941388
let sig = self.cx.tcx.erase_late_bound_regions(sig);
13951389

1396-
let is_internal_abi = self.is_internal_abi(abi);
1397-
let check_ty = |this: &mut ImproperCTypesVisitor<'a, 'tcx>,
1398-
hir_ty: &'tcx hir::Ty<'_>,
1399-
ty: Ty<'tcx>,
1400-
is_return_type: bool| {
1401-
// If this function has an external ABI, then its arguments and return type should be
1402-
// checked..
1403-
if !is_internal_abi {
1404-
this.check_type_for_ffi_and_report_errors(hir_ty.span, ty, false, is_return_type);
1405-
return;
1390+
for (input_ty, input_hir) in iter::zip(sig.inputs(), decl.inputs) {
1391+
for (fn_ptr_ty, span) in self.find_fn_ptr_ty_with_external_abi(input_hir, *input_ty) {
1392+
self.check_type_for_ffi_and_report_errors(span, fn_ptr_ty, false, false);
14061393
}
1394+
}
14071395

1408-
// ..but if this function has an internal ABI, then search the argument or return type
1409-
// for any fn-ptr types with external ABI, which should be checked..
1410-
for (fn_ptr_ty, span) in this.find_fn_ptr_ty_with_external_abi(hir_ty, ty) {
1411-
this.check_type_for_ffi_and_report_errors(span, fn_ptr_ty, false, is_return_type);
1396+
if let hir::FnRetTy::Return(ref ret_hir) = decl.output {
1397+
for (fn_ptr_ty, span) in self.find_fn_ptr_ty_with_external_abi(ret_hir, sig.output()) {
1398+
self.check_type_for_ffi_and_report_errors(span, fn_ptr_ty, false, true);
14121399
}
1413-
};
1400+
}
1401+
}
1402+
1403+
/// Check if a function's argument types and result type are "ffi-safe".
1404+
fn check_foreign_fn(&mut self, def_id: LocalDefId, decl: &'tcx hir::FnDecl<'_>) {
1405+
let sig = self.cx.tcx.fn_sig(def_id).subst_identity();
1406+
let sig = self.cx.tcx.erase_late_bound_regions(sig);
14141407

14151408
for (input_ty, input_hir) in iter::zip(sig.inputs(), decl.inputs) {
1416-
check_ty(self, input_hir, *input_ty, false);
1409+
self.check_type_for_ffi_and_report_errors(input_hir.span, *input_ty, false, false);
14171410
}
14181411

14191412
if let hir::FnRetTy::Return(ref ret_hir) = decl.output {
1420-
check_ty(self, ret_hir, sig.output(), true);
1413+
self.check_type_for_ffi_and_report_errors(ret_hir.span, sig.output(), false, true);
14211414
}
14221415
}
14231416

@@ -1486,12 +1479,13 @@ impl<'tcx> LateLintPass<'tcx> for ImproperCTypesDeclarations {
14861479
let abi = cx.tcx.hir().get_foreign_abi(it.hir_id());
14871480

14881481
match it.kind {
1489-
hir::ForeignItemKind::Fn(ref decl, _, _) => {
1490-
vis.check_maybe_foreign_fn(abi, it.owner_id.def_id, decl);
1482+
hir::ForeignItemKind::Fn(ref decl, _, _) if !vis.is_internal_abi(abi) => {
1483+
vis.check_foreign_fn(it.owner_id.def_id, decl);
14911484
}
14921485
hir::ForeignItemKind::Static(ref ty, _) if !vis.is_internal_abi(abi) => {
14931486
vis.check_foreign_static(it.owner_id, ty.span);
14941487
}
1488+
hir::ForeignItemKind::Fn(ref decl, _, _) => vis.check_fn(it.owner_id.def_id, decl),
14951489
hir::ForeignItemKind::Static(..) | hir::ForeignItemKind::Type => (),
14961490
}
14971491
}
@@ -1574,7 +1568,11 @@ impl<'tcx> LateLintPass<'tcx> for ImproperCTypesDefinitions {
15741568
};
15751569

15761570
let mut vis = ImproperCTypesVisitor { cx, mode: CItemKind::Definition };
1577-
vis.check_maybe_foreign_fn(abi, id, decl);
1571+
if vis.is_internal_abi(abi) {
1572+
vis.check_fn(id, decl);
1573+
} else {
1574+
vis.check_foreign_fn(id, decl);
1575+
}
15781576
}
15791577
}
15801578

0 commit comments

Comments
 (0)