Skip to content

Prevent analyzers from reporting on methods #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions pkg/analysis/commentstart/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,24 @@ func run(pass *analysis.Pass) (interface{}, error) {
return nil, errCouldNotGetJSONTags
}

// Filter to Fields so that we can iterate over fields in a struct.
// Filter to structs so that we can iterate over fields in a struct.
nodeFilter := []ast.Node{
(*ast.Field)(nil),
(*ast.StructType)(nil),
}

inspect.Preorder(nodeFilter, func(n ast.Node) {
field, ok := n.(*ast.Field)
sTyp, ok := n.(*ast.StructType)
if !ok {
return
}

checkField(pass, field, jsonTags)
if sTyp.Fields == nil {
return
}

for _, field := range sTyp.Fields.List {
checkField(pass, field, jsonTags)
}
})

return nil, nil //nolint:nilnil
Expand Down
3 changes: 3 additions & 0 deletions pkg/analysis/commentstart/testdata/src/a/a.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,6 @@ type CommentStartTestStruct struct {
// This comment just isn't correct at all, doesn't even start with anything resembling the field names. // want "godoc for field IncorrectComment should start with 'incorrectComment ...'"
IncorrectComment string `json:"incorrectComment"`
}

// DoNothing is used to check that the analyser doesn't report on methods.
func (CommentStartTestStruct) DoNothing() {}
3 changes: 3 additions & 0 deletions pkg/analysis/commentstart/testdata/src/a/a.go.golden
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,6 @@ type CommentStartTestStruct struct {
// This comment just isn't correct at all, doesn't even start with anything resembling the field names. // want "godoc for field IncorrectComment should start with 'incorrectComment ...'"
IncorrectComment string `json:"incorrectComment"`
}

// DoNothing is used to check that the analyser doesn't report on methods.
func (CommentStartTestStruct) DoNothing() {}
3 changes: 3 additions & 0 deletions pkg/analysis/conditions/testdata/src/a/a.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ type ValidConditions struct {
OtherField string `json:"otherField,omitempty"`
}

// DoNothing is used to check that the analyser doesn't report on methods.
func (ValidConditions) DoNothing() {}

