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>
diff --git a/internal/analysisinternal/analysis.go b/internal/analysisinternal/analysis.go
index e25f4a4..54a6992 100644
--- a/internal/analysisinternal/analysis.go
+++ b/internal/analysisinternal/analysis.go
@@ -198,8 +198,12 @@
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
}
}
diff --git a/internal/lsp/testdata/lsp/primarymod/fillstruct/fill_struct_anon.go b/internal/lsp/testdata/lsp/primarymod/fillstruct/fill_struct_anon.go
new file mode 100644
index 0000000..b5d2337
--- /dev/null
+++ b/internal/lsp/testdata/lsp/primarymod/fillstruct/fill_struct_anon.go
@@ -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")
+}
diff --git a/internal/lsp/testdata/lsp/primarymod/fillstruct/fill_struct_anon.go.golden b/internal/lsp/testdata/lsp/primarymod/fillstruct/fill_struct_anon.go.golden
new file mode 100644
index 0000000..eb6ffd6
--- /dev/null
+++ b/internal/lsp/testdata/lsp/primarymod/fillstruct/fill_struct_anon.go.golden
@@ -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")
+}
+
diff --git a/internal/lsp/testdata/lsp/summary.txt.golden b/internal/lsp/testdata/lsp/summary.txt.golden
index 651e8e9..36120de 100644
--- a/internal/lsp/testdata/lsp/summary.txt.golden
+++ b/internal/lsp/testdata/lsp/summary.txt.golden
@@ -12,7 +12,7 @@
FoldingRangesCount = 2
FormatCount = 6
ImportCount = 8
-SuggestedFixCount = 37
+SuggestedFixCount = 38
FunctionExtractionCount = 11
DefinitionsCount = 63
TypeDefinitionsCount = 2