|
| 1 | +//@aux-build:non-exhaustive-enum.rs |
| 2 | +#![allow( |
| 3 | + clippy::manual_unwrap_or_default, |
| 4 | + clippy::manual_unwrap_or, |
| 5 | + clippy::redundant_pattern_matching |
| 6 | +)] |
| 7 | +#![warn(clippy::unneeded_struct_pattern)] |
| 8 | + |
| 9 | +extern crate non_exhaustive_enum; |
| 10 | +use non_exhaustive_enum::*; |
| 11 | + |
| 12 | +fn noop() {} |
| 13 | + |
| 14 | +fn main() { |
| 15 | + match Some(114514) { |
| 16 | + Some(v) => v, |
| 17 | + None => 0, |
| 18 | + }; |
| 19 | + |
| 20 | + match Some(1919810) { |
| 21 | + Some(v) => v, |
| 22 | + None => 0, |
| 23 | + }; |
| 24 | + |
| 25 | + match Some(123456) { |
| 26 | + Some(v) => v, |
| 27 | + None => 0, |
| 28 | + }; |
| 29 | + |
| 30 | + match Some(Some(123456)) { |
| 31 | + Some(Some(v)) => v, |
| 32 | + Some(None) => 0, |
| 33 | + None => 0, |
| 34 | + }; |
| 35 | + |
| 36 | + if let None = Some(0) {} |
| 37 | + if let None = Some(0) {} |
| 38 | + if let Some(None) = Some(Some(0)) {} |
| 39 | + let None = Some(0) else { panic!() }; |
| 40 | + let None = Some(0) else { panic!() }; |
| 41 | + let Some(None) = Some(Some(0)) else { panic!() }; |
| 42 | + |
| 43 | + enum Custom { |
| 44 | + HasFields { |
| 45 | + field: i32, |
| 46 | + }, |
| 47 | + HasBracketsNoFields {}, |
| 48 | + NoBrackets, |
| 49 | + #[non_exhaustive] |
| 50 | + NoBracketsNonExhaustive, |
| 51 | + Init, |
| 52 | + }; |
| 53 | + |
| 54 | + match Custom::Init { |
| 55 | + Custom::HasFields { field: value } => value, |
| 56 | + Custom::HasBracketsNoFields {} => 0, |
| 57 | + Custom::NoBrackets => 0, //~ ERROR: struct pattern is not needed for a unit variant |
| 58 | + Custom::NoBracketsNonExhaustive => 0, //~ ERROR: struct pattern is not needed for a unit variant |
| 59 | + _ => 0, |
| 60 | + }; |
| 61 | + |
| 62 | + match Custom::Init { |
| 63 | + Custom::HasFields { field: value } => value, |
| 64 | + Custom::HasBracketsNoFields { .. } => 0, |
| 65 | + Custom::NoBrackets => 0, //~ ERROR: struct pattern is not needed for a unit variant |
| 66 | + Custom::NoBracketsNonExhaustive => 0, //~ ERROR: struct pattern is not needed for a unit variant |
| 67 | + _ => 0, |
| 68 | + }; |
| 69 | + |
| 70 | + match Custom::Init { |
| 71 | + Custom::NoBrackets if true => 0, //~ ERROR: struct pattern is not needed for a unit variant |
| 72 | + _ => 0, |
| 73 | + }; |
| 74 | + |
| 75 | + match Custom::Init { |
| 76 | + Custom::NoBrackets | Custom::NoBracketsNonExhaustive => 0, //~ ERROR: struct pattern is not needed for a unit variant |
| 77 | + _ => 0, |
| 78 | + }; |
| 79 | + |
| 80 | + if let Custom::HasFields { field: value } = Custom::Init { |
| 81 | + noop(); |
| 82 | + } |
| 83 | + if let Custom::HasBracketsNoFields {} = Custom::Init { |
| 84 | + noop(); |
| 85 | + } |
| 86 | + if let Custom::HasBracketsNoFields { .. } = Custom::Init { |
| 87 | + noop(); |
| 88 | + } |
| 89 | + if let Custom::NoBrackets = Custom::Init { |
| 90 | + //~^ ERROR: struct pattern is not needed for a unit variant |
| 91 | + noop(); |
| 92 | + } |
| 93 | + if let Custom::NoBrackets = Custom::Init { |
| 94 | + //~^ ERROR: struct pattern is not needed for a unit variant |
| 95 | + noop(); |
| 96 | + } |
| 97 | + if let Custom::NoBrackets | Custom::NoBracketsNonExhaustive = Custom::Init { |
| 98 | + //~^ ERROR: struct pattern is not needed for a unit variant |
| 99 | + noop(); |
| 100 | + } |
| 101 | + if let Custom::NoBracketsNonExhaustive = Custom::Init { |
| 102 | + //~^ ERROR: struct pattern is not needed for a unit variant |
| 103 | + noop(); |
| 104 | + } |
| 105 | + if let Custom::NoBracketsNonExhaustive = Custom::Init { |
| 106 | + //~^ ERROR: struct pattern is not needed for a unit variant |
| 107 | + noop(); |
| 108 | + } |
| 109 | + |
| 110 | + let Custom::HasFields { field: value } = Custom::Init else { |
| 111 | + panic!() |
| 112 | + }; |
| 113 | + |
| 114 | + let Custom::HasBracketsNoFields {} = Custom::Init else { |
| 115 | + panic!() |
| 116 | + }; |
| 117 | + |
| 118 | + let Custom::HasBracketsNoFields { .. } = Custom::Init else { |
| 119 | + panic!() |
| 120 | + }; |
| 121 | + let Custom::NoBrackets = Custom::Init else { panic!() }; //~ ERROR: struct pattern is not needed for a unit variant |
| 122 | + |
| 123 | + let Custom::NoBrackets = Custom::Init else { |
| 124 | + //~^ ERROR: struct pattern is not needed for a unit variant |
| 125 | + panic!() |
| 126 | + }; |
| 127 | + let Custom::NoBracketsNonExhaustive = Custom::Init else { |
| 128 | + //~^ ERROR: struct pattern is not needed for a unit variant |
| 129 | + panic!() |
| 130 | + }; |
| 131 | + let Custom::NoBracketsNonExhaustive = Custom::Init else { |
| 132 | + //~^ ERROR: struct pattern is not needed for a unit variant |
| 133 | + panic!() |
| 134 | + }; |
| 135 | + |
| 136 | + enum Refutable { |
| 137 | + Variant, |
| 138 | + } |
| 139 | + |
| 140 | + fn pat_in_fn_param_1(Refutable::Variant: Refutable) {} //~ ERROR: struct pattern is not needed for a unit variant |
| 141 | + fn pat_in_fn_param_2(Refutable::Variant: Refutable) {} //~ ERROR: struct pattern is not needed for a unit variant |
| 142 | + |
| 143 | + for Refutable::Variant in [] {} //~ ERROR: struct pattern is not needed for a unit variant |
| 144 | + for Refutable::Variant in [] {} //~ ERROR: struct pattern is not needed for a unit variant |
| 145 | +} |
| 146 | + |
| 147 | +fn external_crate() { |
| 148 | + use ExtNonExhaustiveVariant::*; |
| 149 | + |
| 150 | + match ExhaustiveUnit { |
| 151 | + // Expected |
| 152 | + ExhaustiveUnit => 0, |
| 153 | + _ => 0, |
| 154 | + }; |
| 155 | + |
| 156 | + match ExhaustiveUnit { |
| 157 | + // Exhaustive variant |
| 158 | + ExhaustiveUnit => 0, //~ ERROR: struct pattern is not needed for a unit variant |
| 159 | + _ => 0, |
| 160 | + }; |
| 161 | + |
| 162 | + match ExhaustiveUnit { |
| 163 | + // Exhaustive variant |
| 164 | + ExhaustiveUnit => 0, //~ ERROR: struct pattern is not needed for a unit variant |
| 165 | + _ => 0, |
| 166 | + }; |
| 167 | + |
| 168 | + match ExhaustiveUnit { |
| 169 | + ExhaustiveUnit => 0, |
| 170 | + // vvvvv Non-exhaustive variants, should all be ignored |
| 171 | + Unit { .. } => 0, |
| 172 | + Tuple { 0: field, .. } => field, |
| 173 | + StructNoField { .. } => 0, |
| 174 | + Struct { field, .. } => field, |
| 175 | + _ => 0, |
| 176 | + }; |
| 177 | +} |
0 commit comments