aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/fix/main.go
diff options
context:
space:
mode:
Diffstat (limited to 'src/cmd/fix/main.go')
-rw-r--r--src/cmd/fix/main.go25
1 files changed, 21 insertions, 4 deletions
diff --git a/src/cmd/fix/main.go b/src/cmd/fix/main.go
index e72c66398f..1cea9a876a 100644
--- a/src/cmd/fix/main.go
+++ b/src/cmd/fix/main.go
@@ -13,6 +13,8 @@ import (
"go/parser"
"go/scanner"
"go/token"
+ "io"
+ "io/fs"
"io/ioutil"
"os"
"path/filepath"
@@ -127,7 +129,7 @@ func processFile(filename string, useStdin bool) error {
defer f.Close()
}
- src, err := ioutil.ReadAll(f)
+ src, err := io.ReadAll(f)
if err != nil {
return err
}
@@ -137,6 +139,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 +197,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
}
@@ -220,7 +237,7 @@ func walkDir(path string) {
filepath.Walk(path, visitFile)
}
-func visitFile(path string, f os.FileInfo, err error) error {
+func visitFile(path string, f fs.FileInfo, err error) error {
if err == nil && isGoFile(f) {
err = processFile(path, false)
}
@@ -230,7 +247,7 @@ func visitFile(path string, f os.FileInfo, err error) error {
return nil
}
-func isGoFile(f os.FileInfo) bool {
+func isGoFile(f fs.FileInfo) bool {
// ignore non-Go files
name := f.Name()
return !f.IsDir() && !strings.HasPrefix(name, ".") && strings.HasSuffix(name, ".go")