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

Turn trailing tokens in assert!() into hard errors #69548

Merged
merged 1 commit into from
Mar 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions src/librustc_builtin_macros/assert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,15 @@ fn parse_assert<'a>(
// my_function();
// );
//
// Warn about semicolon and suggest removing it. Eventually, this should be turned into an
// error.
// Emit an error about semicolon and suggest removing it.
if parser.token == token::Semi {
let mut err = cx.struct_span_warn(sp, "macro requires an expression as an argument");
let mut err = cx.struct_span_err(sp, "macro requires an expression as an argument");
err.span_suggestion(
parser.token.span,
"try removing semicolon",
String::new(),
Applicability::MaybeIncorrect,
);
err.note("this is going to be an error in the future");
err.emit();

parser.bump();
Expand All @@ -101,19 +99,17 @@ fn parse_assert<'a>(
//
// assert!(true "error message");
//
// Parse this as an actual message, and suggest inserting a comma. Eventually, this should be
// turned into an error.
// Emit an error and suggest inserting a comma.
let custom_message =
if let token::Literal(token::Lit { kind: token::Str, .. }) = parser.token.kind {
let mut err = cx.struct_span_warn(parser.token.span, "unexpected string literal");
let mut err = cx.struct_span_err(parser.token.span, "unexpected string literal");
let comma_span = parser.prev_token.span.shrink_to_hi();
err.span_suggestion_short(
comma_span,
"try adding a comma",
", ".to_string(),
Applicability::MaybeIncorrect,
);
err.note("this is going to be an error in the future");
err.emit();

parse_custom_message(&mut parser)
Expand Down
6 changes: 3 additions & 3 deletions src/test/ui/macros/assert-trailing-junk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ fn main() {
//~^ ERROR no rules expected

assert!(true "whatever" blah);
//~^ WARN unexpected string literal
//~^ ERROR unexpected string literal
//~^^ ERROR no rules expected

assert!(true;);
//~^ WARN macro requires an expression
//~^ ERROR macro requires an expression

assert!(false || true "error message");
//~^ WARN unexpected string literal
//~^ ERROR unexpected string literal
}
14 changes: 4 additions & 10 deletions src/test/ui/macros/assert-trailing-junk.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,13 @@ LL | assert!(true, "whatever" blah);
| |
| help: missing comma here

warning: unexpected string literal
error: unexpected string literal
--> $DIR/assert-trailing-junk.rs:15:18
|
LL | assert!(true "whatever" blah);
| -^^^^^^^^^^
| |
| help: try adding a comma
|
= note: this is going to be an error in the future

error: no rules expected the token `blah`
--> $DIR/assert-trailing-junk.rs:15:29
Expand All @@ -36,25 +34,21 @@ LL | assert!(true "whatever" blah);
| |
| help: missing comma here

warning: macro requires an expression as an argument
error: macro requires an expression as an argument
--> $DIR/assert-trailing-junk.rs:19:5
|
LL | assert!(true;);
| ^^^^^^^^^^^^-^^
| |
| help: try removing semicolon
|
= note: this is going to be an error in the future

warning: unexpected string literal
error: unexpected string literal
--> $DIR/assert-trailing-junk.rs:22:27
|
LL | assert!(false || true "error message");
| -^^^^^^^^^^^^^^^
| |
| help: try adding a comma
|
= note: this is going to be an error in the future

error: aborting due to 4 previous errors
error: aborting due to 7 previous errors