From 1f06a8b361dda56defe67d7af56a406cd2fbae08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Campinas?= Date: Sun, 30 Jun 2019 05:19:24 +0200 Subject: [PATCH] fix extraction of missing comments when rewriting an empty where clause (#3663) --- src/comment.rs | 6 ++++-- tests/source/issue-3639.rs | 5 +++++ tests/target/issue-3639.rs | 5 +++++ 3 files changed, 14 insertions(+), 2 deletions(-) create mode 100644 tests/source/issue-3639.rs create mode 100644 tests/target/issue-3639.rs 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 {}