]> git.lizzy.rs Git - rust.git/commitdiff
clippy_lints: Do not warn against Box parameter in C FFI
authorJoão Paulo Taylor Ienczak Zanette <jpaulotiz@gmail.com>
Thu, 8 Oct 2020 00:41:54 +0000 (21:41 -0300)
committerJoão Paulo Taylor Ienczak Zanette <jpaulotiz@gmail.com>
Thu, 8 Oct 2020 00:41:54 +0000 (21:41 -0300)
Fixes #5542.

When using C FFI, to handle pointers in parameters it is needed to
declare them as `Box` in its Rust-side signature. However, the current
linter warns against the usage of Box stating that "local variable
doesn't need to be boxed here".

This commit fixes it by ignoring functions whose Abi is Cdecl.

clippy_lints/src/escape.rs

index 82549c12d0a207c31b839bcc726dba37d9e61e07..ffe81b3966c7c71951651397660ee341527954a9 100644 (file)
@@ -6,6 +6,7 @@
 use rustc_session::{declare_tool_lint, impl_lint_pass};
 use rustc_span::source_map::Span;
 use rustc_target::abi::LayoutOf;
+use rustc_target::spec::abi::Abi;
 use rustc_typeck::expr_use_visitor::{ConsumeMode, Delegate, ExprUseVisitor, PlaceBase, PlaceWithHirId};
 
 use crate::utils::span_lint;
@@ -60,12 +61,18 @@ impl<'tcx> LateLintPass<'tcx> for BoxedLocal {
     fn check_fn(
         &mut self,
         cx: &LateContext<'tcx>,
-        _: intravisit::FnKind<'tcx>,
+        fn_kind: intravisit::FnKind<'tcx>,
         _: &'tcx FnDecl<'_>,
         body: &'tcx Body<'_>,
         _: Span,
         hir_id: HirId,
     ) {
+        if let Some(header) = fn_kind.header() {
+            if header.abi == Abi::Cdecl {
+                return;
+            }
+        }
+
         // If the method is an impl for a trait, don't warn.
         let parent_id = cx.tcx.hir().get_parent_item(hir_id);
         let parent_node = cx.tcx.hir().find(parent_id);