-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Discuss use of unwrap #145
Comments
unwrap is perfectly fine if there is a check beforehand. There are many safety-related systems where an abort or panic is perfectly safe (though uncomfortable). If panicing is unsafe, you could still use unwrap with a check that unwrap actually does not panic. For my spare time projects (not safety-related at all), I typically put a comment to an unrwap explaining why it is not panicing. |
In a safety critical software, the loss of a thread through a panic due to not checking before doing an unwrap, could be catastrophic to that safety critical software component as a whole. |
There are good use-cases for using unwrap(). See e.g. the first example in https://doc.rust-lang.org/book/ch09-03-to-panic-or-not-to-panic.html for one. More generalized: I don't see, e.g., a problem with unwrapping static/const expressions. |
While somewhat tangential, I found the article by Niko on Unwind considered harmful? and the discussion around the article quite interesting. It would appear even in mission-critical (not safety-critical) applications that sometimes it's best to have another warmed process in the same state to fail over to if truly in a challenging to recover state. From that point of view, I think |
I think that we need rules for all situations where panics could happen because it forces developers to think whether a panic is the appropriate thing to do in a particular situation. If panicking is (in that context and situation) safe, it's absolute legit to do so, and sometimes, when facing a locally unrecoverable error, it's actually often a good thing to do to stay in a safe state. As already mentioned, we should not just cover
|
@darkwisebear -- That seems like reasonable high-level choose-your-own-adventure style guidance. If fleshed out this could make for a useful contribution to the coding guidelines subcommittee. |
Generally in safety critical systems that I've worked on, there is a 2nd process ready to pick up the pieces and recover if the primary application fails. We may have potential unrecoverable situations for the primary application which force the secondary application to take action. But in many cases they are recoverable and it is usually not acceptable to have the safety critical software crash/exit where it could of continued and met all it's safety requirements. However while all this is useful for how to design safety critical software, at the end of the day we need rules/guidelines that are enforceable by (an) automated tool(s). And if that tool flags up an issue, with say the use of unwrap() where the developer believes they know better, then it is acceptable with a peer review to override the tool's flagged issue. And as mentioned above by @darkwisebear, document the fact as to why it's not a problem. And as mentioned by @PLeVasseur, "not great to reach for in "production code", but they have their legitimate use cases.". |
Should .unwrap() be allowed anywhere in safety critical code ?
If so, under what circumstances?
If not this would seem ideal as a first rule/guideline and something that could be enforced by the tooling quite easily.
The text was updated successfully, but these errors were encountered: