about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2020-04-02 13:39:30 +0000
committerbors <bors@rust-lang.org>2020-04-02 13:39:30 +0000
commit949a5bab3333adc5ffcbca618cc93718d0615281 (patch)
tree92668cf8889427d7b5f940f0943e1f186f16b233
parenta840d594cc6eee7cc926e0ddbfb59550891c1060 (diff)
parent5f8b696e2e76374fe600ee0f0e444e94215239b6 (diff)
downloadrust-949a5bab3333adc5ffcbca618cc93718d0615281.tar.gz
rust-949a5bab3333adc5ffcbca618cc93718d0615281.zip
Auto merge of #5403 - farnz:patch-1, r=flip1995
Improve docs for option_option

Hint about using tri-state enums to replace legitimate uses of `Option<Option<_>>`

changelog: The docs for `option_option` now suggest using a tri-state enum
-rw-r--r--clippy_lints/src/types.rs19
1 files changed, 18 insertions, 1 deletions
diff --git a/clippy_lints/src/types.rs b/clippy_lints/src/types.rs
index b21c3739265..8c151f2277c 100644
--- a/clippy_lints/src/types.rs
+++ b/clippy_lints/src/types.rs
@@ -99,14 +99,31 @@ declare_clippy_lint! {
     /// represents an optional optional value which is logically the same thing as an optional
     /// value but has an unneeded extra level of wrapping.
     ///
+    /// If you have a case where `Some(Some(_))`, `Some(None)` and `None` are distinct cases,
+    /// consider a custom `enum` instead, with clear names for each case.
+    ///
     /// **Known problems:** None.
     ///
     /// **Example**
     /// ```rust
-    /// fn x() -> Option<Option<u32>> {
+    /// fn get_data() -> Option<Option<u32>> {
     ///     None
     /// }
     /// ```
+    ///
+    /// Better:
+    ///
+    /// ```rust
+    /// pub enum Contents {
+    ///     Data(Vec<u8>), // Was Some(Some(Vec<u8>))
+    ///     NotYetFetched, // Was Some(None)
+    ///     None,          // Was None
+    /// }
+    ///
+    /// fn get_data() -> Contents {
+    ///     Contents::None
+    /// }
+    /// ```
     pub OPTION_OPTION,
     pedantic,
     "usage of `Option<Option<T>>`"