Skip to content

Commit

Permalink
Rollup merge of rust-lang#100446 - TaKO8Ki:suggest-removing-semicolon…
Browse files Browse the repository at this point in the history
…-after-impl-trait-items, r=compiler-errors

Suggest removing a semicolon after impl/trait items

fixes rust-lang#99822
  • Loading branch information
compiler-errors authored Aug 14, 2022
2 parents d46451c + d47df26 commit 86e1d1e
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 4 deletions.
38 changes: 34 additions & 4 deletions compiler/rustc_parse/src/parser/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -675,14 +675,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(
semicolon_span,
"consider removing this semicolon",
"",
Applicability::MaybeIncorrect,
);
}
err.emit();
break;
}
Ok(Some(item)) => items.extend(item),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// run-rustfix

trait Foo {
fn bar() {} //~ ERROR non-item in item list
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// run-rustfix

trait Foo {
fn bar() {}; //~ ERROR non-item in item list
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
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
| help: consider removing this semicolon
LL | }
| - item list ends here

error: aborting due to previous error

0 comments on commit 86e1d1e

Please sign in to comment.