Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 14 additions & 13 deletions sway-lib-std/src/raw_ptr.sw
Original file line number Diff line number Diff line change
Expand Up @@ -163,19 +163,20 @@ impl raw_ptr {
/// }
/// ```
pub fn write<T>(self, val: T) {
if __size_of::<T>() == 0 { } else if __is_reference_type::<T>() {
asm(dst: self, src: val, count: __size_of_val(val)) {
mcp dst src count;
};
} else if __size_of::<T>() == 1 {
asm(ptr: self, val: val) {
sb ptr val i0;
};
} else {
asm(ptr: self, val: val) {
sw ptr val i0;
};
}
if __size_of::<T>() == 0 {
} else if __is_reference_type::<T>() {
asm(dst: self, src: val, count: __size_of_val(val)) {
mcp dst src count;
};
} else if __size_of::<T>() == 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.
Expand Down
8 changes: 7 additions & 1 deletion swayfmt/src/utils/language/expr/conditional.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
10 changes: 10 additions & 0 deletions swayfmt/src/utils/language/expr/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,16 @@ intermediate_whitespace
let i = 42;
}");

fmt_test_expr!(empty_if_else_if
"if __size_of::<T>() == 0 {
} else if __is_reference_type::<T>() {
let i = 42;
}",
intermediate_whitespace
"if __size_of::<T>() == 0 { } else if __is_reference_type::<T>() {
let i = 42;
}");

fmt_test_expr!(basic_for_loop
"for iter in [0, 1, 7, 8, 15] {
let i = 42;
Expand Down
46 changes: 46 additions & 0 deletions swayfmt/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2113,6 +2113,52 @@ fn empty_if() {
);
}

#[test]
fn empty_if_else_if() {
check(
indoc! {r#"
library;
fn test() {
if __size_of::<T>() == 0 { } else if __is_reference_type::<T>() {
let i = 42;
}
}
"#},
indoc! {r#"
library;
fn test() {
if __size_of::<T>() == 0 {
} else if __is_reference_type::<T>() {
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(
Expand Down