Skip to content

Commit 5793f41

Browse files
rchaser53topecongiro
authored andcommitted
fix internal error when stmt has doc attribute (#3985)
1 parent 0938d37 commit 5793f41

File tree

4 files changed

+19
-2
lines changed

4 files changed

+19
-2
lines changed

src/comment.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1565,7 +1565,8 @@ pub(crate) fn recover_comment_removed(
15651565
context: &RewriteContext<'_>,
15661566
) -> Option<String> {
15671567
let snippet = context.snippet(span);
1568-
if snippet != new && changed_comment_content(snippet, &new) {
1568+
let includes_comment = contains_comment(snippet);
1569+
if snippet != new && includes_comment && changed_comment_content(snippet, &new) {
15691570
// We missed some comments. Warn and keep the original text.
15701571
if context.config.error_on_unformatted() {
15711572
context.report.append(

src/stmt.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,5 +107,7 @@ fn format_stmt(
107107
}
108108
ast::StmtKind::Mac(..) | ast::StmtKind::Item(..) => None,
109109
};
110-
result.and_then(|res| recover_comment_removed(res, stmt.span(), context))
110+
result.and_then(|res| {
111+
recover_comment_removed(res, stmt.span(), context)
112+
})
111113
}

tests/source/issue-3981.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// rustfmt-normalize_doc_attributes: true
2+
3+
#[doc = " item"]
4+
fn main() {
5+
#[doc = " stmt"]
6+
let _=( );
7+
}

tests/target/issue-3981.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// rustfmt-normalize_doc_attributes: true
2+
3+
/// item
4+
fn main() {
5+
/// stmt
6+
let _ = ();
7+
}

0 commit comments

Comments
 (0)