From: Russ Cox Date: Mon, 29 Jun 2020 14:44:56 +0000 (-0400) Subject: cmd/fix: always format source file before fixing X-Git-Tag: go1.16beta1~776 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=09833da6b4cfb494ba61a398ab2063ab016d3890;p=gostls13.git cmd/fix: always format source file before fixing This makes the changes to the file easier to explain. Not all the changes may come from the fixers directly, if the file is not gofmt-ed already. Change-Id: I81776da446a34a1239a3130317d2aae1437d61a5 Reviewed-on: https://go-review.googlesource.com/c/go/+/240555 Trust: Russ Cox Reviewed-by: Jay Conrod --- diff --git a/src/cmd/fix/main.go b/src/cmd/fix/main.go index e72c66398f..d19dde6b4a 100644 --- a/src/cmd/fix/main.go +++ b/src/cmd/fix/main.go @@ -137,6 +137,21 @@ func processFile(filename string, useStdin bool) error { return err } + // Make sure file is in canonical format. + // This "fmt" pseudo-fix cannot be disabled. + newSrc, err := gofmtFile(file) + if err != nil { + return err + } + if !bytes.Equal(newSrc, src) { + newFile, err := parser.ParseFile(fset, filename, newSrc, parserMode) + if err != nil { + return err + } + file = newFile + fmt.Fprintf(&fixlog, " fmt") + } + // Apply all fixes to file. newFile := file fixed := false @@ -180,7 +195,7 @@ func processFile(filename string, useStdin bool) error { // output of the printer run on a standard AST generated by the parser, // but the source we generated inside the loop above is the // output of the printer run on a mangled AST generated by a fixer. - newSrc, err := gofmtFile(newFile) + newSrc, err = gofmtFile(newFile) if err != nil { return err }