authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-08-30 15:48:07-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2019-08-30 15:48:07-04:00
log2148943fff5af1e31f75b1e6651f0b7614e7e36a
tree28b57538d63b452532f8d37ddf87be22bd009245
parentfed5c12d9a2ab14c5bb66251ba6ee3e356bce8d3
parent19964f5dc80792eb50cb3a45bdabe887a92d92cc
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #3139 from hspak/zig-fmt-nested-if

zig fmt: fix nested if

3 files changed, 32 insertions(+), 8 deletions(-)

std/special/compiler_rt/comparetf2.zig+7-6
......@@ -38,12 +38,14 @@ pub extern fn __letf2(a: f128, b: f128) c_int {
3838
3939 // If at least one of a and b is positive, we get the same result comparing
4040 // a and b as signed integers as we would with a floating-point compare.
41 return if ((aInt & bInt) >= 0) if (aInt < bInt)
42 LE_LESS
43 else if (aInt == bInt)
44 LE_EQUAL
41 return if ((aInt & bInt) >= 0)
42 if (aInt < bInt)
43 LE_LESS
44 else if (aInt == bInt)
45 LE_EQUAL
46 else
47 LE_GREATER
4548 else
46 LE_GREATER else
4749 // Otherwise, both are negative, so we need to flip the sense of the
4850 // comparison to get the correct result. (This assumes a twos- or ones-
4951 // complement integer representation; if integers are represented in a
......@@ -73,7 +75,6 @@ pub extern fn __getf2(a: f128, b: f128) c_int {
7375
7476 if (aAbs > infRep or bAbs > infRep) return GE_UNORDERED;
7577 if ((aAbs | bAbs) == 0) return GE_EQUAL;
76 // zig fmt issue here, see https://github.com/ziglang/zig/issues/2661
7778 return if ((aInt & bInt) >= 0)
7879 if (aInt < bInt)
7980 GE_LESS
std/zig/parser_test.zig+21
......@@ -482,6 +482,27 @@ test "zig fmt: if-else with comment before else" {
482482 );
483483}
484484
485test "zig fmt: if nested" {
486 try testCanonical(
487 \\pub fn foo() void {
488 \\ return if ((aInt & bInt) >= 0)
489 \\ if (aInt < bInt)
490 \\ GE_LESS
491 \\ else if (aInt == bInt)
492 \\ GE_EQUAL
493 \\ else
494 \\ GE_GREATER
495 \\ else if (aInt > bInt)
496 \\ GE_LESS
497 \\ else if (aInt == bInt)
498 \\ GE_EQUAL
499 \\ else
500 \\ GE_GREATER;
501 \\}
502 \\
503 );
504}
505
485506test "zig fmt: respect line breaks in if-else" {
486507 try testCanonical(
487508 \\comptime {
std/zig/render.zig+4-2
......@@ -276,7 +276,6 @@ fn renderTopLevelDecl(allocator: *mem.Allocator, stream: var, tree: *ast.Tree, i
276276 } else {
277277 try renderExpression(allocator, stream, tree, indent, start_col, field.type_expr.?, Space.Comma); // type,
278278 }
279
280279 } else if (field.type_expr == null and field.value_expr != null) {
281280 try renderToken(tree, stream, field.name_token, indent, start_col, Space.Space); // name
282281 try renderToken(tree, stream, tree.nextToken(field.name_token), indent, start_col, Space.Space); // =
......@@ -1521,9 +1520,12 @@ fn renderExpression(
15211520
15221521 try renderExpression(allocator, stream, tree, indent, start_col, if_node.condition, Space.None); // condition
15231522
1523 const body_is_if_block = if_node.body.id == ast.Node.Id.If;
15241524 const body_is_block = nodeIsBlock(if_node.body);
15251525
1526 if (body_is_block) {
1526 if (body_is_if_block) {
1527 try renderExtraNewline(tree, stream, start_col, if_node.body);
1528 } else if (body_is_block) {
15271529 const after_rparen_space = if (if_node.payload == null) Space.BlockStart else Space.Space;
15281530 try renderToken(tree, stream, rparen, indent, start_col, after_rparen_space); // )
15291531