Skip to content

Commit

Permalink
Always allow non-empty maps via literal
Browse files Browse the repository at this point in the history
  • Loading branch information
denisvmedia committed Sep 16, 2023
1 parent 8cc3ecb commit fe74add
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
5 changes: 5 additions & 0 deletions rule/enforce-map-style.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ func (r *EnforceMapStyleRule) Apply(file *lint.File, arguments lint.Arguments) [
return true
}

if len(v.Elts) > 0 {
// not an empty map
return true
}

failures = append(failures, lint.Failure{
Confidence: 1,
Node: v,
Expand Down
7 changes: 7 additions & 0 deletions testdata/enforce-map-style-literal.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ func somefn() {
m0 := make(map[string]string, 10)
m1 := make(map[string]string) // MATCH /use map[type]type{} instead of make(map[type]type)/
m2 := map[string]string{}
m3 := map[string]string{"k1": "v1", "k2": "v2"}

_ = m0
_ = m1
_ = m2
_ = m3
}

type Map map[string]string
Expand All @@ -16,10 +18,12 @@ func somefn2() {
m0 := make(Map, 10)
m1 := make(Map) // MATCH /use map[type]type{} instead of make(map[type]type)/
m2 := Map{}
m3 := Map{"k1": "v1", "k2": "v2"}

_ = m0
_ = m1
_ = m2
_ = m3
}

type MapMap Map
Expand All @@ -28,17 +32,20 @@ func somefn3() {
m0 := make(MapMap, 10)
m1 := make(MapMap) // MATCH /use map[type]type{} instead of make(map[type]type)/
m2 := MapMap{}
m3 := MapMap{"k1": "v1", "k2": "v2"}

_ = m0
_ = m1
_ = m2
_ = m3
}

func somefn4() {
m1 := map[string]map[string]string{}
m1["el0"] = make(map[string]string, 10)
m1["el1"] = make(map[string]string) // MATCH /use map[type]type{} instead of make(map[type]type)/
m1["el2"] = map[string]string{}
m1["el3"] = map[string]string{"k1": "v1", "k2": "v2"}

_ = m1
}
7 changes: 7 additions & 0 deletions testdata/enforce-map-style-make.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ func somefn() {
m0 := make(map[string]string, 10)
m1 := make(map[string]string)
m2 := map[string]string{} // MATCH /use make(map[type]type) instead of map[type]type{}/
m3 := map[string]string{"k1": "v1", "k2": "v2"}

_ = m0
_ = m1
_ = m2
_ = m3
}

type Map map[string]string
Expand All @@ -16,10 +18,12 @@ func somefn2() {
m0 := make(Map, 10)
m1 := make(Map)
m2 := Map{} // MATCH /use make(map[type]type) instead of map[type]type{}/
m3 := Map{"k1": "v1", "k2": "v2"}

_ = m0
_ = m1
_ = m2
_ = m3
}

type MapMap Map
Expand All @@ -28,17 +32,20 @@ func somefn3() {
m0 := make(MapMap, 10)
m1 := make(MapMap)
m2 := MapMap{} // MATCH /use make(map[type]type) instead of map[type]type{}/
m3 := MapMap{"k1": "v1", "k2": "v2"}

_ = m0
_ = m1
_ = m2
_ = m3
}

func somefn4() {
m1 := map[string]map[string]string{} // MATCH /use make(map[type]type) instead of map[type]type{}/
m1["el0"] = make(map[string]string, 10)
m1["el1"] = make(map[string]string)
m1["el2"] = map[string]string{} // MATCH /use make(map[type]type) instead of map[type]type{}/
m1["el3"] = map[string]string{"k1": "v1", "k2": "v2"}

_ = m1
}

0 comments on commit fe74add

Please sign in to comment.