Skip to content

Commit 9a3b2ce

Browse files
committed
Suggest add missing , before doc comments for E0585 in parsing item list
Signed-off-by: xizheyin <[email protected]>
1 parent 805ce86 commit 9a3b2ce

File tree

4 files changed

+86
-46
lines changed

4 files changed

+86
-46
lines changed

compiler/rustc_parse/src/parser/diagnostics.rs

Lines changed: 43 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -686,23 +686,49 @@ impl<'a> Parser<'a> {
686686
}
687687

688688
if let token::DocComment(kind, style, _) = self.token.kind {
689-
// We have something like `expr //!val` where the user likely meant `expr // !val`
690-
let pos = self.token.span.lo() + BytePos(2);
691-
let span = self.token.span.with_lo(pos).with_hi(pos);
692-
err.span_suggestion_verbose(
693-
span,
694-
format!(
695-
"add a space before {} to write a regular comment",
696-
match (kind, style) {
697-
(token::CommentKind::Line, ast::AttrStyle::Inner) => "`!`",
698-
(token::CommentKind::Block, ast::AttrStyle::Inner) => "`!`",
699-
(token::CommentKind::Line, ast::AttrStyle::Outer) => "the last `/`",
700-
(token::CommentKind::Block, ast::AttrStyle::Outer) => "the last `*`",
701-
},
702-
),
703-
" ".to_string(),
704-
Applicability::MachineApplicable,
705-
);
689+
// This is used to avoid adding suggestions for doc comments in item list.
690+
// For example,
691+
// `enum Foo {
692+
// Bar ///baz
693+
// Baz }`
694+
// should suggest to add a comma before `///baz`.
695+
let is_expect_item_list_comma =
696+
edible.len() == 1 && edible.iter().any(|e| e.token_type == TokenType::Comma);
697+
if !is_expect_item_list_comma {
698+
// We have something like `expr //!val` where the user likely meant `expr // !val`
699+
let pos = self.token.span.lo() + BytePos(2);
700+
let span = self.token.span.with_lo(pos).with_hi(pos);
701+
err.span_suggestion_verbose(
702+
span,
703+
format!(
704+
"add a space before {} to write a regular comment",
705+
match (kind, style) {
706+
(token::CommentKind::Line, ast::AttrStyle::Inner) => "`!`",
707+
(token::CommentKind::Block, ast::AttrStyle::Inner) => "`!`",
708+
(token::CommentKind::Line, ast::AttrStyle::Outer) => "the last `/`",
709+
(token::CommentKind::Block, ast::AttrStyle::Outer) => "the last `*`",
710+
},
711+
),
712+
" ".to_string(),
713+
Applicability::MaybeIncorrect,
714+
);
715+
} else {
716+
// For fine-grained suggestions, we should suggest to add a comma before the doc comment
717+
// like `expr, //!val`
718+
err.cancel();
719+
err = self.dcx().struct_span_err(
720+
self.token.span,
721+
"found a documentation comment that doesn't document anything",
722+
);
723+
err.code(rustc_errors::E0585);
724+
err.span_label(self.token.span, "this doc comment doesn't document anything");
725+
err.span_suggestion_verbose(
726+
self.prev_token.span.shrink_to_hi(),
727+
"missing `,` before the doc comment",
728+
", ".to_string(),
729+
Applicability::MachineApplicable,
730+
);
731+
}
706732
}
707733

