suggest removing a semicolon after impl/trait items

This commit is contained in:
Takayuki Maeda 2022-08-12 18:11:01 +09:00
parent 93ab13b4e8
commit 48c0341a70
4 changed files with 66 additions and 4 deletions

View File

@ -670,14 +670,44 @@ impl<'a> Parser<'a> {
}
match parse_item(self) {
Ok(None) => {
let is_unnecessary_semicolon = !items.is_empty()
// When the close delim is `)` in a case like the following, `token.kind` is expected to be `token::CloseDelim(Delimiter::Parenthesis)`,
// but the actual `token.kind` is `token::CloseDelim(Delimiter::Bracket)`.
// This is because the `token.kind` of the close delim is treated as the same as
// that of the open delim in `TokenTreesReader::parse_token_tree`, even if the delimiters of them are different.
// Therefore, `token.kind` should not be compared here.
//
// issue-60075.rs
// ```
// trait T {
// fn qux() -> Option<usize> {
// let _ = if true {
// });
// ^ this close delim
// Some(4)
// }
// ```
&& self
.span_to_snippet(self.prev_token.span)
.map_or(false, |snippet| snippet == "}")
&& self.token.kind == token::Semi;
let semicolon_span = self.token.span;
// We have to bail or we'll potentially never make progress.
let non_item_span = self.token.span;
self.consume_block(Delimiter::Brace, ConsumeClosingDelim::Yes);
self.struct_span_err(non_item_span, "non-item in item list")
.span_label(open_brace_span, "item list starts here")
let mut err = self.struct_span_err(non_item_span, "non-item in item list");
err.span_label(open_brace_span, "item list starts here")
.span_label(non_item_span, "non-item starts here")
.span_label(self.prev_token.span, "item list ends here")
.emit();
.span_label(self.prev_token.span, "item list ends here");
if is_unnecessary_semicolon {
err.span_suggestion_verbose(
semicolon_span,
"consider removing this semicolon",
"",
Applicability::MaybeIncorrect,
);
}
err.emit();
break;
}
Ok(Some(item)) => items.extend(item),

View File

@ -0,0 +1,7 @@
// run-rustfix
trait Foo {
fn bar() {} //~ ERROR non-item in item list
}
fn main() {}

View File

@ -0,0 +1,7 @@
// run-rustfix
trait Foo {
fn bar() {}; //~ ERROR non-item in item list
}
fn main() {}

View File

@ -0,0 +1,18 @@
error: non-item in item list
--> $DIR/suggest-removing-semicolon-after-impl-trait-items.rs:4:16
|
LL | trait Foo {
| - item list starts here
LL | fn bar() {};
| ^ non-item starts here
LL | }
| - item list ends here
|
help: consider removing this semicolon
|
LL - fn bar() {};
LL + fn bar() {}
|
error: aborting due to previous error