diff --git a/sway-lib-std/src/raw_ptr.sw b/sway-lib-std/src/raw_ptr.sw index 63aa2bfcdd7..d1735ae80db 100644 --- a/sway-lib-std/src/raw_ptr.sw +++ b/sway-lib-std/src/raw_ptr.sw @@ -163,19 +163,20 @@ impl raw_ptr { /// } /// ``` pub fn write(self, val: T) { - if __size_of::() == 0 { } else if __is_reference_type::() { - asm(dst: self, src: val, count: __size_of_val(val)) { - mcp dst src count; - }; - } else if __size_of::() == 1 { - asm(ptr: self, val: val) { - sb ptr val i0; - }; - } else { - asm(ptr: self, val: val) { - sw ptr val i0; - }; - } + if __size_of::() == 0 { + } else if __is_reference_type::() { + asm(dst: self, src: val, count: __size_of_val(val)) { + mcp dst src count; + }; + } else if __size_of::() == 1 { + asm(ptr: self, val: val) { + sb ptr val i0; + }; + } else { + asm(ptr: self, val: val) { + sw ptr val i0; + }; + } } /// Writes the given byte to the address. diff --git a/swayfmt/src/utils/language/expr/conditional.rs b/swayfmt/src/utils/language/expr/conditional.rs index b97d5896c47..9a2a41b4117 100644 --- a/swayfmt/src/utils/language/expr/conditional.rs +++ b/swayfmt/src/utils/language/expr/conditional.rs @@ -159,7 +159,13 @@ fn format_then_block( formatter, )?; if !comments { - formatter.shape.block_unindent(&formatter.config); + if if_expr.else_opt.is_some() + && formatter.shape.code_line.line_style != LineStyle::Inline + { + writeln!(formatted_code)?; + } else { + formatter.shape.block_unindent(&formatter.config); + } } } if if_expr.else_opt.is_none() { diff --git a/swayfmt/src/utils/language/expr/tests.rs b/swayfmt/src/utils/language/expr/tests.rs index 6472940e346..370ab4d074c 100644 --- a/swayfmt/src/utils/language/expr/tests.rs +++ b/swayfmt/src/utils/language/expr/tests.rs @@ -265,6 +265,16 @@ intermediate_whitespace let i = 42; }"); +fmt_test_expr!(empty_if_else_if +"if __size_of::() == 0 { +} else if __is_reference_type::() { + let i = 42; +}", +intermediate_whitespace +"if __size_of::() == 0 { } else if __is_reference_type::() { +let i = 42; +}"); + fmt_test_expr!(basic_for_loop "for iter in [0, 1, 7, 8, 15] { let i = 42; diff --git a/swayfmt/tests/mod.rs b/swayfmt/tests/mod.rs index c9276e42cc0..4ab0a9052b8 100644 --- a/swayfmt/tests/mod.rs +++ b/swayfmt/tests/mod.rs @@ -2113,6 +2113,52 @@ fn empty_if() { ); } +#[test] +fn empty_if_else_if() { + check( + indoc! {r#" + library; + fn test() { + if __size_of::() == 0 { } else if __is_reference_type::() { + let i = 42; + } + } + "#}, + indoc! {r#" + library; + fn test() { + if __size_of::() == 0 { + } else if __is_reference_type::() { + let i = 42; + } + } + "#}, + ); +} + +#[test] +fn empty_if_with_else() { + check( + indoc! {r#" + library; + fn test() { + if some_really_long_condition_name_here() == 0 { } else { + let really_long_variable_name = some_really_long_function_name(); + } + } + "#}, + indoc! {r#" + library; + fn test() { + if some_really_long_condition_name_here() == 0 { + } else { + let really_long_variable_name = some_really_long_function_name(); + } + } + "#}, + ); +} + #[test] fn bug_whitespace_added_after_comment() { check(