Fixed an integer overflow in zig fmt and added testcase

This commit is contained in:
Timon Kruiper 2019-06-14 17:05:16 +02:00 committed by Andrew Kelley
parent 57aa8997bd
commit 59850c1ce1
2 changed files with 33 additions and 1 deletions

View File

@ -2267,6 +2267,32 @@ test "zig fmt: file ends with struct field" {
);
}
test "zig fmt: comments at several places in struct init" {
try testTransform(
\\var bar = Bar{
\\ .x = 10, // test
\\ .y = "test"
\\ // test
\\};
\\
,
\\var bar = Bar{
\\ .x = 10, // test
\\ .y = "test", // test
\\};
\\
);
try testCanonical(
\\var bar = Bar{ // test
\\ .x = 10, // test
\\ .y = "test",
\\ // test
\\};
\\
);
}
const std = @import("std");
const mem = std.mem;
const warn = std.debug.warn;

View File

@ -2020,7 +2020,13 @@ fn renderTokenOffset(
const after_comment_token = tree.tokens.at(token_index + offset);
const next_line_indent = switch (after_comment_token.id) {
Token.Id.RParen, Token.Id.RBrace, Token.Id.RBracket => indent - indent_delta,
Token.Id.RParen, Token.Id.RBrace, Token.Id.RBracket => blk: {
if (indent > indent_delta) {
break :blk indent - indent_delta;
} else {
break :blk 0;
}
},
else => indent,
};
try stream.writeByteNTimes(' ', next_line_indent);