зеркало из https://github.com/golang/tools.git
go/analysis/passes/fieldalignment: suggest fix for multi-named fields
Add support for providing a suggested fix when the struct have field lines with multiple names, such as: type s struct { b bool i1, i2 int a3 [3]bool _ [0]func() } This was a leftover from CL 270217. Change-Id: Icccec5b8b4b82823cee33b7a84d0cccab38b6f45 Reviewed-on: https://go-review.googlesource.com/c/tools/+/273346 Reviewed-by: Michael Matloob <matloob@golang.org> Trust: Pontus Leitzler <leitzler@gmail.com>
This commit is contained in:
Родитель
a1a1cbeaa5
Коммит
090fee60cc
|
@ -71,22 +71,27 @@ func fieldalignment(pass *analysis.Pass, node *ast.StructType, typ *types.Struct
|
|||
return
|
||||
}
|
||||
|
||||
// TODO: The ast node could contain multiple field names per list item while "indexes"
|
||||
// based on *types.struct contain one item per field. Until this case is handled we
|
||||
// do not provide any suggested fix for it.
|
||||
if len(indexes) != len(node.Fields.List) {
|
||||
pass.Report(analysis.Diagnostic{
|
||||
Pos: node.Pos(),
|
||||
End: node.Pos() + token.Pos(len("struct")),
|
||||
Message: message,
|
||||
})
|
||||
return
|
||||
// Flatten the ast node since it could have multiple field names per list item while
|
||||
// *types.Struct only have one item per field.
|
||||
// TODO: Preserve multi-named fields instead of flattening.
|
||||
var flat []*ast.Field
|
||||
for _, f := range node.Fields.List {
|
||||
if len(f.Names) == 1 {
|
||||
flat = append(flat, f)
|
||||
continue
|
||||
}
|
||||
for _, name := range f.Names {
|
||||
flat = append(flat, &ast.Field{
|
||||
Names: []*ast.Ident{name},
|
||||
Type: f.Type,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Sort fields according to the optimal order.
|
||||
var reordered []*ast.Field
|
||||
for _, index := range indexes {
|
||||
reordered = append(reordered, node.Fields.List[index])
|
||||
reordered = append(reordered, flat[index])
|
||||
}
|
||||
|
||||
newStr := &ast.StructType{
|
||||
|
|
|
@ -11,3 +11,13 @@ type Bad struct { // want "struct of size 12 could be 8"
|
|||
y int32
|
||||
z byte
|
||||
}
|
||||
|
||||
type ZeroGood struct {
|
||||
a [0]byte
|
||||
b uint32
|
||||
}
|
||||
|
||||
type ZeroBad struct { // want "struct of size 8 could be 4"
|
||||
a uint32
|
||||
b [0]byte
|
||||
}
|
||||
|
|
|
@ -11,3 +11,13 @@ type Bad struct {
|
|||
x byte
|
||||
z byte
|
||||
}
|
||||
|
||||
type ZeroGood struct {
|
||||
a [0]byte
|
||||
b uint32
|
||||
}
|
||||
|
||||
type ZeroBad struct {
|
||||
b [0]byte
|
||||
a uint32
|
||||
}
|
||||
|
|
|
@ -31,3 +31,10 @@ type PointerSortaBad struct { // want "struct with 16 pointer bytes could be 12"
|
|||
q uintptr
|
||||
}
|
||||
}
|
||||
|
||||
type MultiField struct { // want "struct of size 20 could be 12"
|
||||
b bool
|
||||
i1, i2 int
|
||||
a3 [3]bool
|
||||
_ [0]func()
|
||||
}
|
|
@ -31,3 +31,11 @@ type PointerSortaBad struct {
|
|||
q [2]uintptr
|
||||
}
|
||||
}
|
||||
|
||||
type MultiField struct {
|
||||
_ [0]func()
|
||||
i1 int
|
||||
i2 int
|
||||
a3 [3]bool
|
||||
b bool
|
||||
}
|
|
@ -31,3 +31,10 @@ type PointerSortaBad struct { // want "struct with 32 pointer bytes could be 24"
|
|||
q uintptr
|
||||
}
|
||||
}
|
||||
|
||||
type MultiField struct { // want "struct of size 40 could be 24"
|
||||
b bool
|
||||
i1, i2 int
|
||||
a3 [3]bool
|
||||
_ [0]func()
|
||||
}
|
|
@ -31,3 +31,11 @@ type PointerSortaBad struct {
|
|||
q [2]uintptr
|
||||
}
|
||||
}
|
||||
|
||||
type MultiField struct {
|
||||
_ [0]func()
|
||||
i1 int
|
||||
i2 int
|
||||
a3 [3]bool
|
||||
b bool
|
||||
}
|
|
@ -1,11 +0,0 @@
|
|||
package a
|
||||
|
||||
type ZeroGood struct {
|
||||
a [0]byte
|
||||
b uint32
|
||||
}
|
||||
|
||||
type ZeroBad struct { // want "struct of size 8 could be 4"
|
||||
a uint32
|
||||
b [0]byte
|
||||
}
|
|
@ -1,11 +0,0 @@
|
|||
package a
|
||||
|
||||
type ZeroGood struct {
|
||||
a [0]byte
|
||||
b uint32
|
||||
}
|
||||
|
||||
type ZeroBad struct {
|
||||
b [0]byte
|
||||
a uint32
|
||||
}
|
Загрузка…
Ссылка в новой задаче