Skip to content

Commit bce00ef

Browse files
authored
Merge pull request #102 from pmalek/nomaps-allow-string-based-types-in-maps
nomaps: allow string based types in maps
2 parents 55d257d + 411fd3b commit bce00ef

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

pkg/analysis/nomaps/analyzer.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,6 @@ func (a *analyzer) run(pass *analysis.Pass) (any, error) {
6868
}
6969

7070
func (a *analyzer) checkField(pass *analysis.Pass, field *ast.Field) {
71-
stringToStringMapType := types.NewMap(types.Typ[types.String], types.Typ[types.String])
72-
7371
underlyingType := pass.TypesInfo.TypeOf(field.Type).Underlying()
7472

7573
if ptr, ok := underlyingType.(*types.Pointer); ok {
@@ -87,7 +85,8 @@ func (a *analyzer) checkField(pass *analysis.Pass, field *ast.Field) {
8785
}
8886

8987
if a.policy == config.NoMapsAllowStringToStringMaps {
90-
if types.Identical(m, stringToStringMapType) {
88+
if types.AssignableTo(m.Elem().Underlying(), types.Typ[types.String]) &&
89+
types.AssignableTo(m.Key().Underlying(), types.Typ[types.String]) {
9190
return
9291
}
9392

pkg/analysis/nomaps/testdata/src/c/c.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,11 @@ type Component struct {
6464
Key string `json:"key"`
6565
Value int32 `json:"value"`
6666
}
67+
68+
type StringBasedType string
69+
70+
type MapWithStringBasedTypes struct {
71+
MapWithStringBasedElement map[string]StringBasedType `json:"stringBasedMapElem,omitempty"`
72+
MapWithStringBasedKey map[StringBasedType]string `json:"stringBasedMapKey,omitempty"`
73+
MapWithStringBasedKeyAndElem map[StringBasedType]StringBasedType `json:"stringBasedMapKey,omitempty"`
74+
}

0 commit comments

Comments
 (0)