Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add special case handling for matches! to format args as patterns #4462

Open
youknowone opened this issue Oct 10, 2020 · 5 comments · May be fixed by #5554
Open

Add special case handling for matches! to format args as patterns #4462

youknowone opened this issue Oct 10, 2020 · 5 comments · May be fixed by #5554

Comments

@youknowone
Copy link

youknowone commented Oct 10, 2020

Input

        matches!(c,
            'x' | 'c' | 'b' | 'B' | '?' | 'h' | 'H' | 'i' | 'I' | 'l' | 'L' | 'q' | 'Q' | 'n'
            | 'N' | 'f' | 'd' | 's' | 'p' | 'P')

Output

        matches!(
            c,
            'x' | 'c'    // <- this part make inconsistency of the formatting
                | 'b'
                | 'B'
                | '?'
                | 'h'
                | 'H'
                | 'i'
                | 'I'
                | 'l'
                | 'L'
                | 'q'
                | 'Q'
                | 'n'
                | 'N'
                | 'f'
                | 'd'
                | 's'
                | 'p'
                | 'P'
        )

Expected output

I expect the input form

        matches!(c,
            'x' | 'c' | 'b' | 'B' | '?' | 'h' | 'H' | 'i' | 'I' | 'l' | 'L' | 'q' | 'Q' | 'n'
            | 'N' | 'f' | 'd' | 's' | 'p' | 'P')

Because it is the closest when we use match

        match c {
            'x' | 'c' | 'b' | 'B' | '?' | 'h' | 'H' | 'i' | 'I' | 'l' | 'L' | 'q' | 'Q' | 'n'
            | 'N' | 'f' | 'd' | 's' | 'p' | 'P' => true,
            _ => false,
        }

Or this kind of form is also acceptable in term of consistency

        matches!(
            c,
            'x' 
            | 'c'
            | 'b'
            | 'B'
            | '?'
            | 'h'
            | 'H'
            | 'i'
            | 'I'
            | 'l'
            | 'L'
            | 'q'
            | 'Q'
            | 'n'
            | 'N'
            | 'f'
            | 'd'
            | 's'
            | 'p'
            | 'P'
        )

Meta

  • rustfmt version: 1.4.20-stable (48f6c32e 2020-08-09)
  • From where did you install rustfmt?: rustup
@coolreader18
Copy link

I think this is due to not special-casing matches!() -- this is the same formatting given to a long bitwise OR chain:

fn foo() {
    1 | 2
        | 3
        | 4
        | 5
        | 6
        | 7
        | 8
        | 9
        | 10
        | 11
        | 12
        | 13
        | 14
        | 16
        | 61
        | 1521
        | 5
        | 678
        | 85394
        | 143
}

@bcantrill
Copy link

This feels like another example of #4306 -- and another example where it would be nice to defer to programmer discretion.

@topecongiro
Copy link
Contributor

@coolreader18's observation is correct. Since matches! is in the standard library, I think it is safe to special-case it in rustfmt.

@youknowone youknowone changed the title matches!() with long pattern formatted as ugly long form long bitwise OR chains are formatted with ugly head Oct 14, 2020
@youknowone
Copy link
Author

I edited the title and content to make clear.

#4306 seems to be one of solutions of this issue but not only by that.

To be more specifically, I think the first line including 2 items is the buggy part.
I think this at least can be handled like trailing commas to make every line equals. (except for the last one)

@youknowone
Copy link
Author

I tested a bit with master and the version seems to put only one item for each line

 fn issue4462() -> isize {
-    100000001 | 100000002 | 100000003 | 100000004 | 100000005 | 100000006 | 100000007 | 100000008 | 100000009 | 100000010
+    100000001
+    | 100000002
+    | 100000003
+    | 100000004
+    | 100000005
+    | 100000006
+    | 100000007
+    | 100000008
+    | 100000009
+    | 100000010
 }

@calebcartwright calebcartwright changed the title long bitwise OR chains are formatted with ugly head Add special case handling for matches! to format args as patterns Jul 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants