Rollup merge of #142341 - xizheyin:142311, r=fee1-dead

Don't suggest converting `///` to `//` when expecting `,`

Fixes rust-lang/rust#142311
This commit is contained in:
Jakub Beránek
2025-06-16 19:54:33 +02:00
committed by GitHub
3 changed files with 105 additions and 17 deletions

View File

@@ -686,23 +686,34 @@ impl<'a> Parser<'a> {
}
if let token::DocComment(kind, style, _) = self.token.kind {
// We have something like `expr //!val` where the user likely meant `expr // !val`
let pos = self.token.span.lo() + BytePos(2);
let span = self.token.span.with_lo(pos).with_hi(pos);
err.span_suggestion_verbose(
span,
format!(
"add a space before {} to write a regular comment",
match (kind, style) {
(token::CommentKind::Line, ast::AttrStyle::Inner) => "`!`",
(token::CommentKind::Block, ast::AttrStyle::Inner) => "`!`",
(token::CommentKind::Line, ast::AttrStyle::Outer) => "the last `/`",
(token::CommentKind::Block, ast::AttrStyle::Outer) => "the last `*`",
},
),
" ".to_string(),
Applicability::MachineApplicable,
);
// This is to avoid suggesting converting a doc comment to a regular comment
// when missing a comma before the doc comment in lists (#142311):
//
// ```
// enum Foo{
// A /// xxxxxxx
// B,
// }
// ```
if !expected.contains(&TokenType::Comma) {
// We have something like `expr //!val` where the user likely meant `expr // !val`
let pos = self.token.span.lo() + BytePos(2);
let span = self.token.span.with_lo(pos).with_hi(pos);
err.span_suggestion_verbose(
span,
format!(
"add a space before {} to write a regular comment",
match (kind, style) {
(token::CommentKind::Line, ast::AttrStyle::Inner) => "`!`",
(token::CommentKind::Block, ast::AttrStyle::Inner) => "`!`",
(token::CommentKind::Line, ast::AttrStyle::Outer) => "the last `/`",
(token::CommentKind::Block, ast::AttrStyle::Outer) => "the last `*`",
},
),
" ".to_string(),
Applicability::MaybeIncorrect,
);
}
}
let sp = if self.token == token::Eof {