|
| 1 | +// Public struct with #[derive(PartialOrd)] and reordered fields - should trigger warning |
| 2 | +#[derive(PartialOrd, PartialEq)] |
| 3 | +pub struct PublicStruct { |
| 4 | + pub b: u16, // moved |
| 5 | + pub c: u32, // moved |
| 6 | + pub a: u8, // moved |
| 7 | +} |
| 8 | + |
| 9 | +// Private struct with #[derive(PartialOrd)] and reordered fields - should not trigger |
| 10 | +#[derive(PartialOrd, PartialEq)] |
| 11 | +struct PrivateStruct { |
| 12 | + pub b: u16, |
| 13 | + pub c: u32, |
| 14 | + pub a: u8, |
| 15 | +} |
| 16 | + |
| 17 | +// Public struct without #[derive(PartialOrd)] but with reordered fields - should not trigger |
| 18 | +pub struct RegularStruct { |
| 19 | + pub b: u16, |
| 20 | + pub c: u32, |
| 21 | + pub a: u8, |
| 22 | +} |
| 23 | + |
| 24 | +// Public struct with #[derive(PartialOrd)] and doc(hidden) - should not trigger |
| 25 | +#[doc(hidden)] |
| 26 | +#[derive(PartialOrd, PartialEq)] |
| 27 | +pub struct DocHiddenStruct { |
| 28 | + pub b: u16, |
| 29 | + pub c: u32, |
| 30 | + pub a: u8, |
| 31 | +} |
| 32 | + |
| 33 | +// Public struct with #[derive(PartialOrd)] and doc(hidden) fields swapped - should not trigger |
| 34 | +#[derive(PartialOrd, PartialEq)] |
| 35 | +pub struct PublicStructWithDocHiddenFieldsSwapped { |
| 36 | + pub a: u8, |
| 37 | + #[doc(hidden)] |
| 38 | + pub c: u32, |
| 39 | + #[doc(hidden)] |
| 40 | + pub b: u16, |
| 41 | + pub d: u64, |
| 42 | +} |
| 43 | + |
| 44 | +// Public struct with #[derive(PartialOrd)] and same order - should not trigger |
| 45 | +#[derive(PartialOrd, PartialEq)] |
| 46 | +pub struct UnchangedStruct { |
| 47 | + pub x: u8, // same position |
| 48 | + pub y: u16, // same position |
| 49 | +} |
| 50 | + |
| 51 | +// Test with manual PartialOrd implementation - should not trigger |
| 52 | +#[derive(PartialEq)] |
| 53 | +pub struct ManuallyImplemented { |
| 54 | + pub b: u16, // reordered but has manual impl |
| 55 | + pub a: u8, |
| 56 | +} |
| 57 | + |
| 58 | +impl PartialOrd for ManuallyImplemented { |
| 59 | + fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> { |
| 60 | + Some(self.a.cmp(&other.a)) |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +// Test with other derives - should not trigger |
| 65 | +#[derive(Debug, Clone, PartialEq)] |
| 66 | +pub struct OtherDerives { |
| 67 | + pub b: u16, |
| 68 | + pub a: u8, |
| 69 | +} |
| 70 | + |
| 71 | +// Test with multiple derives including PartialOrd - should trigger |
| 72 | +#[derive(Debug, PartialOrd, Clone, PartialEq)] |
| 73 | +pub struct MultipleDerivesWithPartialOrd { |
| 74 | + pub b: u16, // moved |
| 75 | + pub c: u32, // moved |
| 76 | + pub a: u8, // moved |
| 77 | +} |
| 78 | + |
| 79 | +// Struct that no longer derives PartialOrd but has reordered fields - should not trigger |
| 80 | +pub struct NoLongerPartialOrd { |
| 81 | + pub b: u16, |
| 82 | + pub c: u32, |
| 83 | + pub a: u8, |
| 84 | +} |
| 85 | + |
| 86 | +// Struct that newly derives PartialOrd and has reordered fields - should not trigger |
| 87 | +#[derive(PartialOrd, PartialEq)] |
| 88 | +pub struct NewlyPartialOrd { |
| 89 | + pub b: u16, |
| 90 | + pub c: u32, |
| 91 | + pub a: u8, |
| 92 | +} |
| 93 | + |
| 94 | +// Struct that switches from derived to hand-implemented PartialOrd - should not trigger |
| 95 | +pub struct SwitchToHandImpl { |
| 96 | + pub b: u16, |
| 97 | + pub c: u32, |
| 98 | + pub a: u8, |
| 99 | +} |
| 100 | + |
| 101 | +impl PartialEq for SwitchToHandImpl { |
| 102 | + fn eq(&self, other: &Self) -> bool { |
| 103 | + self.a == other.a && self.b == other.b && self.c == other.c |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +impl PartialOrd for SwitchToHandImpl { |
| 108 | + fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> { |
| 109 | + // Custom implementation that maintains original ordering logic |
| 110 | + match self.a.partial_cmp(&other.a) { |
| 111 | + Some(core::cmp::Ordering::Equal) => {} |
| 112 | + ord => return ord, |
| 113 | + } |
| 114 | + match self.b.partial_cmp(&other.b) { |
| 115 | + Some(core::cmp::Ordering::Equal) => {} |
| 116 | + ord => return ord, |
| 117 | + } |
| 118 | + self.c.partial_cmp(&other.c) |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +// Struct that switches from hand-implemented to derived PartialOrd - should not trigger |
| 123 | +#[derive(PartialOrd, PartialEq)] |
| 124 | +pub struct SwitchToDerived { |
| 125 | + pub b: u16, |
| 126 | + pub c: u32, |
| 127 | + pub a: u8, |
| 128 | +} |
0 commit comments