]> git.lizzy.rs Git - rust.git/commitdiff
add description for derive_ord_xor_partial_ord
authorRyan1729 <Ryan1729@gmail.com>
Mon, 27 Jul 2020 05:15:36 +0000 (23:15 -0600)
committerRyan1729 <Ryan1729@gmail.com>
Mon, 27 Jul 2020 05:15:36 +0000 (23:15 -0600)
clippy_lints/src/derive.rs

index ab001f7773e444842c505d552bd12b41eb83287c..84566252abd714b23c59ed72b9116a17d77a74da 100644 (file)
 }
 
 declare_clippy_lint! {
-    /// **What it does:**
+    /// **What it does:** Checks for deriving `Ord` but implementing `PartialOrd`
+    /// explicitly or vice versa.
+    ///
+    /// **Why is this bad?** The implementation of these traits must agree (for
+    /// example for use with `sort`) so it’s probably a bad idea to use a
+    /// default-generated `Ord` implementation with an explicitly defined
+    /// `PartialOrd`. In particular, the following must hold for any type
+    /// implementing `Ord`:
     ///
-    /// **Why is this bad?**
+    /// ```text
+    /// k1.cmp(&k2) == k1.partial_cmp(&k2).unwrap()
+    /// ```
     ///
     /// **Known problems:** None.
     ///
     /// **Example:**
     ///
-    /// ```rust
-    /// // example code where clippy issues a warning
+    /// ```rust,ignore
+    /// #[derive(Ord, PartialEq, Eq)]
+    /// struct Foo;
+    ///
+    /// impl PartialOrd for Foo {
+    ///     ...
+    /// }
     /// ```
     /// Use instead:
-    /// ```rust
-    /// // example code which does not raise clippy warning
+    /// ```rust,ignore
+    /// #[derive(PartialEq, Eq)]
+    /// struct Foo;
+    ///
+    /// impl PartialOrd for Foo {
+    ///     fn partial_cmp(&self, other: &Foo) -> Option<Ordering> {
+    ///        Some(self.cmp(other))
+    ///     }
+    /// }
+    ///
+    /// impl Ord for Foo {
+    ///     ...
+    /// }
+    /// ```
+    /// or, if you don't need a custom ordering:
+    /// ```rust,ignore
+    /// #[derive(Ord, PartialOrd, PartialEq, Eq)]
+    /// struct Foo;
     /// ```
     pub DERIVE_ORD_XOR_PARTIAL_ORD,
     correctness,