diff options
Diffstat (limited to 'src/cmd/fix')
| -rw-r--r-- | src/cmd/fix/gotypes.go | 6 | ||||
| -rw-r--r-- | src/cmd/fix/main.go | 30 | ||||
| -rw-r--r-- | src/cmd/fix/typecheck.go | 9 |
3 files changed, 30 insertions, 15 deletions
diff --git a/src/cmd/fix/gotypes.go b/src/cmd/fix/gotypes.go index 8a4019cc8c..031f85c9cc 100644 --- a/src/cmd/fix/gotypes.go +++ b/src/cmd/fix/gotypes.go @@ -21,11 +21,11 @@ var gotypesFix = fix{ } func gotypes(f *ast.File) bool { - truth := fixGoTypes(f) + fixed := fixGoTypes(f) if fixGoExact(f) { - truth = true + fixed = true } - return truth + return fixed } func fixGoTypes(f *ast.File) bool { 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") diff --git a/src/cmd/fix/typecheck.go b/src/cmd/fix/typecheck.go index 66e0cdcec0..40b2287f26 100644 --- a/src/cmd/fix/typecheck.go +++ b/src/cmd/fix/typecheck.go @@ -9,7 +9,6 @@ import ( "go/ast" "go/parser" "go/token" - "io/ioutil" "os" "os/exec" "path/filepath" @@ -162,12 +161,12 @@ func typecheck(cfg *TypeConfig, f *ast.File) (typeof map[interface{}]string, ass if err != nil { return err } - dir, err := ioutil.TempDir(os.TempDir(), "fix_cgo_typecheck") + dir, err := os.MkdirTemp(os.TempDir(), "fix_cgo_typecheck") if err != nil { return err } defer os.RemoveAll(dir) - err = ioutil.WriteFile(filepath.Join(dir, "in.go"), txt, 0600) + err = os.WriteFile(filepath.Join(dir, "in.go"), txt, 0600) if err != nil { return err } @@ -176,7 +175,7 @@ func typecheck(cfg *TypeConfig, f *ast.File) (typeof map[interface{}]string, ass if err != nil { return err } - out, err := ioutil.ReadFile(filepath.Join(dir, "_cgo_gotypes.go")) + out, err := os.ReadFile(filepath.Join(dir, "_cgo_gotypes.go")) if err != nil { return err } @@ -207,7 +206,7 @@ func typecheck(cfg *TypeConfig, f *ast.File) (typeof map[interface{}]string, ass return nil }() if err != nil { - fmt.Printf("warning: no cgo types: %s\n", err) + fmt.Fprintf(os.Stderr, "go fix: warning: no cgo types: %s\n", err) } } |
