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.go30
1 files changed, 23 insertions, 7 deletions
diff --git a/src/cmd/fix/main.go b/src/cmd/fix/main.go
index e72c66398f..d055929aac 100644
--- a/src/cmd/fix/main.go
+++ b/src/cmd/fix/main.go
@@ -13,7 +13,8 @@ import (
"go/parser"
"go/scanner"
"go/token"
- "io/ioutil"
+ "io"
+ "io/fs"
"os"
"path/filepath"
"sort"
@@ -127,7 +128,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 +138,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 +196,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
}
@@ -200,7 +216,7 @@ func processFile(filename string, useStdin bool) error {
return nil
}
- return ioutil.WriteFile(f.Name(), newSrc, 0)
+ return os.WriteFile(f.Name(), newSrc, 0)
}
func gofmt(n interface{}) string {
@@ -217,10 +233,10 @@ func report(err error) {
}
func walkDir(path string) {
- filepath.Walk(path, visitFile)
+ filepath.WalkDir(path, visitFile)
}
-func visitFile(path string, f os.FileInfo, err error) error {
+func visitFile(path string, f fs.DirEntry, err error) error {
if err == nil && isGoFile(f) {
err = processFile(path, false)
}
@@ -230,7 +246,7 @@ func visitFile(path string, f os.FileInfo, err error) error {
return nil
}
-func isGoFile(f os.FileInfo) bool {
+func isGoFile(f fs.DirEntry) bool {
// ignore non-Go files
name := f.Name()
return !f.IsDir() && !strings.HasPrefix(name, ".") && strings.HasSuffix(name, ".go")