Skip to content

Commit a7fc4ad

Browse files
giacomocavalierilpil
authored andcommitted
fix assert formatting bug
1 parent e6c2ef1 commit a7fc4ad

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
# Changelog
22

3+
## Unreleased
4+
5+
### Bug fixes
6+
7+
- Fixed a bug where the formatter would move a comment before `assert` to be
8+
after it.
9+
([Giacomo Cavalieri](https://github.com/giacomocavalieri))
10+
311
## v1.12.0-rc1 - 2025-07-18
412

513
### Compiler

compiler-core/src/format.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2573,14 +2573,16 @@ impl<'comments> Formatter<'comments> {
25732573
}
25742574

25752575
fn assert<'a>(&mut self, assert: &'a UntypedAssert) -> Document<'a> {
2576+
let comments = self.pop_comments(assert.location.start);
2577+
25762578
let expression = if assert.value.is_binop() || assert.value.is_pipeline() {
25772579
self.expr(&assert.value).nest(INDENT)
25782580
} else {
25792581
self.expr(&assert.value)
25802582
};
25812583

25822584
let doc = self.append_as_message(expression, assert.message.as_ref());
2583-
docvec!["assert ", doc]
2585+
commented(docvec!["assert ", doc], comments)
25842586
}
25852587

25862588
fn bit_array<'a>(

compiler-core/src/format/tests.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6490,3 +6490,14 @@ fn assert_with_long_binary_expression() {
64906490
"#
64916491
);
64926492
}
6493+
6494+
#[test]
6495+
fn comment_is_not_moved_after_assert() {
6496+
assert_format!(
6497+
"pub fn main() {
6498+
// Wibble!
6499+
assert True
6500+
}
6501+
"
6502+
);
6503+
}

0 commit comments

Comments
 (0)