-
Notifications
You must be signed in to change notification settings - Fork 13.1k
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
assert_eq!
has a terrible error message when given a trailing comma
#39369
Comments
I think we should add a hint to remove the last comma. |
er... aren't you supposed to provide one then? :) |
@abhijeetbhagat You certainly can do that, yes, but you don't have to. |
This looks tricky. What's happening is that the comma puts us into the "match a 'token tree' of zero or more additional tokens after the What we would want is to only match one or more additional tokens (or emulate this by conditioning on the length of the token tree or something), but I don't immediately see how or whether this can be done with the macro system (my naïve hopes that |
Wait, it looks like I was wrong about this! |
Previously, `assert_eq!(left, right,)` (respectively, `assert_ne!(left, right,)`; note the trailing comma) would result in a confusing "requires at least a format string argument" error. In reality, a format string is optional, but the trailing comma puts us into the "match a token tree of zero or more tokens" branch of the macro (in order to support the optional format string), and passing the empty token tree into `format_args!` results in the confusing error. If instead we match a token tree of one or more tokens, we get a much more sensible "unexpected end of macro invocation" error. While we're here, fix up a stray space before a comma in the match guards. Resolves rust-lang#39369.
…age_when_given_a_trailing_comma, r=BurntSushi improve error message when two-arg assert_eq! receives a trailing comma Previously, `assert_eq!(left, right,)` (respectively, `assert_ne!(left, right,)`; note the trailing comma) would result in a confusing "requires at least a format string argument" error. In reality, a format string is optional, but the trailing comma puts us into the "match a token tree of zero or more tokens" branch of the macro (in order to support the optional format string), and passing the empty token tree into `format_args!` results in the confusing error. If instead we match a token tree of one or more tokens, we get a much more sensible "unexpected end of macro invocation" error. While we're here, fix up a stray space before a comma in the match guards. Resolves #39369. ----- **Before:** ``` $ rustc scratch.rs error: requires at least a format string argument --> scratch.rs:2:5 | 2 | assert_eq!(1, 2,); | ^^^^^^^^^^^^^^^^^^ | = note: this error originates in a macro outside of the current crate error: aborting due to previous error ``` **After:** ``` $ ./build/x86_64-unknown-linux-gnu/stage1/bin/rustc scratch.rs error: unexpected end of macro invocation --> scratch.rs:2:20 | 2 | assert_eq!(1, 2,); | ^ ```
Trailing commas are an easy mistake to make, especially in a language like Rust where they are often accepted. Erroneously adding a trailing comma to
assert_eq!
triggers the error message "requires at least a format string argument", which is confusing and leads the user to think (incorrectly) that they need to add some kind of format string. Example.The text was updated successfully, but these errors were encountered: