From 44ff72195e0a5a3b0bfd4b8b3cef919cfa9661ef Mon Sep 17 00:00:00 2001 From: Richo Healey Date: Wed, 21 Jan 2015 12:07:03 -0800 Subject: [PATCH] lint: warn about #[no_mangle] fns that aren't exported The usecase is that functions made visible to systems outside of the rust ecosystem require the symbol to be visible. --- src/librustc/lint/builtin.rs | 29 +++++++++++++++++++++++++++++ src/librustc/lint/context.rs | 1 + 2 files changed, 30 insertions(+) diff --git a/src/librustc/lint/builtin.rs b/src/librustc/lint/builtin.rs index f13814527cd..bef9e6be8d9 100644 --- a/src/librustc/lint/builtin.rs +++ b/src/librustc/lint/builtin.rs @@ -2036,6 +2036,35 @@ impl LintPass for HardwiredLints { } } +declare_lint! { + PRIVATE_NO_MANGLE_FNS, + Warn, + "functions marked #[no_mangle] should be exported" +} + +#[derive(Copy)] +pub struct PrivateNoMangleFns; + +impl LintPass for PrivateNoMangleFns { + fn get_lints(&self) -> LintArray { + lint_array!(PRIVATE_NO_MANGLE_FNS) + } + + fn check_item(&mut self, cx: &Context, it: &ast::Item) { + match it.node { + ast::ItemFn(..) => { + if attr::contains_name(it.attrs.as_slice(), "no_mangle") && + !cx.exported_items.contains(&it.id) { + let msg = format!("function {} is marked #[no_mangle], but not exported", + it.ident); + cx.span_lint(PRIVATE_NO_MANGLE_FNS, it.span, msg.as_slice()); + } + }, + _ => {}, + } + } +} + /// Forbids using the `#[feature(...)]` attribute #[derive(Copy)] pub struct UnstableFeatures; diff --git a/src/librustc/lint/context.rs b/src/librustc/lint/context.rs index f57d7956edf..76f874f2428 100644 --- a/src/librustc/lint/context.rs +++ b/src/librustc/lint/context.rs @@ -213,6 +213,7 @@ impl LintStore { UnstableFeatures, Stability, UnconditionalRecursion, + PrivateNoMangleFns, ); add_builtin_with_new!(sess,