From c3f8a46e5491fb6d6d9662ff46165ca087c9f60b Mon Sep 17 00:00:00 2001 From: Ary Borenszweig Date: Mon, 13 Jan 2025 17:20:18 -0300 Subject: [PATCH] fix(nargo_fmt): let doc comment could come after regular comment (#7046) --- noir_stdlib/src/collections/umap.nr | 3 +-- tooling/nargo_fmt/src/formatter/statement.rs | 22 ++++++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/noir_stdlib/src/collections/umap.nr b/noir_stdlib/src/collections/umap.nr index fb15d532bc5..bcb9759b4db 100644 --- a/noir_stdlib/src/collections/umap.nr +++ b/noir_stdlib/src/collections/umap.nr @@ -114,8 +114,7 @@ impl UHashMap { { // docs:end:contains_key /// Safety: unconstrained context - unsafe { self.get(key) } - .is_some() + unsafe { self.get(key) }.is_some() } // Returns true if the map contains no elements. diff --git a/tooling/nargo_fmt/src/formatter/statement.rs b/tooling/nargo_fmt/src/formatter/statement.rs index 1736197403c..9e3ea652ec6 100644 --- a/tooling/nargo_fmt/src/formatter/statement.rs +++ b/tooling/nargo_fmt/src/formatter/statement.rs @@ -23,10 +23,17 @@ impl<'a, 'b> ChunkFormatter<'a, 'b> { // Now write any leading comment respecting multiple newlines after them group.leading_comment(self.chunk(|formatter| { + // Doc comments for a let statement could come before a potential non-doc comment if formatter.token.kind() == TokenKind::OuterDocComment { formatter.format_outer_doc_comments(); } + formatter.skip_comments_and_whitespace_writing_multiple_lines_if_found(); + + // Or doc comments could come after a potential non-doc comment + if formatter.token.kind() == TokenKind::OuterDocComment { + formatter.format_outer_doc_comments(); + } })); ignore_next |= self.ignore_next; @@ -390,6 +397,21 @@ mod tests { assert_format(src, expected); } + #[test] + fn format_let_statement_with_unsafe_and_comment_before_it() { + let src = " fn foo() { + // Some comment + /// Safety: some doc + let x = unsafe { 1 } ; } "; + let expected = "fn foo() { + // Some comment + /// Safety: some doc + let x = unsafe { 1 }; +} +"; + assert_format(src, expected); + } + #[test] fn format_assign() { let src = " fn foo() { x = 2 ; } ";