cmd/fix: remove all functionality except for buildtag

For #73605

Change-Id: I4b46b5eb72471c215f2cc208c1b0cdd1fbdbf81a
Reviewed-on: https://go-review.googlesource.com/c/go/+/695855
Reviewed-by: Michael Matloob <matloob@golang.org>
Reviewed-by: Michael Pratt <mpratt@google.com>
Reviewed-by: Alan Donovan <adonovan@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Auto-Submit: Alan Donovan <adonovan@google.com>
This commit is contained in:
qiulaidongfeng 2025-08-13 22:33:14 +08:00 committed by Gopher Robot
parent 87e72769fa
commit 459b85ccaa
14 changed files with 18 additions and 1259 deletions

View file

@ -6,9 +6,6 @@ package main
import (
"go/ast"
"go/token"
"reflect"
"strings"
)
func init() {
@ -18,130 +15,11 @@ func init() {
var cftypeFix = fix{
name: "cftype",
date: "2017-09-27",
f: cftypefix,
desc: `Fixes initializers and casts of C.*Ref and JNI types`,
f: noop,
desc: `Fixes initializers and casts of C.*Ref and JNI types (removed)`,
disabled: false,
}
// Old state:
//
// type CFTypeRef unsafe.Pointer
//
// New state:
//
// type CFTypeRef uintptr
//
// and similar for other *Ref types.
// This fix finds nils initializing these types and replaces the nils with 0s.
func cftypefix(f *ast.File) bool {
return typefix(f, func(s string) bool {
return strings.HasPrefix(s, "C.") && strings.HasSuffix(s, "Ref") && s != "C.CFAllocatorRef"
})
}
// typefix replaces nil with 0 for all nils whose type, when passed to badType, returns true.
func typefix(f *ast.File, badType func(string) bool) bool {
if !imports(f, "C") {
return false
}
typeof, _ := typecheck(&TypeConfig{}, f)
changed := false
// step 1: Find all the nils with the offending types.
// Compute their replacement.
badNils := map[any]ast.Expr{}
walk(f, func(n any) {
if i, ok := n.(*ast.Ident); ok && i.Name == "nil" && badType(typeof[n]) {
badNils[n] = &ast.BasicLit{ValuePos: i.NamePos, Kind: token.INT, Value: "0"}
}
})
// step 2: find all uses of the bad nils, replace them with 0.
// There's no easy way to map from an ast.Expr to all the places that use them, so
// we use reflect to find all such references.
if len(badNils) > 0 {
exprType := reflect.TypeFor[ast.Expr]()
exprSliceType := reflect.TypeFor[[]ast.Expr]()
walk(f, func(n any) {
if n == nil {
return
}
v := reflect.ValueOf(n)
if v.Kind() != reflect.Pointer {
return
}
if v.IsNil() {
return
}
v = v.Elem()
if v.Kind() != reflect.Struct {
return
}
for i := 0; i < v.NumField(); i++ {
f := v.Field(i)
if f.Type() == exprType {
if r := badNils[f.Interface()]; r != nil {
f.Set(reflect.ValueOf(r))
changed = true
}
}
if f.Type() == exprSliceType {
for j := 0; j < f.Len(); j++ {
e := f.Index(j)
if r := badNils[e.Interface()]; r != nil {
e.Set(reflect.ValueOf(r))
changed = true
}
}
}
}
})
}
// step 3: fix up invalid casts.
// It used to be ok to cast between *unsafe.Pointer and *C.CFTypeRef in a single step.
// Now we need unsafe.Pointer as an intermediate cast.
// (*unsafe.Pointer)(x) where x is type *bad -> (*unsafe.Pointer)(unsafe.Pointer(x))
// (*bad.type)(x) where x is type *unsafe.Pointer -> (*bad.type)(unsafe.Pointer(x))
walk(f, func(n any) {
if n == nil {
return
}
// Find pattern like (*a.b)(x)
c, ok := n.(*ast.CallExpr)
if !ok {
return
}
if len(c.Args) != 1 {
return
}
p, ok := c.Fun.(*ast.ParenExpr)
if !ok {
return
}
s, ok := p.X.(*ast.StarExpr)
if !ok {
return
}
t, ok := s.X.(*ast.SelectorExpr)
if !ok {
return
}
pkg, ok := t.X.(*ast.Ident)
if !ok {
return
}
dst := pkg.Name + "." + t.Sel.Name
src := typeof[c.Args[0]]
if badType(dst) && src == "*unsafe.Pointer" ||
dst == "unsafe.Pointer" && strings.HasPrefix(src, "*") && badType(src[1:]) {
c.Args[0] = &ast.CallExpr{
Fun: &ast.SelectorExpr{X: &ast.Ident{Name: "unsafe"}, Sel: &ast.Ident{Name: "Pointer"}},
Args: []ast.Expr{c.Args[0]},
}
changed = true
}
})
return changed
func noop(f *ast.File) bool {
return false
}

View file

@ -1,241 +0,0 @@
// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
func init() {
addTestCases(cftypeTests, cftypefix)
}
var cftypeTests = []testCase{
{
Name: "cftype.localVariable",
In: `package main
// typedef const void *CFTypeRef;
import "C"
func f() {
var x C.CFTypeRef = nil
x = nil
x, x = nil, nil
}
`,
Out: `package main
// typedef const void *CFTypeRef;
import "C"
func f() {
var x C.CFTypeRef = 0
x = 0
x, x = 0, 0
}
`,
},
{
Name: "cftype.globalVariable",
In: `package main
// typedef const void *CFTypeRef;
import "C"
var x C.CFTypeRef = nil
func f() {
x = nil
}
`,
Out: `package main
// typedef const void *CFTypeRef;
import "C"
var x C.CFTypeRef = 0
func f() {
x = 0
}
`,
},
{
Name: "cftype.EqualArgument",
In: `package main
// typedef const void *CFTypeRef;
import "C"
var x C.CFTypeRef
var y = x == nil
var z = x != nil
`,
Out: `package main
// typedef const void *CFTypeRef;
import "C"
var x C.CFTypeRef
var y = x == 0
var z = x != 0
`,
},
{
Name: "cftype.StructField",
In: `package main
// typedef const void *CFTypeRef;
import "C"
type T struct {
x C.CFTypeRef
}
var t = T{x: nil}
`,
Out: `package main
// typedef const void *CFTypeRef;
import "C"
type T struct {
x C.CFTypeRef
}
var t = T{x: 0}
`,
},
{
Name: "cftype.FunctionArgument",
In: `package main
// typedef const void *CFTypeRef;
import "C"
func f(x C.CFTypeRef) {
}
func g() {
f(nil)
}
`,
Out: `package main
// typedef const void *CFTypeRef;
import "C"
func f(x C.CFTypeRef) {
}
func g() {
f(0)
}
`,
},
{
Name: "cftype.ArrayElement",
In: `package main
// typedef const void *CFTypeRef;
import "C"
var x = [3]C.CFTypeRef{nil, nil, nil}
`,
Out: `package main
// typedef const void *CFTypeRef;
import "C"
var x = [3]C.CFTypeRef{0, 0, 0}
`,
},
{
Name: "cftype.SliceElement",
In: `package main
// typedef const void *CFTypeRef;
import "C"
var x = []C.CFTypeRef{nil, nil, nil}
`,
Out: `package main
// typedef const void *CFTypeRef;
import "C"
var x = []C.CFTypeRef{0, 0, 0}
`,
},
{
Name: "cftype.MapKey",
In: `package main
// typedef const void *CFTypeRef;
import "C"
var x = map[C.CFTypeRef]int{nil: 0}
`,
Out: `package main
// typedef const void *CFTypeRef;
import "C"
var x = map[C.CFTypeRef]int{0: 0}
`,
},
{
Name: "cftype.MapValue",
In: `package main
// typedef const void *CFTypeRef;
import "C"
var x = map[int]C.CFTypeRef{0: nil}
`,
Out: `package main
// typedef const void *CFTypeRef;
import "C"
var x = map[int]C.CFTypeRef{0: 0}
`,
},
{
Name: "cftype.Conversion1",
In: `package main
// typedef const void *CFTypeRef;
import "C"
var x C.CFTypeRef
var y = (*unsafe.Pointer)(&x)
`,
Out: `package main
// typedef const void *CFTypeRef;
import "C"
var x C.CFTypeRef
var y = (*unsafe.Pointer)(unsafe.Pointer(&x))
`,
},
{
Name: "cftype.Conversion2",
In: `package main
// typedef const void *CFTypeRef;
import "C"
var x unsafe.Pointer
var y = (*C.CFTypeRef)(&x)
`,
Out: `package main
// typedef const void *CFTypeRef;
import "C"
var x unsafe.Pointer
var y = (*C.CFTypeRef)(unsafe.Pointer(&x))
`,
},
}

View file

@ -4,10 +4,6 @@
package main
import (
"go/ast"
)
func init() {
register(contextFix)
}
@ -15,11 +11,7 @@ func init() {
var contextFix = fix{
name: "context",
date: "2016-09-09",
f: ctxfix,
desc: `Change imports of golang.org/x/net/context to context`,
f: noop,
desc: `Change imports of golang.org/x/net/context to context (removed)`,
disabled: false,
}
func ctxfix(f *ast.File) bool {
return rewriteImport(f, "golang.org/x/net/context", "context")
}

View file

@ -1,42 +0,0 @@
// Copyright 2016 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
func init() {
addTestCases(contextTests, ctxfix)
}
var contextTests = []testCase{
{
Name: "context.0",
In: `package main
import "golang.org/x/net/context"
var _ = "golang.org/x/net/context"
`,
Out: `package main
import "context"
var _ = "golang.org/x/net/context"
`,
},
{
Name: "context.1",
In: `package main
import ctx "golang.org/x/net/context"
var _ = ctx.Background()
`,
Out: `package main
import ctx "context"
var _ = ctx.Background()
`,
},
}

View file

@ -4,10 +4,6 @@
package main
import (
"go/ast"
)
func init() {
register(eglFixDisplay)
register(eglFixConfig)
@ -16,45 +12,15 @@ func init() {
var eglFixDisplay = fix{
name: "egl",
date: "2018-12-15",
f: eglfixDisp,
desc: `Fixes initializers of EGLDisplay`,
f: noop,
desc: `Fixes initializers of EGLDisplay (removed)`,
disabled: false,
}
// Old state:
//
// type EGLDisplay unsafe.Pointer
//
// New state:
//
// type EGLDisplay uintptr
//
// This fix finds nils initializing these types and replaces the nils with 0s.
func eglfixDisp(f *ast.File) bool {
return typefix(f, func(s string) bool {
return s == "C.EGLDisplay"
})
}
var eglFixConfig = fix{
name: "eglconf",
date: "2020-05-30",
f: eglfixConfig,
desc: `Fixes initializers of EGLConfig`,
f: noop,
desc: `Fixes initializers of EGLConfig (removed)`,
disabled: false,
}
// Old state:
//
// type EGLConfig unsafe.Pointer
//
// New state:
//
// type EGLConfig uintptr
//
// This fix finds nils initializing these types and replaces the nils with 0s.
func eglfixConfig(f *ast.File) bool {
return typefix(f, func(s string) bool {
return s == "C.EGLConfig"
})
}

View file

@ -1,214 +0,0 @@
// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import "strings"
func init() {
addTestCases(eglTestsFor("EGLDisplay"), eglfixDisp)
addTestCases(eglTestsFor("EGLConfig"), eglfixConfig)
}
func eglTestsFor(tname string) []testCase {
var eglTests = []testCase{
{
Name: "egl.localVariable",
In: `package main
// typedef void *$EGLTYPE;
import "C"
func f() {
var x C.$EGLTYPE = nil
x = nil
x, x = nil, nil
}
`,
Out: `package main
// typedef void *$EGLTYPE;
import "C"
func f() {
var x C.$EGLTYPE = 0
x = 0
x, x = 0, 0
}
`,
},
{
Name: "egl.globalVariable",
In: `package main
// typedef void *$EGLTYPE;
import "C"
var x C.$EGLTYPE = nil
func f() {
x = nil
}
`,
Out: `package main
// typedef void *$EGLTYPE;
import "C"
var x C.$EGLTYPE = 0
func f() {
x = 0
}
`,
},
{
Name: "egl.EqualArgument",
In: `package main
// typedef void *$EGLTYPE;
import "C"
var x C.$EGLTYPE
var y = x == nil
var z = x != nil
`,
Out: `package main
// typedef void *$EGLTYPE;
import "C"
var x C.$EGLTYPE
var y = x == 0
var z = x != 0
`,
},
{
Name: "egl.StructField",
In: `package main
// typedef void *$EGLTYPE;
import "C"
type T struct {
x C.$EGLTYPE
}
var t = T{x: nil}
`,
Out: `package main
// typedef void *$EGLTYPE;
import "C"
type T struct {
x C.$EGLTYPE
}
var t = T{x: 0}
`,
},
{
Name: "egl.FunctionArgument",
In: `package main
// typedef void *$EGLTYPE;
import "C"
func f(x C.$EGLTYPE) {
}
func g() {
f(nil)
}
`,
Out: `package main
// typedef void *$EGLTYPE;
import "C"
func f(x C.$EGLTYPE) {
}
func g() {
f(0)
}
`,
},
{
Name: "egl.ArrayElement",
In: `package main
// typedef void *$EGLTYPE;
import "C"
var x = [3]C.$EGLTYPE{nil, nil, nil}
`,
Out: `package main
// typedef void *$EGLTYPE;
import "C"
var x = [3]C.$EGLTYPE{0, 0, 0}
`,
},
{
Name: "egl.SliceElement",
In: `package main
// typedef void *$EGLTYPE;
import "C"
var x = []C.$EGLTYPE{nil, nil, nil}
`,
Out: `package main
// typedef void *$EGLTYPE;
import "C"
var x = []C.$EGLTYPE{0, 0, 0}
`,
},
{
Name: "egl.MapKey",
In: `package main
// typedef void *$EGLTYPE;
import "C"
var x = map[C.$EGLTYPE]int{nil: 0}
`,
Out: `package main
// typedef void *$EGLTYPE;
import "C"
var x = map[C.$EGLTYPE]int{0: 0}
`,
},
{
Name: "egl.MapValue",
In: `package main
// typedef void *$EGLTYPE;
import "C"
var x = map[int]C.$EGLTYPE{0: nil}
`,
Out: `package main
// typedef void *$EGLTYPE;
import "C"
var x = map[int]C.$EGLTYPE{0: 0}
`,
},
}
for i := range eglTests {
t := &eglTests[i]
t.In = strings.ReplaceAll(t.In, "$EGLTYPE", tname)
t.Out = strings.ReplaceAll(t.Out, "$EGLTYPE", tname)
}
return eglTests
}

View file

@ -4,11 +4,6 @@
package main
import (
"go/ast"
"strconv"
)
func init() {
register(gotypesFix)
}
@ -16,60 +11,6 @@ func init() {
var gotypesFix = fix{
name: "gotypes",
date: "2015-07-16",
f: gotypes,
desc: `Change imports of golang.org/x/tools/go/{exact,types} to go/{constant,types}`,
}
func gotypes(f *ast.File) bool {
fixed := fixGoTypes(f)
if fixGoExact(f) {
fixed = true
}
return fixed
}
func fixGoTypes(f *ast.File) bool {
return rewriteImport(f, "golang.org/x/tools/go/types", "go/types")
}
func fixGoExact(f *ast.File) bool {
// This one is harder because the import name changes.
// First find the import spec.
var importSpec *ast.ImportSpec
walk(f, func(n any) {
if importSpec != nil {
return
}
spec, ok := n.(*ast.ImportSpec)
if !ok {
return
}
path, err := strconv.Unquote(spec.Path.Value)
if err != nil {
return
}
if path == "golang.org/x/tools/go/exact" {
importSpec = spec
}
})
if importSpec == nil {
return false
}
// We are about to rename exact.* to constant.*, but constant is a common
// name. See if it will conflict. This is a hack but it is effective.
exists := renameTop(f, "constant", "constant")
suffix := ""
if exists {
suffix = "_"
}
// Now we need to rename all the uses of the import. RewriteImport
// affects renameTop, but not vice versa, so do them in this order.
renameTop(f, "exact", "constant"+suffix)
rewriteImport(f, "golang.org/x/tools/go/exact", "go/constant")
// renameTop will also rewrite the imported package name. Fix that;
// we know it should be missing.
importSpec.Name = nil
return true
f: noop,
desc: `Change imports of golang.org/x/tools/go/{exact,types} to go/{constant,types} (removed)`,
}

View file

@ -1,89 +0,0 @@
// Copyright 2012 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
func init() {
addTestCases(gotypesTests, gotypes)
}
var gotypesTests = []testCase{
{
Name: "gotypes.0",
In: `package main
import "golang.org/x/tools/go/types"
import "golang.org/x/tools/go/exact"
var _ = exact.Kind
func f() {
_ = exact.MakeBool(true)
}
`,
Out: `package main
import "go/types"
import "go/constant"
var _ = constant.Kind
func f() {
_ = constant.MakeBool(true)
}
`,
},
{
Name: "gotypes.1",
In: `package main
import "golang.org/x/tools/go/types"
import foo "golang.org/x/tools/go/exact"
var _ = foo.Kind
func f() {
_ = foo.MakeBool(true)
}
`,
Out: `package main
import "go/types"
import "go/constant"
var _ = foo.Kind
func f() {
_ = foo.MakeBool(true)
}
`,
},
{
Name: "gotypes.0",
In: `package main
import "golang.org/x/tools/go/types"
import "golang.org/x/tools/go/exact"
var _ = exact.Kind
var constant = 23 // Use of new package name.
func f() {
_ = exact.MakeBool(true)
}
`,
Out: `package main
import "go/types"
import "go/constant"
var _ = constant_.Kind
var constant = 23 // Use of new package name.
func f() {
_ = constant_.MakeBool(true)
}
`,
},
}

View file

@ -4,10 +4,6 @@
package main
import (
"go/ast"
)
func init() {
register(jniFix)
}
@ -15,55 +11,7 @@ func init() {
var jniFix = fix{
name: "jni",
date: "2017-12-04",
f: jnifix,
desc: `Fixes initializers of JNI's jobject and subtypes`,
f: noop,
desc: `Fixes initializers of JNI's jobject and subtypes (removed)`,
disabled: false,
}
// Old state:
//
// type jobject *_jobject
//
// New state:
//
// type jobject uintptr
//
// and similar for subtypes of jobject.
// This fix finds nils initializing these types and replaces the nils with 0s.
func jnifix(f *ast.File) bool {
return typefix(f, func(s string) bool {
switch s {
case "C.jobject":
return true
case "C.jclass":
return true
case "C.jthrowable":
return true
case "C.jstring":
return true
case "C.jarray":
return true
case "C.jbooleanArray":
return true
case "C.jbyteArray":
return true
case "C.jcharArray":
return true
case "C.jshortArray":
return true
case "C.jintArray":
return true
case "C.jlongArray":
return true
case "C.jfloatArray":
return true
case "C.jdoubleArray":
return true
case "C.jobjectArray":
return true
case "C.jweak":
return true
}
return false
})
}

View file

@ -1,203 +0,0 @@
// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
func init() {
addTestCases(jniTests, jnifix)
}
var jniTests = []testCase{
{
Name: "jni.localVariable",
In: `package main
// typedef struct _jobject* jobject;
import "C"
func f() {
var x C.jobject = nil
x = nil
x, x = nil, nil
}
`,
Out: `package main
// typedef struct _jobject* jobject;
import "C"
func f() {
var x C.jobject = 0
x = 0
x, x = 0, 0
}
`,
},
{
Name: "jni.globalVariable",
In: `package main
// typedef struct _jobject* jobject;
import "C"
var x C.jobject = nil
func f() {
x = nil
}
`,
Out: `package main
// typedef struct _jobject* jobject;
import "C"
var x C.jobject = 0
func f() {
x = 0
}
`,
},
{
Name: "jni.EqualArgument",
In: `package main
// typedef struct _jobject* jobject;
import "C"
var x C.jobject
var y = x == nil
var z = x != nil
`,
Out: `package main
// typedef struct _jobject* jobject;
import "C"
var x C.jobject
var y = x == 0
var z = x != 0
`,
},
{
Name: "jni.StructField",
In: `package main
// typedef struct _jobject* jobject;
import "C"
type T struct {
x C.jobject
}
var t = T{x: nil}
`,
Out: `package main
// typedef struct _jobject* jobject;
import "C"
type T struct {
x C.jobject
}
var t = T{x: 0}
`,
},
{
Name: "jni.FunctionArgument",
In: `package main
// typedef struct _jobject* jobject;
import "C"
func f(x C.jobject) {
}
func g() {
f(nil)
}
`,
Out: `package main
// typedef struct _jobject* jobject;
import "C"
func f(x C.jobject) {
}
func g() {
f(0)
}
`,
},
{
Name: "jni.ArrayElement",
In: `package main
// typedef struct _jobject* jobject;
import "C"
var x = [3]C.jobject{nil, nil, nil}
`,
Out: `package main
// typedef struct _jobject* jobject;
import "C"
var x = [3]C.jobject{0, 0, 0}
`,
},
{
Name: "jni.SliceElement",
In: `package main
// typedef struct _jobject* jobject;
import "C"
var x = []C.jobject{nil, nil, nil}
`,
Out: `package main
// typedef struct _jobject* jobject;
import "C"
var x = []C.jobject{0, 0, 0}
`,
},
{
Name: "jni.MapKey",
In: `package main
// typedef struct _jobject* jobject;
import "C"
var x = map[C.jobject]int{nil: 0}
`,
Out: `package main
// typedef struct _jobject* jobject;
import "C"
var x = map[C.jobject]int{0: 0}
`,
},
{
Name: "jni.MapValue",
In: `package main
// typedef struct _jobject* jobject;
import "C"
var x = map[int]C.jobject{0: nil}
`,
Out: `package main
// typedef struct _jobject* jobject;
import "C"
var x = map[int]C.jobject{0: 0}
`,
},
}

View file

@ -4,11 +4,6 @@
package main
import (
"go/ast"
"slices"
)
func init() {
register(netipv6zoneFix)
}
@ -16,56 +11,9 @@ func init() {
var netipv6zoneFix = fix{
name: "netipv6zone",
date: "2012-11-26",
f: netipv6zone,
desc: `Adapt element key to IPAddr, UDPAddr or TCPAddr composite literals.
f: noop,
desc: `Adapt element key to IPAddr, UDPAddr or TCPAddr composite literals (removed).
https://codereview.appspot.com/6849045/
`,
}
func netipv6zone(f *ast.File) bool {
if !imports(f, "net") {
return false
}
fixed := false
walk(f, func(n any) {
cl, ok := n.(*ast.CompositeLit)
if !ok {
return
}
se, ok := cl.Type.(*ast.SelectorExpr)
if !ok {
return
}
if !isTopName(se.X, "net") || se.Sel == nil {
return
}
switch ss := se.Sel.String(); ss {
case "IPAddr", "UDPAddr", "TCPAddr":
for i, e := range cl.Elts {
if _, ok := e.(*ast.KeyValueExpr); ok {
break
}
switch i {
case 0:
cl.Elts[i] = &ast.KeyValueExpr{
Key: ast.NewIdent("IP"),
Value: e,
}
case 1:
if elit, ok := e.(*ast.BasicLit); ok && elit.Value == "0" {
cl.Elts = slices.Delete(cl.Elts, i, i+1)
} else {
cl.Elts[i] = &ast.KeyValueExpr{
Key: ast.NewIdent("Port"),
Value: e,
}
}
}
fixed = true
}
}
})
return fixed
}

View file

@ -1,43 +0,0 @@
// Copyright 2012 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
func init() {
addTestCases(netipv6zoneTests, netipv6zone)
}
var netipv6zoneTests = []testCase{
{
Name: "netipv6zone.0",
In: `package main
import "net"
func f() net.Addr {
a := &net.IPAddr{ip1}
sub(&net.UDPAddr{ip2, 12345})
c := &net.TCPAddr{IP: ip3, Port: 54321}
d := &net.TCPAddr{ip4, 0}
p := 1234
e := &net.TCPAddr{ip4, p}
return &net.TCPAddr{ip5}, nil
}
`,
Out: `package main
import "net"
func f() net.Addr {
a := &net.IPAddr{IP: ip1}
sub(&net.UDPAddr{IP: ip2, Port: 12345})
c := &net.TCPAddr{IP: ip3, Port: 54321}
d := &net.TCPAddr{IP: ip4}
p := 1234
e := &net.TCPAddr{IP: ip4, Port: p}
return &net.TCPAddr{IP: ip5}, nil
}
`,
},
}

View file

@ -4,8 +4,6 @@
package main
import "go/ast"
func init() {
register(printerconfigFix)
}
@ -13,49 +11,6 @@ func init() {
var printerconfigFix = fix{
name: "printerconfig",
date: "2012-12-11",
f: printerconfig,
desc: `Add element keys to Config composite literals.`,
}
func printerconfig(f *ast.File) bool {
if !imports(f, "go/printer") {
return false
}
fixed := false
walk(f, func(n any) {
cl, ok := n.(*ast.CompositeLit)
if !ok {
return
}
se, ok := cl.Type.(*ast.SelectorExpr)
if !ok {
return
}
if !isTopName(se.X, "printer") || se.Sel == nil {
return
}
if ss := se.Sel.String(); ss == "Config" {
for i, e := range cl.Elts {
if _, ok := e.(*ast.KeyValueExpr); ok {
break
}
switch i {
case 0:
cl.Elts[i] = &ast.KeyValueExpr{
Key: ast.NewIdent("Mode"),
Value: e,
}
case 1:
cl.Elts[i] = &ast.KeyValueExpr{
Key: ast.NewIdent("Tabwidth"),
Value: e,
}
}
fixed = true
}
}
})
return fixed
f: noop,
desc: `Add element keys to Config composite literals (removed).`,
}

View file

@ -1,37 +0,0 @@
// Copyright 2012 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
func init() {
addTestCases(printerconfigTests, printerconfig)
}
var printerconfigTests = []testCase{
{
Name: "printerconfig.0",
In: `package main
import "go/printer"
func f() printer.Config {
b := printer.Config{0, 8}
c := &printer.Config{0}
d := &printer.Config{Tabwidth: 8, Mode: 0}
return printer.Config{0, 8}
}
`,
Out: `package main
import "go/printer"
func f() printer.Config {
b := printer.Config{Mode: 0, Tabwidth: 8}
c := &printer.Config{Mode: 0}
d := &printer.Config{Tabwidth: 8, Mode: 0}
return printer.Config{Mode: 0, Tabwidth: 8}
}
`,
},
}