Skip to content

Commit 1f06a8b

Browse files
scampitopecongiro
authored andcommitted
fix extraction of missing comments when rewriting an empty where clause (#3663)
1 parent b6f7f24 commit 1f06a8b

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

src/comment.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -859,7 +859,9 @@ pub(crate) fn rewrite_missing_comment(
859859
) -> Option<String> {
860860
let missing_snippet = context.snippet(span);
861861
let trimmed_snippet = missing_snippet.trim();
862-
if !trimmed_snippet.is_empty() {
862+
// check the span starts with a comment
863+
let pos = trimmed_snippet.find('/');
864+
if !trimmed_snippet.is_empty() && pos.is_some() {
863865
rewrite_comment(trimmed_snippet, false, shape, context.config)
864866
} else {
865867
Some(String::new())
@@ -880,7 +882,7 @@ pub(crate) fn recover_missing_comment_in_span(
880882
Some(String::new())
881883
} else {
882884
let missing_snippet = context.snippet(span);
883-
let pos = missing_snippet.find('/').unwrap_or(0);
885+
let pos = missing_snippet.find('/')?;
884886
// 1 = ` `
885887
let total_width = missing_comment.len() + used_width + 1;
886888
let force_new_line_before_comment =

tests/source/issue-3639.rs

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
trait Foo where {}
2+
struct Bar where {}
3+
struct Bax where;
4+
struct Baz(String) where;
5+
impl<> Foo<> for Bar<> where {}

tests/target/issue-3639.rs

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
trait Foo {}
2+
struct Bar {}
3+
struct Bax;
4+
struct Baz(String);
5+
impl Foo for Bar {}

0 commit comments

Comments
 (0)