zig icon indicating copy to clipboard operation
zig copied to clipboard

Proposal: add the --simplify option to zig fmt

Open perillo opened this issue 1 year ago • 1 comments

gofmt supports the -s code to simplify code. The details are available at https://github.com/golang/go/blob/master/src/cmd/gofmt/simplify.go.

I propose to add the --simplify flag to zig fmt.

Here a possible list of simplifications:

  • Use struct literals when possible
  • Replace s[a..len] with s[a..]
  • Use tuples when creating a slice of slices, when possible The main use case is slices of strings.

Here is an example from build.zig, showing how different coding styles are used in the same file.

docgen_cmd.addArgs(&.{ "--zig", b.zig_exe });
       const client_cpp = b.pathJoin(
            &[_][]const u8{ tracy_path, "public", "TracyClient.cpp" },
        );

Thanks.

perillo avatar Dec 26 '23 19:12 perillo

i dont think many of these simplifications would be possible since they may rely on type information that zig fmt doesnt have access to

xdBronch avatar Dec 26 '23 19:12 xdBronch

i dont think many of these simplifications would be possible since they may rely on type information that zig fmt doesnt have access to

You are right, but the cmdFmt function can load the required information as it is done when the --ast-checkflag is set, modify the original tree and render it.

perillo avatar Dec 26 '23 20:12 perillo