From fe74addd7401a895ff846ad336d44df5bab3f26b Mon Sep 17 00:00:00 2001 From: Denis Vojtuk <5462781+denisvmedia@users.noreply.github.com> Date: Sat, 16 Sep 2023 16:51:26 +0200 Subject: [PATCH] Always allow non-empty maps via literal --- rule/enforce-map-style.go | 5 +++++ testdata/enforce-map-style-literal.go | 7 +++++++ testdata/enforce-map-style-make.go | 7 +++++++ 3 files changed, 19 insertions(+) diff --git a/rule/enforce-map-style.go b/rule/enforce-map-style.go index baa8c2b7d..7517a8001 100644 --- a/rule/enforce-map-style.go +++ b/rule/enforce-map-style.go @@ -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, diff --git a/testdata/enforce-map-style-literal.go b/testdata/enforce-map-style-literal.go index 55bb26b96..a64d2e410 100644 --- a/testdata/enforce-map-style-literal.go +++ b/testdata/enforce-map-style-literal.go @@ -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 @@ -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 @@ -28,10 +32,12 @@ 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() { @@ -39,6 +45,7 @@ func somefn4() { 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 } diff --git a/testdata/enforce-map-style-make.go b/testdata/enforce-map-style-make.go index 60ba96fe1..e990dce11 100644 --- a/testdata/enforce-map-style-make.go +++ b/testdata/enforce-map-style-make.go @@ -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 @@ -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 @@ -28,10 +32,12 @@ 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() { @@ -39,6 +45,7 @@ func somefn4() { 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 }