Skip to content

Commit 44ff721

Browse files
committed
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.
1 parent 52c74e6 commit 44ff721

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

src/librustc/lint/builtin.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2036,6 +2036,35 @@ impl LintPass for HardwiredLints {
20362036
}
20372037
}
20382038

2039+
declare_lint! {
2040+
PRIVATE_NO_MANGLE_FNS,
2041+
Warn,
2042+
"functions marked #[no_mangle] should be exported"
2043+
}
2044+
2045+
#[derive(Copy)]
2046+
pub struct PrivateNoMangleFns;
2047+
2048+
impl LintPass for PrivateNoMangleFns {
2049+
fn get_lints(&self) -> LintArray {
2050+
lint_array!(PRIVATE_NO_MANGLE_FNS)
2051+
}
2052+
2053+
fn check_item(&mut self, cx: &Context, it: &ast::Item) {
2054+
match it.node {
2055+
ast::ItemFn(..) => {
2056+
if attr::contains_name(it.attrs.as_slice(), "no_mangle") &&
2057+
!cx.exported_items.contains(&it.id) {
2058+
let msg = format!("function {} is marked #[no_mangle], but not exported",
2059+
it.ident);
2060+
cx.span_lint(PRIVATE_NO_MANGLE_FNS, it.span, msg.as_slice());
2061+
}
2062+
},
2063+
_ => {},
2064+
}
2065+
}
2066+
}
2067+
20392068
/// Forbids using the `#[feature(...)]` attribute
20402069
#[derive(Copy)]
20412070
pub struct UnstableFeatures;

src/librustc/lint/context.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ impl LintStore {
213213
UnstableFeatures,
214214
Stability,
215215
UnconditionalRecursion,
216+
PrivateNoMangleFns,
216217
);
217218

218219
add_builtin_with_new!(sess,

0 commit comments

Comments
 (0)