Skip to content

Commit 72243f4

Browse files
authored
Fix missing_inline_in_public_items FP on functions with extern (rust-lang#15313)
Closes rust-lang/rust-clippy#15301 changelog: [`missing_inline_in_public_items`] fix FP on functions with `extern`
2 parents fd7076b + 75811df commit 72243f4

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

clippy_lints/src/missing_inline.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use clippy_utils::diagnostics::span_lint;
22
use rustc_attr_data_structures::{AttributeKind, find_attr};
3-
use rustc_hir as hir;
4-
use rustc_hir::Attribute;
3+
use rustc_hir::def_id::DefId;
4+
use rustc_hir::{self as hir, Attribute};
55
use rustc_lint::{LateContext, LateLintPass, LintContext};
66
use rustc_middle::ty::AssocItemContainer;
77
use rustc_session::declare_lint_pass;
@@ -97,6 +97,10 @@ impl<'tcx> LateLintPass<'tcx> for MissingInline {
9797
}
9898
match it.kind {
9999
hir::ItemKind::Fn { .. } => {
100+
if fn_is_externally_exported(cx, it.owner_id.to_def_id()) {
101+
return;
102+
}
103+
100104
let desc = "a function";
101105
let attrs = cx.tcx.hir_attrs(it.hir_id());
102106
check_missing_inline_attrs(cx, attrs, it.span, desc);
@@ -173,3 +177,10 @@ impl<'tcx> LateLintPass<'tcx> for MissingInline {
173177
check_missing_inline_attrs(cx, attrs, impl_item.span, desc);
174178
}
175179
}
180+
181+
/// Checks if this function is externally exported, where #[inline] wouldn't have the desired effect
182+
/// and a rustc warning would be triggered, see #15301
183+
fn fn_is_externally_exported(cx: &LateContext<'_>, def_id: DefId) -> bool {
184+
let attrs = cx.tcx.codegen_fn_attrs(def_id);
185+
attrs.contains_extern_indicator()
186+
}

tests/ui/missing_inline.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,20 @@ impl PubFoo {
8080
// do not lint this since users cannot control the external code
8181
#[derive(Debug)]
8282
pub struct S;
83+
84+
pub mod issue15301 {
85+
#[unsafe(no_mangle)]
86+
pub extern "C" fn call_from_c() {
87+
println!("Just called a Rust function from C!");
88+
}
89+
90+
#[unsafe(no_mangle)]
91+
pub extern "Rust" fn call_from_rust() {
92+
println!("Just called a Rust function from Rust!");
93+
}
94+
95+
#[unsafe(no_mangle)]
96+
pub fn call_from_rust_no_extern() {
97+
println!("Just called a Rust function from Rust!");
98+
}
99+
}

0 commit comments

Comments
 (0)