Are there any tools to count calls to panic or unsafe?

I would like to know that I cleaned up all of the calls to panic!(), unwrap(), expect(), etc... in my code.

Failing that, I would like to know that I have reviewed and annotated such calls saying, "Yes, this is allowed here".

Same question for unsafe.

Will clippy do this for me?

Do folks do this sort of thing when they review Rust code?


Clippy has warnings you can turn on for using them.

Do folks generally do that? What are a good set of warnings to enable for those sorts of checks?


Not really. Instead, I'll read a bunch of code to get a feel for it, then let my intuition tell me whether those unwrap() calls were justified or the author is being sloppy.

When writing code, I also prefer expect() over unwrap() because it forces me to write a message explaining why I believe the code can never panic. That's often enough to act as a mental check, similar to an assertion, and if we are troubleshooting a panic later on it points to where the broken assumptions may be. For me, it's often a trade-off between writing correct/resilient code and getting things done.

I also take the belts-and-braces approach. It's infeasible to prevent all possible panics, so design your wider system to take this into account and make sure one program panicking doesn't bring down your entire application (e.g. tell systemd or Kubernetes to automatically restart your program if it crashes).


You may be interested in:

and the built-in lint unsafe_code.


There is also Geiger for unsafe code.

For panics, I am using combination of lints:


Have tried to use libraries like assert_panic_free - Rust but they're super fragile. And then there are tools like

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.