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 | 25 | ||||
-rw-r--r-- | src/cmd/fix/typecheck.go | 2 |
3 files changed, 25 insertions, 8 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..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") diff --git a/src/cmd/fix/typecheck.go b/src/cmd/fix/typecheck.go index 66e0cdcec0..f45155b06d 100644 --- a/src/cmd/fix/typecheck.go +++ b/src/cmd/fix/typecheck.go @@ -207,7 +207,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) } } |