1
0
mirror of https://github.com/golang/go synced 2024-09-30 14:28:33 -06:00

internal/analysisinternal: add anonymous structs & interfaces to

Fillstruct wasn't able to fill structs with fileds that held anonymous
structs or maps with anonymous structs/interfaces.

By adding *types.Struct and *types.Interface support in TypeExpr(),
fillstruct is now works for those as well.

Fixes golang/go#40980

Change-Id: I2a1d6d4db237800197cf14e3ad5067a0e7ab701a
Reviewed-on: https://go-review.googlesource.com/c/tools/+/249999
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
Pontus Leitzler 2020-08-23 00:09:11 +02:00 committed by Rebecca Stambler
parent c024452afb
commit b303f430e3
4 changed files with 40 additions and 2 deletions

View File

@ -198,8 +198,12 @@ func TypeExpr(fset *token.FileSet, f *ast.File, pkg *types.Package, typ types.Ty
X: ast.NewIdent(pkgName),
Sel: ast.NewIdent(t.Obj().Name()),
}
case *types.Struct:
return ast.NewIdent(t.String())
case *types.Interface:
return ast.NewIdent(t.String())
default:
return nil // TODO: anonymous structs, but who does that
return nil
}
}

View File

@ -0,0 +1,14 @@
package fillstruct
type StructAnon struct {
a struct{}
b map[string]interface{}
c map[string]struct {
d int
e bool
}
}
func fill() {
_ := StructAnon{} //@suggestedfix("}", "refactor.rewrite")
}

View File

@ -0,0 +1,20 @@
-- suggestedfix_fill_struct_anon_13_18 --
package fillstruct
type StructAnon struct {
a struct{}
b map[string]interface{}
c map[string]struct {
d int
e bool
}
}
func fill() {
_ := StructAnon{
a: struct{}{},
b: map[string]interface{}{},
c: map[string]struct{d int; e bool}{},
} //@suggestedfix("}", "refactor.rewrite")
}

View File

@ -12,7 +12,7 @@ DiagnosticsCount = 44
FoldingRangesCount = 2
FormatCount = 6
ImportCount = 8
SuggestedFixCount = 37
SuggestedFixCount = 38
FunctionExtractionCount = 11
DefinitionsCount = 63
TypeDefinitionsCount = 2