Skip to content

Commit

Permalink
Merge pull request #173 from qrilka/104-mark-no_mangle-as-unsafe
Browse files Browse the repository at this point in the history
Mark no_mangle functions as unsafe
  • Loading branch information
anderejd authored Jan 19, 2021
2 parents df45256 + 87aeef3 commit 5bcba4f
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 18 deletions.
26 changes: 18 additions & 8 deletions geiger/src/find.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,16 @@ pub fn g() {
}.as_bytes()).unwrap();
}
#[no_mangle]
pub fn h() {
unimplemented!()
}
#[export_name = \"exported_g\"]
pub fn g() {
unimplemented!()
}
#[cfg(test)]
mod tests {
use super::*;
Expand All @@ -76,11 +86,11 @@ mod tests {
counters: CounterBlock {
functions: Count {
safe: 2,
unsafe_: 1
unsafe_: 3
},
exprs: Count {
safe: 4,
unsafe_: 3
unsafe_: 5
},
item_impls: Count {
safe: 0,
Expand All @@ -104,11 +114,11 @@ mod tests {
counters: CounterBlock {
functions: Count {
safe: 1,
unsafe_: 1
unsafe_: 3
},
exprs: Count {
safe: 4,
unsafe_: 2
unsafe_: 4
},
item_impls: Count {
safe: 0,
Expand Down Expand Up @@ -156,11 +166,11 @@ mod tests {
counters: CounterBlock {
functions: Count {
safe: 2,
unsafe_: 1
unsafe_: 3
},
exprs: Count {
safe: 4,
unsafe_: 3
unsafe_: 5
},
item_impls: Count {
safe: 0,
Expand All @@ -184,11 +194,11 @@ mod tests {
counters: CounterBlock {
functions: Count {
safe: 1,
unsafe_: 1
unsafe_: 3
},
exprs: Count {
safe: 4,
unsafe_: 2
unsafe_: 4
},
item_impls: Count {
safe: 0,
Expand Down
12 changes: 6 additions & 6 deletions geiger/src/geiger_syn_visitor.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::{
file_forbids_unsafe, is_test_fn, is_test_mod, IncludeTests, RsFileMetrics,
file_forbids_unsafe, has_unsafe_attributes, is_test_fn, is_test_mod,
IncludeTests, RsFileMetrics,
};

use syn::{visit, Expr, ImplItemMethod, ItemFn, ItemImpl, ItemMod, ItemTrait};
Expand Down Expand Up @@ -50,13 +51,12 @@ impl<'ast> visit::Visit<'ast> for GeigerSynVisitor {
if IncludeTests::No == self.include_tests && is_test_fn(item_fn) {
return;
}
if item_fn.sig.unsafety.is_some() {
let unsafe_fn =
item_fn.sig.unsafety.is_some() || has_unsafe_attributes(item_fn);
if unsafe_fn {
self.enter_unsafe_scope()
}
self.metrics
.counters
.functions
.count(item_fn.sig.unsafety.is_some());
self.metrics.counters.functions.count(unsafe_fn);
visit::visit_item_fn(self, item_fn);
if item_fn.sig.unsafety.is_some() {
self.exit_unsafe_scope()
Expand Down
28 changes: 24 additions & 4 deletions geiger/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,19 @@ fn is_test_fn(item_fn: &ItemFn) -> bool {
.attrs
.iter()
.flat_map(Attribute::parse_meta)
.any(|m| meta_is_word_test(&m))
.any(|m| meta_contains_ident(&m, "test"))
}

fn has_unsafe_attributes(item_fn: &ItemFn) -> bool {
use syn::Attribute;
item_fn
.attrs
.iter()
.flat_map(Attribute::parse_meta)
.any(|m| {
meta_contains_ident(&m, "no_mangle")
|| meta_contains_attribute(&m, "export_name")
})
}

/// Will return true for #[cfg(test)] decorated modules.
Expand All @@ -110,10 +122,18 @@ fn is_test_mod(i: &ItemMod) -> bool {
})
}

fn meta_is_word_test(m: &syn::Meta) -> bool {
fn meta_contains_ident(m: &syn::Meta, ident: &str) -> bool {
use syn::Meta;
match m {
Meta::Path(p) => p.is_ident(ident),
_ => false,
}
}

fn meta_contains_attribute(m: &syn::Meta, ident: &str) -> bool {
use syn::Meta;
match m {
Meta::Path(p) => p.is_ident("test"),
Meta::NameValue(nv) => nv.path.is_ident(ident),
_ => false,
}
}
Expand All @@ -139,7 +159,7 @@ fn meta_list_is_cfg_test(meta_list: &syn::MetaList) -> bool {
return false;
}
meta_list.nested.iter().any(|n| match n {
NestedMeta::Meta(meta) => meta_is_word_test(meta),
NestedMeta::Meta(meta) => meta_contains_ident(meta, "test"),
_ => false,
})
}

0 comments on commit 5bcba4f

Please sign in to comment.