Skip to content

Commit 152dbd3

Browse files
committed
refactor: Remove unused GetStructNameForField util function
1 parent 46670a1 commit 152dbd3

File tree

2 files changed

+0
-93
lines changed

2 files changed

+0
-93
lines changed

pkg/analysis/utils/utils.go

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"go/ast"
2222
"go/token"
2323
"go/types"
24-
"slices"
2524
"strings"
2625

2726
"golang.org/x/tools/go/analysis"
@@ -354,52 +353,6 @@ func isTypeBasic(t types.Type) bool {
354353
return false
355354
}
356355

357-
// GetStructNameForField inspects the AST of the package and returns the name of the struct
358-
// that contains the field being inspected.
359-
func GetStructNameForField(pass *analysis.Pass, field *ast.Field) string {
360-
for _, file := range pass.Files {
361-
var (
362-
structName string
363-
found bool
364-
)
365-
366-
ast.Inspect(file, func(n ast.Node) bool {
367-
if found {
368-
return false
369-
}
370-
371-
typeSpec, ok := n.(*ast.TypeSpec)
372-
if !ok {
373-
return true
374-
}
375-
376-
structType, ok := typeSpec.Type.(*ast.StructType)
377-
if !ok {
378-
return true
379-
}
380-
381-
structName = typeSpec.Name.Name
382-
383-
if structType.Fields == nil {
384-
return true
385-
}
386-
387-
if slices.Contains(structType.Fields.List, field) {
388-
found = true
389-
return false
390-
}
391-
392-
return true
393-
})
394-
395-
if found {
396-
return structName
397-
}
398-
}
399-
400-
return ""
401-
}
402-
403356
// GetMinProperties returns the value of the minimum properties marker.
404357
// It returns a nil value when the marker is not present, and an error
405358
// when the marker is present, but malformed.

pkg/analysis/utils/utils_test.go

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,6 @@ import (
2020

2121
. "github.com/onsi/ginkgo/v2"
2222
. "github.com/onsi/gomega"
23-
"golang.org/x/tools/go/analysis"
24-
"golang.org/x/tools/go/analysis/analysistest"
25-
"golang.org/x/tools/go/analysis/passes/inspect"
26-
"golang.org/x/tools/go/ast/inspector"
2723

2824
"sigs.k8s.io/kube-api-linter/pkg/analysis/utils"
2925
)
@@ -77,45 +73,3 @@ var _ = Describe("FieldName", func() {
7773
}),
7874
)
7975
})
80-
81-
var _ = Describe("GetStructNameForField", func() {
82-
It("should find the correct struct name for a field", func() {
83-
testdata := analysistest.TestData()
84-
analysistest.Run(GinkgoT(), testdata, testGetStructNameAnalyzer(), "getstructname")
85-
})
86-
})
87-
88-
func testGetStructNameAnalyzer() *analysis.Analyzer {
89-
return &analysis.Analyzer{
90-
Name: "testgetstructname",
91-
Doc: "test analyzer for GetStructNameForField",
92-
Requires: []*analysis.Analyzer{inspect.Analyzer},
93-
Run: func(pass *analysis.Pass) (any, error) {
94-
inspect, ok := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
95-
if !ok {
96-
return nil, errCouldNotGetInspector
97-
}
98-
99-
nodeFilter := []ast.Node{
100-
(*ast.Field)(nil),
101-
}
102-
103-
inspect.Preorder(nodeFilter, func(n ast.Node) {
104-
field, ok := n.(*ast.Field)
105-
if !ok {
106-
return
107-
}
108-
if len(field.Names) == 0 {
109-
return
110-
}
111-
112-
structName := utils.GetStructNameForField(pass, field)
113-
if structName != "" {
114-
pass.Reportf(field.Pos(), "field %s is in struct %s", field.Names[0].Name, structName)
115-
}
116-
})
117-
118-
return nil, nil
119-
},
120-
}
121-
}

0 commit comments

Comments
 (0)