about summary refs log tree commit diff
diff options
context:
space:
mode:
authorRicho Healey <richo@psych0tik.net>2015-01-21 12:07:03 -0800
committerRicho Healey <richo@psych0tik.net>2015-01-29 21:32:00 -0800
commit44ff72195e0a5a3b0bfd4b8b3cef919cfa9661ef (patch)
treeaf5080857a005797ea6e5d0e22cddc5d5db9319b
parent52c74e63dacd49017b19330e0cbecbac0a3fe62e (diff)
downloadrust-44ff72195e0a5a3b0bfd4b8b3cef919cfa9661ef.tar.gz
rust-44ff72195e0a5a3b0bfd4b8b3cef919cfa9661ef.zip
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.
-rw-r--r--src/librustc/lint/builtin.rs29
-rw-r--r--src/librustc/lint/context.rs1
2 files changed, 30 insertions, 0 deletions
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,