github.com/amarpal/go-tools@v0.0.0-20240422043104-40142f59f616/simple/s1036/testdata/src/example.com/CheckUnnecessaryGuard/LintUnnecessaryGuard.go (about) 1 package pkg 2 3 func fn() { 4 var m = map[string][]string{} 5 6 if _, ok := m["k1"]; ok { //@ diag(`unnecessary guard around map access`) 7 m["k1"] = append(m["k1"], "v1", "v2") 8 } else { 9 m["k1"] = []string{"v1", "v2"} 10 } 11 12 if _, ok := m["k1"]; ok { 13 m["k1"] = append(m["k1"], "v1", "v2") 14 } else { 15 m["k1"] = []string{"v1"} 16 } 17 18 if _, ok := m["k1"]; ok { 19 m["k2"] = append(m["k2"], "v1") 20 } else { 21 m["k1"] = []string{"v1"} 22 } 23 24 k1 := "key" 25 if _, ok := m[k1]; ok { //@ diag(`unnecessary guard around map access`) 26 m[k1] = append(m[k1], "v1", "v2") 27 } else { 28 m[k1] = []string{"v1", "v2"} 29 } 30 31 // ellipsis is not currently supported 32 v := []string{"v1", "v2"} 33 if _, ok := m["k1"]; ok { 34 m["k1"] = append(m["k1"], v...) 35 } else { 36 m["k1"] = v 37 } 38 39 var m2 map[string]int 40 if _, ok := m2["k"]; ok { //@ diag(`unnecessary guard around map access`) 41 m2["k"] += 4 42 } else { 43 m2["k"] = 4 44 } 45 46 if _, ok := m2["k"]; ok { 47 m2["k"] += 4 48 } else { 49 m2["k"] = 3 50 } 51 52 if _, ok := m2["k"]; ok { //@ diag(`unnecessary guard around map access`) 53 m2["k"]++ 54 } else { 55 m2["k"] = 1 56 } 57 58 if _, ok := m2["k"]; ok { 59 m2["k"] -= 1 60 } else { 61 m2["k"] = 1 62 } 63 } 64 65 // this used to cause a panic in the pattern package 66 func fn2() { 67 var obj interface{} 68 69 if _, ok := obj.(map[string]interface{})["items"]; ok { 70 obj.(map[string]interface{})["version"] = 1 71 } 72 }