Skip to content

Commit 0929018

Browse files
chore: Apply suggestions
Co-authored-by: Nick <[email protected]>
1 parent 7aaa845 commit 0929018

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

modules/contributor/pages/code-style-guide.adoc

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -533,12 +533,14 @@ enum Error {
533533

534534
=== Using `unwrap`
535535

536-
Generally, it is not recommended to use `unwrap` (or any other method which consumes the error) in any fallible code path.
537-
Instead, proper error handling like above should be used.
536+
The `unwrap` function must not be used in any fallible code paths.
537+
Instead, proper error handling like above should be used, unless there is a valid reason to use `expect` described below.
538+
Using https://doc.rust-lang.org/std/result/enum.Result.html#method.unwrap_or[`unwrap_or`], https://doc.rust-lang.org/std/result/enum.Result.html#method.unwrap_or_default[`unwrap_or_default`] or https://doc.rust-lang.org/std/result/enum.Result.html#method.unwrap_or_else[`unwrap_or_else`] is allowed because these functions will not panic.
539+
538540
There are however cases, where it is fine to use `unwrap` or friends.
539541

540-
One such an example is when compiling regular expressions inside const/static environments.
541-
For such cases code must use `expect` instead of `unwrap` to provide additional context why a particular piece of code should never fail.
542+
The `expect` function can be used when external factors cannot influence whether a panic will happen. For example, when compiling regular expressions inside const/static environments.
543+
For such cases code must use `expect` instead of `unwrap` to provide additional context for why a particular piece of code should never fail.
542544

543545
// Do we want to mention that this is enforced via clippy and that we actually enable that lint in our repos?
544546

@@ -775,7 +777,7 @@ mod test {
775777

776778
=== Using `unwrap`
777779

778-
The usage of `unwrap` in unit tests is not recommended for the same reasons as mentioned above, but allowed.
780+
The usage of `unwrap` in unit tests is also not allowed for the same reasons as mentioned above.
779781

780782
[TIP.code-rule,caption=Examples of correct code for this rule]
781783
====

0 commit comments

Comments
 (0)