Skip to content

Commit

Permalink
Turn trailing tokens in assert!() into hard errors
Browse files Browse the repository at this point in the history
  • Loading branch information
LeSeulArtichaut committed Mar 6, 2020
1 parent 865b44a commit 33ebc20
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 21 deletions.
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

0 comments on commit 33ebc20

Please sign in to comment.