|
| 1 | +//rustfmt-version: One |
| 2 | + |
| 3 | +// To visually verify the special case handling of `matches!` we include the equivalent match expr |
| 4 | + |
| 5 | +// issue #4462 |
| 6 | +fn issue_4462() { |
| 7 | +matches!(c, |
| 8 | + 'x' | 'c' | 'b' | 'B' | '?' | 'h' | 'H' | 'i' | 'I' | 'l' | 'L' | 'q' | 'Q' | 'n' |
| 9 | + | 'N' | 'f' | 'd' | 's' | 'p' | 'P'); |
| 10 | + |
| 11 | +match c { |
| 12 | + 'x' | 'c' | 'b' | 'B' | '?' | 'h' | 'H' | 'i' | 'I' | 'l' | 'L' | 'q' | 'Q' | 'n' |
| 13 | + | 'N' | 'f' | 'd' | 's' | 'p' | 'P' => {} |
| 14 | +} |
| 15 | +} |
| 16 | + |
| 17 | +// issue #4885 |
| 18 | +fn issue_4885() { |
| 19 | +matches!( |
| 20 | + c, |
| 21 | + '\\' | '.' | '+' | '(' | ')' | '|' | '[' | ']' | '{' | '}' | '^' | |
| 22 | + '$' | '#' | '&' | '-' | '~' | '*' | '?' |
| 23 | +); |
| 24 | + |
| 25 | +match c { |
| 26 | + '\\' | '.' | '+' | '(' | ')' | '|' | '[' | ']' | '{' | '}' | '^' | |
| 27 | + '$' | '#' | '&' | '-' | '~' | '*' | '?' => {} |
| 28 | +} |
| 29 | +} |
| 30 | + |
| 31 | +// issue #5176 |
| 32 | +fn issue_5176() { |
| 33 | +matches!(self, | Self::A | Self::B); |
| 34 | +match self { |
| 35 | + | Self::A | Self::B => {} |
| 36 | +} |
| 37 | +} |
| 38 | + |
| 39 | +// issue #5547 |
| 40 | +fn issue_5547() { |
| 41 | + matches!(something.very_very_very.long.even.more.fields, Some(very_long_field_name_name_to_check) if method(very_long_field_name)); |
| 42 | + match something.very_very_very.long.even.more.fields { |
| 43 | + Some(very_long_field_name_name_to_check) if method(very_long_field_name) => {} |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +// other scenarios |
| 48 | +fn other_scenarios() { |
| 49 | + |
| 50 | + // no guard with trailing comma |
| 51 | + matches!(self, | Self::A | Self::B,); |
| 52 | + match self { |
| 53 | + | Self::A | Self::B => {} |
| 54 | + } |
| 55 | + |
| 56 | + // guard with trailing comma |
| 57 | + matches!(something.very_very_very.long.even.more.fields, Some(very_long_field_name_name_to_check) if method(very_long_field_name),); |
| 58 | + match something.very_very_very.long.even.more.fields { |
| 59 | + Some(very_long_field_name_name_to_check) if method(very_long_field_name) => {} |
| 60 | + } |
| 61 | + |
| 62 | + // short expr and pattern, but guard is long. |
| 63 | + matches!(something, Some(_) if method(very_long_input_1, very_long_input_2, very_long_input_3, very_long_input_4, very_long_input_5),); |
| 64 | + match something { |
| 65 | + Some(_) if method(very_long_input_1, very_long_input_2, very_long_input_3, very_long_input_4, very_long_input_5) => {} |
| 66 | + } |
| 67 | + |
| 68 | + // square brackets |
| 69 | + matches![self, | Self::A | Self::B]; |
| 70 | + match self { |
| 71 | + | Self::A | Self::B => {} |
| 72 | + } |
| 73 | + // curly braces |
| 74 | + matches!{self, | Self::A | Self::B}; |
| 75 | + match self { |
| 76 | + | Self::A | Self::B => {} |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +// nested matches! calls |
| 81 | +impl Mystruct { |
| 82 | + pub(crate) fn is_expr(&self) -> bool { |
| 83 | + matches!( |
| 84 | + self, |
| 85 | + OverflowableItem::Expr(..) |
| 86 | + | OverflowableItem::MacroArg(MacroArg::Expr(..)) |
| 87 | + | OverflowableItem::MatchMacroItem(MatchMacroItem::Expr(..)) |
| 88 | + ); |
| 89 | + |
| 90 | + match self { |
| 91 | + OverflowableItem::Expr(..) |
| 92 | + | OverflowableItem::MacroArg(MacroArg::Expr(..)) |
| 93 | + | OverflowableItem::MatchMacroItem(MatchMacroItem::Expr(..)) => {} |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + pub(crate) fn is_expr_with_guard(&self) -> bool { |
| 98 | + matches!( |
| 99 | + self, |
| 100 | + OverflowableItem::Expr(..) |
| 101 | + | OverflowableItem::MacroArg(MacroArg::Expr(..)) |
| 102 | + | OverflowableItem::MatchMacroItem(MatchMacroItem::Expr(..)) |
| 103 | + if self.condition() |
| 104 | + ); |
| 105 | + |
| 106 | + match self { |
| 107 | + OverflowableItem::Expr(..) |
| 108 | + | OverflowableItem::MacroArg(MacroArg::Expr(..)) |
| 109 | + | OverflowableItem::MatchMacroItem(MatchMacroItem::Expr(..)) |
| 110 | + if self.condition() => {} |
| 111 | + } |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +fn multi_line_struct_pattern_with_guard() { |
| 116 | + matches!( |
| 117 | + token, |
| 118 | + Token::Dimension { |
| 119 | + value, |
| 120 | + ref unit, |
| 121 | + .. |
| 122 | + } if num_context.is_ok(context.parsing_mode, value) |
| 123 | + ); |
| 124 | + |
| 125 | + match token { |
| 126 | + Token::Dimension { |
| 127 | + value, |
| 128 | + ref unit, |
| 129 | + .. |
| 130 | + } if num_context.is_ok(context.parsing_mode, value) => { |
| 131 | + // body |
| 132 | + }, |
| 133 | + } |
| 134 | +} |
0 commit comments