How to get or view the count of unsafe block in a project or library in

Is there any way to get or view the the number of unsafe block in a project or in a rust library ?
Is the any Commands for this?

i feel it will be nice if show the number of unsafe block in each librrary

There's cargo-geiger, but in my opinion this isn't as useful as you'd think.

If you want to inspect the unsafe usage in a particular source tree, just grep for it.


If you are on a Unix system, grep -r -E "unsafe\s?\{" | wc -l should give you a decent estimate.

You can also just piggyback on the compiler for a 100% accurate detection of unsafe blocks:

  • Clone the project locally
  • Add #![forbid(unsafe_code)] at the top of temporarily
  • cargo build will now give you errors for every single use of unsafe, including unsafe impls and other things which a simple grep invocation might not detect.
1 Like

You're probably making a dangerous assumption that absence of unsafe means safety. There are many dangerous constructs that are technically "safe" in Rust. There are also ways of running unsafe code without the unsafe block, e.g. via no_mangle or linker directives.

You should check out cargo-crev — Rust/Cargo add-on // and cargo-audit — Rust/Cargo add-on // if you're worried about safety of dependencies.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.