Skip to content

Commit 1c619e4

Browse files
committed
Dont suggest converting /// to regular comment when it appears after missing , in list
Signed-off-by: xizheyin <[email protected]>
1 parent f48df4a commit 1c619e4

File tree

2 files changed

+37
-51
lines changed

2 files changed

+37
-51
lines changed

compiler/rustc_parse/src/parser/diagnostics.rs

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -686,23 +686,35 @@ 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 adding suggestions for doc comments in list.
690+
// For example,
691+
// `enum Foo {
692+
// Bar ///baz
693+
// Baz }`
694+
// or `[
695+
// expr ///baz
696+
// expr2
697+
// ]`
698+
// should suggest to add a comma before `///baz`.
699+
if !expected.contains(&TokenType::Comma) {
700+
// We have something like `expr //!val` where the user likely meant `expr // !val`
701+
let pos = self.token.span.lo() + BytePos(2);
702+
let span = self.token.span.with_lo(pos).with_hi(pos);
703+
err.span_suggestion_verbose(
704+
span,
705+
format!(
706+
"add a space before {} to write a regular comment",
707+
match (kind, style) {
708+
(token::CommentKind::Line, ast::AttrStyle::Inner) => "`!`",
709+
(token::CommentKind::Block, ast::AttrStyle::Inner) => "`!`",
710+
(token::CommentKind::Line, ast::AttrStyle::Outer) => "the last `/`",
711+
(token::CommentKind::Block, ast::AttrStyle::Outer) => "the last `*`",
712+
},
713+
),
714+
" ".to_string(),
715+
Applicability::MaybeIncorrect,
716+
);
717+
}
706718
}
707719

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

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

Lines changed: 8 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,21 @@ error: expected one of `(`, `,`, `=`, `{`, or `}`, found doc comment `/// Like w
22
--> $DIR/doc-comment-after-missing-comma-issue-142311.rs:10:5
33
|
44
LL | Bar
5-
| - expected one of `(`, `,`, `=`, `{`, or `}`
5+
| -
6+
| |
7+
| expected one of `(`, `,`, `=`, `{`, or `}`
8+
| help: missing `,`
69
LL | /// Like where people drink
710
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ unexpected token
8-
|
9-
help: add a space before the last `/` to write a regular comment
10-
|
11-
LL | // / Like where people drink
12-
| +
13-
help: missing `,`
14-
|
15-
LL | Bar,
16-
| +
1711

1812
error: expected one of `(`, `,`, `=`, `{`, or `}`, found doc comment `///xxxxxx`
1913
--> $DIR/doc-comment-after-missing-comma-issue-142311.rs:12:8
2014
|
2115
LL | Baa///xxxxxx
22-
| ^^^^^^^^^ expected one of `(`, `,`, `=`, `{`, or `}`
23-
|
24-
help: add a space before the last `/` to write a regular comment
25-
|
26-
LL | Baa// /xxxxxx
27-
| +
28-
help: missing `,`
29-
|
30-
LL | Baa,///xxxxxx
31-
| +
16+
| -^^^^^^^^
17+
| |
18+
| expected one of `(`, `,`, `=`, `{`, or `}`
19+
| help: missing `,`
3220

3321
error: expected one of `(`, `,`, `=`, `{`, or `}`, found doc comment `///xxxxxx`
3422
--> $DIR/doc-comment-after-missing-comma-issue-142311.rs:14:8
@@ -38,32 +26,18 @@ LL | Baz///xxxxxx
3826
|
3927
= help: doc comments must come before what they document, if a comment was intended use `//`
4028
= 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-
|
43-
LL | Baz// /xxxxxx
44-
| +
4529

4630
error: expected one of `,`, `.`, `;`, `?`, `]`, or an operator, found doc comment `///xxxxxx`
4731
--> $DIR/doc-comment-after-missing-comma-issue-142311.rs:20:10
4832
|
4933
LL | 1///xxxxxx
5034
| ^^^^^^^^^ expected one of `,`, `.`, `;`, `?`, `]`, or an operator
51-
|
52-
help: add a space before the last `/` to write a regular comment
53-
|
54-
LL | 1// /xxxxxx
55-
| +
5635

5736
error: expected one of `,`, `.`, `?`, `]`, or an operator, found doc comment `///xxxxxx`
5837
--> $DIR/doc-comment-after-missing-comma-issue-142311.rs:29:10
5938
|
6039
LL | 2///xxxxxx
6140
| ^^^^^^^^^ expected one of `,`, `.`, `?`, `]`, or an operator
62-
|
63-
help: add a space before the last `/` to write a regular comment
64-
|
65-
LL | 2// /xxxxxx
66-
| +
6741

6842
error: aborting due to 5 previous errors
6943

0 commit comments

Comments
 (0)