Suggest surrounding the macro with {} to interpret as a statement

This commit is contained in:
Mu42 2023-03-17 14:36:22 +08:00
parent 511364e787
commit 550e3087d1
3 changed files with 43 additions and 6 deletions

View file

@ -245,12 +245,24 @@ pub(super) fn emit_frag_parse_err(
e.note(
"the macro call doesn't expand to an expression, but it can expand to a statement",
);
e.span_suggestion_verbose(
site_span.shrink_to_hi(),
"add `;` to interpret the expansion as a statement",
";",
Applicability::MaybeIncorrect,
);
if parser.token == token::Semi {
if let Ok(snippet) = parser.sess.source_map().span_to_snippet(site_span) {
e.span_suggestion_verbose(
site_span,
"surround the macro invocation with `{}` to interpret the expansion as a statement",
format!("{{ {}; }}", snippet),
Applicability::MaybeIncorrect,
);
}
} else {
e.span_suggestion_verbose(
site_span.shrink_to_hi(),
"add `;` to interpret the expansion as a statement",
";",
Applicability::MaybeIncorrect,
);
}
}
},
_ => annotate_err_with_kind(&mut e, kind, site_span),

View file

@ -0,0 +1,7 @@
macro_rules! statement {
() => {;}; //~ ERROR expected expression
}
fn main() {
let _ = statement!();
}

View file

@ -0,0 +1,18 @@
error: expected expression, found `;`
--> $DIR/issue-109237.rs:2:12
|
LL | () => {;};
| ^ expected expression
...
LL | let _ = statement!();
| ------------ in this macro invocation
|
= note: the macro call doesn't expand to an expression, but it can expand to a statement
= note: this error originates in the macro `statement` (in Nightly builds, run with -Z macro-backtrace for more info)
help: surround the macro invocation with `{}` to interpret the expansion as a statement
|
LL | let _ = { statement!(); };
| ~~~~~~~~~~~~~~~~~
error: aborting due to previous error