Skip to content

Commit 1290219

Browse files
typechecker uses qualifiedFieldName
1 parent 569467d commit 1290219

File tree

3 files changed

+10
-14
lines changed

3 files changed

+10
-14
lines changed

pkg/analysis/numericbounds/analyzer.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ func run(pass *analysis.Pass) (any, error) {
7171

7272
inspect.InspectFields(func(field *ast.Field, _ extractjsontags.FieldTagInfo, markersAccess markershelper.Markers, qualifiedFieldName string) {
7373
// Create TypeChecker with closure capturing markersAccess and qualifiedFieldName
74+
// Ignore TypeChecker's prefix since we use qualifiedFieldName from inspector
7475
typeChecker := utils.NewTypeChecker(func(pass *analysis.Pass, ident *ast.Ident, node ast.Node, _ string) {
7576
checkNumericType(pass, ident, node, markersAccess, qualifiedFieldName)
7677
})

pkg/analysis/numericbounds/testdata/src/a/a.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,10 +145,6 @@ type SliceFields struct {
145145
InvalidSlice []int32 // want "SliceFields.InvalidSlice is missing minimum bound validation marker" "SliceFields.InvalidSlice is missing maximum bound validation marker"
146146

147147
InvalidSlice64 []int64 // want "SliceFields.InvalidSlice64 is missing minimum bound validation marker" "SliceFields.InvalidSlice64 is missing maximum bound validation marker"
148-
149-
// +kubebuilder:validation:items:Minimum=0
150-
// +kubebuilder:validation:items:Maximum=100
151-
ValidSliceWithBounds []int32
152148
}
153149

154150
// TypeAliasFields with type aliases should be checked

pkg/analysis/utils/type_check.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,14 @@ type TypeChecker interface {
3131
}
3232

3333
// NewTypeChecker returns a new TypeChecker with the provided checkFunc.
34-
func NewTypeChecker(isTypeFunc func(pass *analysis.Pass, ident ast.Expr) bool, checkFunc func(pass *analysis.Pass, expr ast.Expr, node ast.Node, prefix string)) TypeChecker {
34+
func NewTypeChecker(checkFunc func(pass *analysis.Pass, ident *ast.Ident, node ast.Node, qualifiedFieldName string)) TypeChecker {
3535
return &typeChecker{
36-
isTypeFunc: isTypeFunc,
37-
checkFunc: checkFunc,
36+
checkFunc: checkFunc,
3837
}
3938
}
4039

4140
type typeChecker struct {
42-
isTypeFunc func(pass *analysis.Pass, expr ast.Expr) bool
43-
checkFunc func(pass *analysis.Pass, expr ast.Expr, node ast.Node, prefix string)
41+
checkFunc func(pass *analysis.Pass, ident *ast.Ident, node ast.Node, qualifiedFieldName string)
4442
}
4543

4644
// CheckNode checks the provided node for built-in types.
@@ -86,11 +84,6 @@ func (t *typeChecker) checkTypeSpec(pass *analysis.Pass, tSpec *ast.TypeSpec, no
8684
}
8785

8886
func (t *typeChecker) checkTypeExpr(pass *analysis.Pass, typeExpr ast.Expr, node ast.Node, prefix string) {
89-
if t.isTypeFunc(pass, typeExpr) {
90-
t.checkFunc(pass, typeExpr, node, prefix)
91-
return
92-
}
93-
9487
switch typ := typeExpr.(type) {
9588
case *ast.Ident:
9689
t.checkIdent(pass, typ, node, prefix)
@@ -109,6 +102,12 @@ func (t *typeChecker) checkTypeExpr(pass *analysis.Pass, typeExpr ast.Expr, node
109102
// checkIdent calls the checkFunc with the ident, when we have hit a built-in type.
110103
// If the ident is not a built in, we look at the underlying type until we hit a built-in type.
111104
func (t *typeChecker) checkIdent(pass *analysis.Pass, ident *ast.Ident, node ast.Node, prefix string) {
105+
if IsBasicType(pass, ident) {
106+
// We've hit a built-in type, no need to check further.
107+
t.checkFunc(pass, ident, node, prefix)
108+
return
109+
}
110+
112111
tSpec, ok := LookupTypeSpec(pass, ident)
113112
if !ok {
114113
return

0 commit comments

Comments
 (0)