You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: modules/contributor/pages/code-style-guide.adoc
+7-5Lines changed: 7 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -533,12 +533,14 @@ enum Error {
533
533
534
534
=== Using `unwrap`
535
535
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
+
538
540
There are however cases, where it is fine to use `unwrap` or friends.
539
541
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.
542
544
543
545
// Do we want to mention that this is enforced via clippy and that we actually enable that lint in our repos?
544
546
@@ -775,7 +777,7 @@ mod test {
775
777
776
778
=== Using `unwrap`
777
779
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.
779
781
780
782
[TIP.code-rule,caption=Examples of correct code for this rule]
0 commit comments