diff --git a/src/comment.rs b/src/comment.rs index 232d3745e59..6222c34b1a2 100644 --- a/src/comment.rs +++ b/src/comment.rs @@ -859,7 +859,9 @@ pub(crate) fn rewrite_missing_comment( ) -> Option { let missing_snippet = context.snippet(span); let trimmed_snippet = missing_snippet.trim(); - if !trimmed_snippet.is_empty() { + // check the span starts with a comment + let pos = trimmed_snippet.find('/'); + if !trimmed_snippet.is_empty() && pos.is_some() { rewrite_comment(trimmed_snippet, false, shape, context.config) } else { Some(String::new()) @@ -880,7 +882,7 @@ pub(crate) fn recover_missing_comment_in_span( Some(String::new()) } else { let missing_snippet = context.snippet(span); - let pos = missing_snippet.find('/').unwrap_or(0); + let pos = missing_snippet.find('/')?; // 1 = ` ` let total_width = missing_comment.len() + used_width + 1; let force_new_line_before_comment = diff --git a/tests/source/issue-3639.rs b/tests/source/issue-3639.rs new file mode 100644 index 00000000000..7b16b2dfde0 --- /dev/null +++ b/tests/source/issue-3639.rs @@ -0,0 +1,5 @@ +trait Foo where {} +struct Bar where {} +struct Bax where; +struct Baz(String) where; +impl<> Foo<> for Bar<> where {} diff --git a/tests/target/issue-3639.rs b/tests/target/issue-3639.rs new file mode 100644 index 00000000000..e8fddce2de6 --- /dev/null +++ b/tests/target/issue-3639.rs @@ -0,0 +1,5 @@ +trait Foo {} +struct Bar {} +struct Bax; +struct Baz(String); +impl Foo for Bar {}