assert_matches icon indicating copy to clipboard operation
assert_matches copied to clipboard

Permit trailing commas in macro invocations

Open myrrlyn opened this issue 5 years ago • 5 comments

This commit uses the ? operator to support optional trailing commas in macro calls without requiring the call to use the panic-message arms.

The ? macro operator was stabilized in 1.32 for the 2018 edition, and 1.37 for the 2015 edition. Applying this commit requires raising the crate MSRV to one of these two versions; for the lower, the crate must be explicitly set to the edition = "2018" key in its manifest.

myrrlyn avatar Feb 17 '20 03:02 myrrlyn

I wrote this patch because I noticed that my rustfmt settings were causing macro calls to break. Insertion of the trailing commas forced the macro expansion into the arms expecting a panic message.

As noted, it forces an MSRV. Please feel completely free to reject the PR if this is unacceptable.

myrrlyn avatar Feb 17 '20 04:02 myrrlyn

I have to say, I'm not persuaded that there's a good reason to accept this proposed change and I'm inclined to reject it.

It seems to me that rustfmt, as a rule, should not change the tokens within a macro invocation. Indeed, it seems that the default settings of rustfmt do not change macro tokens. Further, the matches macro recently added to std does not accept a trailing comma (although that could change between now and the time of its stabilization).

murarth avatar Feb 19 '20 17:02 murarth

My motivation for submitting this change was predicated solely on the fact that all the std macros started out without trailing comma support, and then added it in 1.26. I elected to use the ? operator instead of duplicate arms only for simplicity. If you're opposed to the requisite MSRV that imposes, I'm happy to rewrite to the std style; if you're opposed syntactically, I have absolutely no hard feelings about closing this.

myrrlyn avatar Feb 19 '20 18:02 myrrlyn

I've stumbled upon this issue recently. I have to notice that the matches! std macro already accepts a trailing comma. Moreover, I do not see any reason not to accept an optional trailing comma here, I suppose that it should be the user's choice which code style to use.

nothingelsematters avatar Oct 06 '22 11:10 nothingelsematters