Skip to content

Commit

Permalink
Mark no_mangle functions as unsafe
Browse files Browse the repository at this point in the history
Resolves #104
  • Loading branch information
qrilka committed Jan 14, 2021
1 parent df45256 commit e0c4d9c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 15 deletions.
21 changes: 13 additions & 8 deletions geiger/src/find.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ pub fn g() {
}.as_bytes()).unwrap();
}
#[no_mangle]
pub fn h() {
unimplemented!()
}
#[cfg(test)]
mod tests {
use super::*;
Expand All @@ -76,11 +81,11 @@ mod tests {
counters: CounterBlock {
functions: Count {
safe: 2,
unsafe_: 1
unsafe_: 2
},
exprs: Count {
safe: 4,
unsafe_: 3
unsafe_: 4
},
item_impls: Count {
safe: 0,
Expand All @@ -104,11 +109,11 @@ mod tests {
counters: CounterBlock {
functions: Count {
safe: 1,
unsafe_: 1
unsafe_: 2
},
exprs: Count {
safe: 4,
unsafe_: 2
unsafe_: 3
},
item_impls: Count {
safe: 0,
Expand Down Expand Up @@ -156,11 +161,11 @@ mod tests {
counters: CounterBlock {
functions: Count {
safe: 2,
unsafe_: 1
unsafe_: 2
},
exprs: Count {
safe: 4,
unsafe_: 3
unsafe_: 4
},
item_impls: Count {
safe: 0,
Expand All @@ -184,11 +189,11 @@ mod tests {
counters: CounterBlock {
functions: Count {
safe: 1,
unsafe_: 1
unsafe_: 2
},
exprs: Count {
safe: 4,
unsafe_: 2
unsafe_: 3
},
item_impls: Count {
safe: 0,
Expand Down
7 changes: 4 additions & 3 deletions geiger/src/geiger_syn_visitor.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::{
file_forbids_unsafe, is_test_fn, is_test_mod, IncludeTests, RsFileMetrics,
file_forbids_unsafe, is_test_fn, is_test_mod, has_unsafe_attributes, IncludeTests, RsFileMetrics,
};

use syn::{visit, Expr, ImplItemMethod, ItemFn, ItemImpl, ItemMod, ItemTrait};
Expand Down Expand Up @@ -50,13 +50,14 @@ 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());
.count(unsafe_fn);
visit::visit_item_fn(self, item_fn);
if item_fn.sig.unsafety.is_some() {
self.exit_unsafe_scope()
Expand Down
17 changes: 13 additions & 4 deletions geiger/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,16 @@ 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"))
}

/// Will return true for #[cfg(test)] decorated modules.
Expand All @@ -110,10 +119,10 @@ 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("test"),
Meta::Path(p) => p.is_ident(ident),
_ => false,
}
}
Expand All @@ -139,7 +148,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 e0c4d9c

Please sign in to comment.