From 30d06a810c8b8967d9d0ea2cb64d5adf8b357914 Mon Sep 17 00:00:00 2001 From: Milo Moisson Date: Sun, 23 Jul 2023 17:05:54 +0200 Subject: [PATCH 1/2] ptr_arg should ignore extern functions --- clippy_lints/src/ptr.rs | 11 +++++++++++ tests/ui/ptr_arg.rs | 13 +++++++++++++ 2 files changed, 24 insertions(+) diff --git a/clippy_lints/src/ptr.rs b/clippy_lints/src/ptr.rs index 82a55166aeae..4534b2ab28af 100644 --- a/clippy_lints/src/ptr.rs +++ b/clippy_lints/src/ptr.rs @@ -26,6 +26,7 @@ use rustc_session::{declare_lint_pass, declare_tool_lint}; use rustc_span::source_map::Span; use rustc_span::sym; use rustc_span::symbol::Symbol; +use rustc_target::spec::abi::Abi; use rustc_trait_selection::infer::InferCtxtExt as _; use rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt as _; use std::{fmt, iter}; @@ -162,6 +163,11 @@ impl<'tcx> LateLintPass<'tcx> for Ptr { return; } + if !matches!(sig.header.abi, Abi::Rust) { + // Ignore `extern` functions with non-Rust calling conventions + return; + } + check_mut_from_ref(cx, sig, None); for arg in check_fn_args( cx, @@ -217,6 +223,11 @@ impl<'tcx> LateLintPass<'tcx> for Ptr { _ => return, }; + if !matches!(sig.header.abi, Abi::Rust) { + // Ignore `extern` functions with non-Rust calling conventions + return; + } + check_mut_from_ref(cx, sig, Some(body)); let decl = sig.decl; let sig = cx.tcx.fn_sig(item_id).subst_identity().skip_binder(); diff --git a/tests/ui/ptr_arg.rs b/tests/ui/ptr_arg.rs index 13e993d247b2..08075c382a22 100644 --- a/tests/ui/ptr_arg.rs +++ b/tests/ui/ptr_arg.rs @@ -267,3 +267,16 @@ mod issue_9218 { todo!() } } + +mod issue_11181 { + extern "C" fn allowed(_v: &Vec) {} + + struct S; + impl S { + extern "C" fn allowed(_v: &Vec) {} + } + + trait T { + extern "C" fn allowed(_v: &Vec) {} + } +} From 7b8598d6c0c28c4e60387f18fae8d5b021ce5907 Mon Sep 17 00:00:00 2001 From: Milo Moisson Date: Mon, 24 Jul 2023 01:23:35 +0200 Subject: [PATCH 2/2] ptr lint: check_mut_from_ref is checked independently of the function's ABI --- clippy_lints/src/ptr.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/clippy_lints/src/ptr.rs b/clippy_lints/src/ptr.rs index 4534b2ab28af..d32e58c1af90 100644 --- a/clippy_lints/src/ptr.rs +++ b/clippy_lints/src/ptr.rs @@ -163,12 +163,13 @@ impl<'tcx> LateLintPass<'tcx> for Ptr { return; } + check_mut_from_ref(cx, sig, None); + if !matches!(sig.header.abi, Abi::Rust) { // Ignore `extern` functions with non-Rust calling conventions return; } - check_mut_from_ref(cx, sig, None); for arg in check_fn_args( cx, cx.tcx.fn_sig(item.owner_id).subst_identity().skip_binder().inputs(), @@ -223,12 +224,13 @@ impl<'tcx> LateLintPass<'tcx> for Ptr { _ => return, }; + check_mut_from_ref(cx, sig, Some(body)); + if !matches!(sig.header.abi, Abi::Rust) { // Ignore `extern` functions with non-Rust calling conventions return; } - check_mut_from_ref(cx, sig, Some(body)); let decl = sig.decl; let sig = cx.tcx.fn_sig(item_id).subst_identity().skip_binder(); let lint_args: Vec<_> = check_fn_args(cx, sig.inputs(), decl.inputs, &decl.output, body.params)