type ConditionsNotFirst struct {
// other fields
OtherField string `json:"otherField,omitempty"`
Expand Down
3 changes: 3 additions & 0 deletions pkg/analysis/conditions/testdata/src/a/a.go.golden
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ type ValidConditions struct {
OtherField string `json:"otherField,omitempty"`
}

// DoNothing is used to check that the analyser doesn't report on methods.
func (ValidConditions) DoNothing() {}

type ConditionsNotFirst struct {
// other fields
OtherField string `json:"otherField,omitempty"`
Expand Down
2 changes: 1 addition & 1 deletion pkg/analysis/integers/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func run(pass *analysis.Pass) (interface{}, error) {
// Filter to fields so that we can look at fields within structs.
// Filter typespecs so that we can look at type aliases.
nodeFilter := []ast.Node{
(*ast.Field)(nil),
(*ast.StructType)(nil),
(*ast.TypeSpec)(nil),
}

Expand Down
5 changes: 5 additions & 0 deletions pkg/analysis/integers/testdata/src/a/a.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ type Integers struct {
InvalidMapUIntToString map[uint]string // want "field InvalidMapUIntToString map key should not use unsigned integers, use only int32 or int64 and apply validation to ensure the value is positive"
}

// DoNothing is used to check that the analyser doesn't report on methods.
func (Integers) DoNothing(a int, b uint) (int, uint) {
return 0, 0
}

type ValidInt32Alias int32

type ValidInt32PtrAlias *int32
Expand Down
12 changes: 9 additions & 3 deletions pkg/analysis/jsontags/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,22 @@ func (a *analyzer) run(pass *analysis.Pass) (interface{}, error) {

// Filter to fields so that we can iterate over fields in a struct.
nodeFilter := []ast.Node{
(*ast.Field)(nil),
(*ast.StructType)(nil),
}

inspect.Preorder(nodeFilter, func(n ast.Node) {
field, ok := n.(*ast.Field)
sTyp, ok := n.(*ast.StructType)
if !ok {
return
}

a.checkField(pass, field, jsonTags)
if sTyp.Fields == nil {
return
}

for _, field := range sTyp.Fields.List {
a.checkField(pass, field, jsonTags)
}
})

return nil, nil //nolint:nilnil
Expand Down
9 changes: 9 additions & 0 deletions pkg/analysis/jsontags/testdata/src/a/a.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package a

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

type JSONTagTestStruct struct {
NoJSONTag string // want "field NoJSONTag is missing json tag"
EmptyJSONTag string `json:""` // want "field EmptyJSONTag has empty json tag"
Expand All @@ -20,10 +24,15 @@ type JSONTagTestStruct struct {
C // want "embedded field C is missing json tag"
D `json:""` // want "embedded field D has empty json tag"
E `json:"e-"` // want "embedded field E json tag does not match pattern \"\\^\\[a-z\\]\\[a-z0-9\\]\\*\\(\\?:\\[A-Z\\]\\[a-z0-9\\]\\*\\)\\*\\$\": e-"

metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`
}

type A struct{}

func (A) DoNothing() {}

type B struct{}

type C struct{}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*
This is a copy of the minimum amount of the original file to be able to test the jsontags linter.
*/
package v1

type TypeMeta struct{}

type ObjectMeta struct{}
2 changes: 1 addition & 1 deletion pkg/analysis/nobools/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func run(pass *analysis.Pass) (interface{}, error) {
// Filter to fields so that we can look at fields within structs.
// Filter typespecs so that we can look at type aliases.
nodeFilter := []ast.Node{
(*ast.Field)(nil),
(*ast.StructType)(nil),
(*ast.TypeSpec)(nil),
}

Expand Down
7 changes: 6 additions & 1 deletion pkg/analysis/nobools/testdata/src/a/a.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package a

type Integers struct {
type Bools struct {
ValidString string

ValidMap map[string]string
Expand Down Expand Up @@ -34,6 +34,11 @@ type Integers struct {
InvalidMapBoolPtrToString map[*bool]string // want "field InvalidMapBoolPtrToString map key pointer should not use a bool. Use a string type with meaningful constant values as an enum."
}

// DoNothing is used to check that the analyser doesn't report on methods.
func (Bools) DoNothing(a bool) bool {
return a
}

type BoolAlias bool // want "type BoolAlias should not use a bool. Use a string type with meaningful constant values as an enum."

type BoolAliasPtr *bool // want "type BoolAliasPtr pointer should not use a bool. Use a string type with meaningful constant values as an enum."
Expand Down
3 changes: 3 additions & 0 deletions pkg/analysis/nophase/testdata/src/a/a.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ type NoPhaseTestStruct struct {

}

// DoNothing is used to check that the analyser doesn't report on methods.
func (NoPhaseTestStruct) DoNothing() {}

type NoSubPhaseTestStruct struct {
// +optional
FooPhase *string `json:"fooPhase,omitempty"` // want "field FooPhase: phase fields are deprecated and conditions should be preferred, avoid phase like enum fields"
Expand Down
16 changes: 11 additions & 5 deletions pkg/analysis/optionalorrequired/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,19 +93,25 @@ func (a *analyzer) run(pass *analysis.Pass) (interface{}, error) {

// Filter to fields so that we can iterate over fields in a struct.
nodeFilter := []ast.Node{
(*ast.Field)(nil),
(*ast.StructType)(nil),
}

inspect.Preorder(nodeFilter, func(n ast.Node) {
field, ok := n.(*ast.Field)
sTyp, ok := n.(*ast.StructType)
if !ok {
return
}

fieldMarkers := markersAccess.FieldMarkers(field)
fieldTagInfo := jsonTags.FieldTags(field)
if sTyp.Fields == nil {
return
}

a.checkField(pass, field, fieldMarkers, fieldTagInfo)
for _, field := range sTyp.Fields.List {
fieldMarkers := markersAccess.FieldMarkers(field)
fieldTagInfo := jsonTags.FieldTags(field)

a.checkField(pass, field, fieldMarkers, fieldTagInfo)
}
})

return nil, nil //nolint:nilnil
Expand Down
3 changes: 3 additions & 0 deletions pkg/analysis/optionalorrequired/testdata/src/a/a.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ type OptionalOrRequiredTestStruct struct {

type A struct{}

// DoNothing is used to check that the analyser doesn't report on methods.
func (A) DoNothing() {}

type B struct{}

type C struct{}
3 changes: 3 additions & 0 deletions pkg/analysis/optionalorrequired/testdata/src/a/a.go.golden
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ type OptionalOrRequiredTestStruct struct {

type A struct{}

// DoNothing is used to check that the analyser doesn't report on methods.
func (A) DoNothing() {}

type B struct{}

type C struct{}
3 changes: 3 additions & 0 deletions pkg/analysis/requiredfields/testdata/src/a/a.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,6 @@ type A struct {
// +kubebuilder:validation:Required
RequiredKubebuilderMarkerPointerOmitEmptyField *string `json:"requiredKubebuilderMarkerPointerOmitEmptyField,omitempty"` // want "field RequiredKubebuilderMarkerPointerOmitEmptyField is marked as required, but has the omitempty tag" "field RequiredKubebuilderMarkerPointerOmitEmptyField is marked as required, should not be a pointer"
}

// DoNothing is used to check that the analyser doesn't report on methods.
func (A) DoNothing() {}
3 changes: 3 additions & 0 deletions pkg/analysis/requiredfields/testdata/src/a/a.go.golden
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,6 @@ type A struct {
// +kubebuilder:validation:Required
RequiredKubebuilderMarkerPointerOmitEmptyField string `json:"requiredKubebuilderMarkerPointerOmitEmptyField"` // want "field RequiredKubebuilderMarkerPointerOmitEmptyField is marked as required, but has the omitempty tag" "field RequiredKubebuilderMarkerPointerOmitEmptyField is marked as required, should not be a pointer"
}

// DoNothing is used to check that the analyser doesn't report on methods.
func (A) DoNothing() {}
Loading