mirror of
https://github.com/rust-lang/rust
synced 2024-10-14 12:33:57 +00:00
Move where doc comment meant as comment check
The new place makes more sense and covers more cases beyond individual statements. ``` error: expected one of `.`, `;`, `?`, `else`, or an operator, found doc comment `//!foo --> $DIR/doc-comment-in-stmt.rs:25:22 | LL | let y = x.max(1) //!foo | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected one of `.`, `;`, `?`, `else`, or an operator | help: add a space before `!` to write a regular comment | LL | let y = x.max(1) // !foo | + ``` Fix #65329.
This commit is contained in:
parent
cc705b8012
commit
20de5c762d
|
@ -39,7 +39,7 @@
|
||||||
use rustc_session::errors::ExprParenthesesNeeded;
|
use rustc_session::errors::ExprParenthesesNeeded;
|
||||||
use rustc_span::source_map::Spanned;
|
use rustc_span::source_map::Spanned;
|
||||||
use rustc_span::symbol::{kw, sym, Ident};
|
use rustc_span::symbol::{kw, sym, Ident};
|
||||||
use rustc_span::{Span, SpanSnippetError, Symbol, DUMMY_SP};
|
use rustc_span::{BytePos, Span, SpanSnippetError, Symbol, DUMMY_SP};
|
||||||
use std::mem::take;
|
use std::mem::take;
|
||||||
use std::ops::{Deref, DerefMut};
|
use std::ops::{Deref, DerefMut};
|
||||||
use thin_vec::{thin_vec, ThinVec};
|
use thin_vec::{thin_vec, ThinVec};
|
||||||
|
@ -648,6 +648,26 @@ fn is_ident_eq_keyword(found: &TokenKind, expected: &TokenType) -> bool {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
// Add suggestion for a missing closing angle bracket if '>' is included in expected_tokens
|
// Add suggestion for a missing closing angle bracket if '>' is included in expected_tokens
|
||||||
// there are unclosed angle brackets
|
// there are unclosed angle brackets
|
||||||
if self.unmatched_angle_bracket_count > 0
|
if self.unmatched_angle_bracket_count > 0
|
||||||
|
|
|
@ -632,23 +632,6 @@ pub fn parse_full_stmt(
|
||||||
// Recover from parser, skip type error to avoid extra errors.
|
// Recover from parser, skip type error to avoid extra errors.
|
||||||
Ok(true) => true,
|
Ok(true) => true,
|
||||||
Err(mut e) => {
|
Err(mut e) => {
|
||||||
if let TokenKind::DocComment(..) = self.token.kind
|
|
||||||
&& let Ok(snippet) = self.span_to_snippet(self.token.span)
|
|
||||||
{
|
|
||||||
let sp = self.token.span;
|
|
||||||
let marker = &snippet[..3];
|
|
||||||
let (comment_marker, doc_comment_marker) = marker.split_at(2);
|
|
||||||
|
|
||||||
e.span_suggestion(
|
|
||||||
sp.with_hi(sp.lo() + BytePos(marker.len() as u32)),
|
|
||||||
format!(
|
|
||||||
"add a space before `{doc_comment_marker}` to use a regular comment",
|
|
||||||
),
|
|
||||||
format!("{comment_marker} {doc_comment_marker}"),
|
|
||||||
Applicability::MaybeIncorrect,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
if self.recover_colon_as_semi() {
|
if self.recover_colon_as_semi() {
|
||||||
// recover_colon_as_semi has already emitted a nicer error.
|
// recover_colon_as_semi has already emitted a nicer error.
|
||||||
e.delay_as_bug();
|
e.delay_as_bug();
|
||||||
|
|
27
tests/ui/parser/doc-comment-in-stmt.fixed
Normal file
27
tests/ui/parser/doc-comment-in-stmt.fixed
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
// run-rustfix
|
||||||
|
#![allow(unused)]
|
||||||
|
fn foo() -> bool {
|
||||||
|
false
|
||||||
|
// !self.allow_ty_infer()
|
||||||
|
//~^ ERROR found doc comment
|
||||||
|
}
|
||||||
|
|
||||||
|
fn bar() -> bool {
|
||||||
|
false
|
||||||
|
/* ! bar */ //~ ERROR found doc comment
|
||||||
|
}
|
||||||
|
|
||||||
|
fn baz() -> i32 {
|
||||||
|
1 /* * baz */ //~ ERROR found doc comment
|
||||||
|
}
|
||||||
|
|
||||||
|
fn quux() -> i32 {
|
||||||
|
2 // / quux
|
||||||
|
//~^ ERROR found doc comment
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let x = 0;
|
||||||
|
let y = x.max(1) // !foo //~ ERROR found doc comment
|
||||||
|
.min(2);
|
||||||
|
}
|
|
@ -1,3 +1,5 @@
|
||||||
|
// run-rustfix
|
||||||
|
#![allow(unused)]
|
||||||
fn foo() -> bool {
|
fn foo() -> bool {
|
||||||
false
|
false
|
||||||
//!self.allow_ty_infer()
|
//!self.allow_ty_infer()
|
||||||
|
@ -14,7 +16,12 @@ fn baz() -> i32 {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn quux() -> i32 {
|
fn quux() -> i32 {
|
||||||
2 /*! quux */ //~ ERROR found doc comment
|
2 /// quux
|
||||||
|
//~^ ERROR found doc comment
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {}
|
fn main() {
|
||||||
|
let x = 0;
|
||||||
|
let y = x.max(1) //!foo //~ ERROR found doc comment
|
||||||
|
.min(2);
|
||||||
|
}
|
||||||
|
|
|
@ -1,50 +1,61 @@
|
||||||
error: expected one of `.`, `;`, `?`, `}`, or an operator, found doc comment `//!self.allow_ty_infer()`
|
error: expected one of `.`, `;`, `?`, `}`, or an operator, found doc comment `//!self.allow_ty_infer()`
|
||||||
--> $DIR/doc-comment-in-stmt.rs:3:5
|
--> $DIR/doc-comment-in-stmt.rs:5:5
|
||||||
|
|
|
|
||||||
LL | false
|
LL | false
|
||||||
| - expected one of `.`, `;`, `?`, `}`, or an operator
|
| - expected one of `.`, `;`, `?`, `}`, or an operator
|
||||||
LL | //!self.allow_ty_infer()
|
LL | //!self.allow_ty_infer()
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^^^^ unexpected token
|
| ^^^^^^^^^^^^^^^^^^^^^^^^ unexpected token
|
||||||
|
|
|
|
||||||
help: add a space before `!` to use a regular comment
|
help: add a space before `!` to write a regular comment
|
||||||
|
|
|
|
||||||
LL | // !self.allow_ty_infer()
|
LL | // !self.allow_ty_infer()
|
||||||
| ~~~~
|
| +
|
||||||
|
|
||||||
error: expected one of `.`, `;`, `?`, `}`, or an operator, found doc comment `/*! bar */`
|
error: expected one of `.`, `;`, `?`, `}`, or an operator, found doc comment `/*! bar */`
|
||||||
--> $DIR/doc-comment-in-stmt.rs:9:5
|
--> $DIR/doc-comment-in-stmt.rs:11:5
|
||||||
|
|
|
|
||||||
LL | false
|
LL | false
|
||||||
| - expected one of `.`, `;`, `?`, `}`, or an operator
|
| - expected one of `.`, `;`, `?`, `}`, or an operator
|
||||||
LL | /*! bar */
|
LL | /*! bar */
|
||||||
| ^^^^^^^^^^ unexpected token
|
| ^^^^^^^^^^ unexpected token
|
||||||
|
|
|
|
||||||
help: add a space before `!` to use a regular comment
|
help: add a space before `!` to write a regular comment
|
||||||
|
|
|
|
||||||
LL | /* ! bar */
|
LL | /* ! bar */
|
||||||
| ~~~~
|
| +
|
||||||
|
|
||||||
error: expected one of `.`, `;`, `?`, `}`, or an operator, found doc comment `/** baz */`
|
error: expected one of `.`, `;`, `?`, `}`, or an operator, found doc comment `/** baz */`
|
||||||
--> $DIR/doc-comment-in-stmt.rs:13:7
|
--> $DIR/doc-comment-in-stmt.rs:15:7
|
||||||
|
|
|
|
||||||
LL | 1 /** baz */
|
LL | 1 /** baz */
|
||||||
| ^^^^^^^^^^ expected one of `.`, `;`, `?`, `}`, or an operator
|
| ^^^^^^^^^^ expected one of `.`, `;`, `?`, `}`, or an operator
|
||||||
|
|
|
|
||||||
help: add a space before `*` to use a regular comment
|
help: add a space before the last `*` to write a regular comment
|
||||||
|
|
|
|
||||||
LL | 1 /* * baz */
|
LL | 1 /* * baz */
|
||||||
| ~~~~
|
| +
|
||||||
|
|
||||||
error: expected one of `.`, `;`, `?`, `}`, or an operator, found doc comment `/*! quux */`
|
error: expected one of `.`, `;`, `?`, `}`, or an operator, found doc comment `/// quux`
|
||||||
--> $DIR/doc-comment-in-stmt.rs:17:7
|
--> $DIR/doc-comment-in-stmt.rs:19:7
|
||||||
|
|
|
|
||||||
LL | 2 /*! quux */
|
LL | 2 /// quux
|
||||||
| ^^^^^^^^^^^ expected one of `.`, `;`, `?`, `}`, or an operator
|
| ^^^^^^^^ expected one of `.`, `;`, `?`, `}`, or an operator
|
||||||
|
|
|
|
||||||
help: add a space before `!` to use a regular comment
|
help: add a space before the last `/` to write a regular comment
|
||||||
|
|
|
|
||||||
LL | 2 /* ! quux */
|
LL | 2 // / quux
|
||||||
| ~~~~
|
| +
|
||||||
|
|
||||||
error: aborting due to 4 previous errors
|
error: expected one of `.`, `;`, `?`, `else`, or an operator, found doc comment `//!foo
|
||||||
|
--> $DIR/doc-comment-in-stmt.rs:25:22
|
||||||
|
|
|
||||||
|
LL | let y = x.max(1) //!foo
|
||||||
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected one of `.`, `;`, `?`, `else`, or an operator
|
||||||
|
|
|
||||||
|
help: add a space before `!` to write a regular comment
|
||||||
|
|
|
||||||
|
LL | let y = x.max(1) // !foo
|
||||||
|
| +
|
||||||
|
|
||||||
|
error: aborting due to 5 previous errors
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue