Fix comment formatting wrongly assuming false invariant.

This commit is contained in:
KtorZ 2023-03-16 23:33:02 +01:00 committed by Lucas
parent 57e217e81c
commit 660ca3fada
4 changed files with 43 additions and 48 deletions

View File

@ -1799,29 +1799,33 @@ fn printed_comments<'a, 'comments>(
let mut doc = Vec::new(); let mut doc = Vec::new();
while let Some(c) = comments.next() { while let Some(c) = comments.next() {
// There will never be consecutive empty lines (None values), match c {
// and whenever we peek a None, we advance past it. None => continue,
let c = c.expect("no consecutive empty lines"); Some(c) => {
doc.push("//".to_doc().append(Document::String(c.to_string()))); // There will never be consecutive empty lines (None values),
match comments.peek() { // and whenever we peek a None, we advance past it.
// Next line is a comment doc.push("//".to_doc().append(Document::String(c.to_string())));
Some(Some(_)) => doc.push(line()),
// Next line is empty
Some(None) => {
comments.next();
match comments.peek() { match comments.peek() {
Some(_) => doc.push(lines(2)), // Next line is a comment
None => { Some(Some(_)) => doc.push(line()),
if trailing_newline { // Next line is empty
doc.push(lines(2)); Some(None) => {
comments.next();
match comments.peek() {
Some(_) => doc.push(lines(2)),
None => {
if trailing_newline {
doc.push(lines(2));
}
}
}
}
// We've reached the end, there are no more lines
None => {
if trailing_newline {
doc.push(line());
} }
} }
}
}
// We've reached the end, there are no more lines
None => {
if trailing_newline {
doc.push(line());
} }
} }
} }

View File

@ -322,25 +322,6 @@ fn utf8_hex_literal_warning() {
)) ))
} }
#[test]
fn weird_comments() {
let source_code = r#"
// A
/// B
// C
fn foo () {
todo
}
"#;
assert!(matches!(
check(parse(source_code)),
Err((_, Error::UnexpectedDocComment { .. }))
))
}
#[test] #[test]
fn discarded_let_bindings() { fn discarded_let_bindings() {
let source_code = r#" let source_code = r#"

View File

@ -638,7 +638,16 @@ fn weird_comments() {
/// B /// B
/// C /// C
fn bar () { fn foo() {
todo
}
// E
/// F
// G
fn bar() {
todo todo
} }
"#}; "#};
@ -648,7 +657,15 @@ fn weird_comments() {
/// B /// B
/// C /// C
fn bar () { fn foo() {
todo
}
// E
// G
/// F
fn bar() {
todo todo
} }
"#}; "#};

View File

@ -892,13 +892,6 @@ The best thing to do from here is to remove it."#))]
#[label("{} arguments", if *count < 2 { "not enough" } else { "too many" })] #[label("{} arguments", if *count < 2 { "not enough" } else { "too many" })]
location: Span, location: Span,
}, },
#[error("I ran into a lonely doc comment not attached to any function definition.")]
#[diagnostic(code("unexpected::doc_comment"))]
UnexpectedDocComment {
#[label("unexpected doc comment")]
location: Span,
},
} }
impl Error { impl Error {