708734
let sp = if self.token == token::Eof {

compiler/rustc_parse/src/parser/mod.rs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -918,15 +918,27 @@ impl<'a> Parser<'a> {
918918
Ok(t) => {
919919
// Parsed successfully, therefore most probably the code only
920920
// misses a separator.
921-
expect_err
922-
.with_span_suggestion_short(
921+
922+
// This is to avoid adding suggestions twice for doc comments in item list.
923+
let contains_missing_comma_suggestion =
924+
expect_err.suggestions.clone().unwrap_tag().iter().any(
925+
|s| {
926+
if let Some(msg) = s.msg.as_str() {
927+
msg.contains("missing `,`")
928+
} else {
929+
false
930+
}
931+
},
932+
);
933+
if !contains_missing_comma_suggestion {
934+
expect_err.span_suggestion_short(
923935
sp,
924936
format!("missing `{token_str}`"),
925937
token_str,
926938
Applicability::MaybeIncorrect,
927-
)
928-
.emit();
929-
939+
);
940+
}
941+
expect_err.emit();
930942
v.push(t);
931943
continue;
932944
}

tests/ui/parser/doc-comment-after-missing-comma-issue-142311.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ enum Foo {
88
/// Like the noise a sheep makes
99
Bar
1010
/// Like where people drink
11-
//~^ ERROR expected one of `(`, `,`, `=`, `{`, or `}`, found doc comment `/// Like where people drink`
11+
//~^ ERROR found a documentation comment that doesn't document anything [E0585]
1212
Baa///xxxxxx
13-
//~^ ERROR expected one of `(`, `,`, `=`, `{`, or `}`, found doc comment `///xxxxxx`
13+
//~^ ERROR found a documentation comment that doesn't document anything [E0585]
1414
Baz///xxxxxx
15-
//~^ ERROR expected one of `(`, `,`, `=`, `{`, or `}`, found doc comment `///xxxxxx`
15+
//~^ ERROR found a documentation comment that doesn't document anything [E0585]
1616
}
1717

1818
fn main() {}
Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,49 @@
1-
error: expected one of `(`, `,`, `=`, `{`, or `}`, found doc comment `/// Like where people drink`
1+
error[E0585]: found a documentation comment that doesn't document anything
22
--> $DIR/doc-comment-after-missing-comma-issue-142311.rs:10:5
33
|
44
LL | Bar
55
| - expected one of `(`, `,`, `=`, `{`, or `}`
66
LL | /// Like where people drink
7-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ unexpected token
7+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
8+
| |
9+
| this doc comment doesn't document anything
10+
| unexpected token
811
|
9-
help: add a space before the last `/` to write a regular comment
12+
help: missing `,` before the doc comment
1013
|
11-
LL | // / Like where people drink
12-
| +
13-
help: missing `,`
14-
|
15-
LL | Bar,
14+
LL | Bar,
1615
| +
1716

18-
error: expected one of `(`, `,`, `=`, `{`, or `}`, found doc comment `///xxxxxx`
17+
error[E0585]: found a documentation comment that doesn't document anything
1918
--> $DIR/doc-comment-after-missing-comma-issue-142311.rs:12:8
2019
|
2120
LL | Baa///xxxxxx
22-
| ^^^^^^^^^ expected one of `(`, `,`, `=`, `{`, or `}`
23-
|
24-
help: add a space before the last `/` to write a regular comment
21+
| ^^^^^^^^^
22+
| |
23+
| this doc comment doesn't document anything
24+
| expected one of `(`, `,`, `=`, `{`, or `}`
2525
|
26-
LL | Baa// /xxxxxx
27-
| +
28-
help: missing `,`
26+
help: missing `,` before the doc comment
2927
|
30-
LL | Baa,///xxxxxx
28+
LL | Baa, ///xxxxxx
3129
| +
3230

33-
error: expected one of `(`, `,`, `=`, `{`, or `}`, found doc comment `///xxxxxx`
31+
error[E0585]: found a documentation comment that doesn't document anything
3432
--> $DIR/doc-comment-after-missing-comma-issue-142311.rs:14:8
3533
|
3634
LL | Baz///xxxxxx
37-
| ^^^^^^^^^ expected one of `(`, `,`, `=`, `{`, or `}`
35+
| ^^^^^^^^^
36+
| |
37+
| this doc comment doesn't document anything
38+
| expected one of `(`, `,`, `=`, `{`, or `}`
3839
|
3940
= help: doc comments must come before what they document, if a comment was intended use `//`
4041
= help: enum variants can be `Variant`, `Variant = <integer>`, `Variant(Type, ..., TypeN)` or `Variant { fields: Types }`
41-
help: add a space before the last `/` to write a regular comment
42+
help: missing `,` before the doc comment
4243
|
43-
LL | Baz// /xxxxxx
44-
| +
44+
LL | Baz, ///xxxxxx
45+
| +
4546

4647
error: aborting due to 3 previous errors
4748

49+
For more information about this error, try `rustc --explain E0585`.

0 commit comments

Comments
 (0)