github.com/golangci/go-tools@v0.0.0-20190318060251-af6baa5dc196/simple/testdata/src/nil-len/nil-len.go (about) 1 package pkg 2 3 func fn() { 4 var pa *[5]int 5 var s []int 6 var m map[int]int 7 var ch chan int 8 9 if s == nil || len(s) == 0 { // MATCH /should omit nil check/ 10 } 11 if m == nil || len(m) == 0 { // MATCH /should omit nil check/ 12 } 13 if ch == nil || len(ch) == 0 { // MATCH /should omit nil check/ 14 } 15 16 if s != nil && len(s) != 0 { // MATCH /should omit nil check/ 17 } 18 if m != nil && len(m) > 0 { // MATCH /should omit nil check/ 19 } 20 if s != nil && len(s) > 5 { // MATCH /should omit nil check/ 21 } 22 if s != nil && len(s) >= 5 { // MATCH /should omit nil check/ 23 } 24 const five = 5 25 if s != nil && len(s) == five { // MATCH /should omit nil check/ 26 } 27 28 if ch != nil && len(ch) == 5 { // MATCH /should omit nil check/ 29 } 30 31 if pa == nil || len(pa) == 0 { // nil check cannot be removed with pointer to an array 32 } 33 if s == nil || len(m) == 0 { // different variables 34 } 35 if s != nil && len(m) == 1 { // different variables 36 } 37 38 var ch2 chan int 39 if ch == ch2 || len(ch) == 0 { // not comparing with nil 40 } 41 if ch != ch2 && len(ch) != 0 { // not comparing with nil 42 } 43 44 const zero = 0 45 if s != nil && len(s) == zero { // nil check is not redundant here 46 } 47 if s != nil && len(s) == 0 { // nil check is not redundant here 48 } 49 if s != nil && len(s) >= 0 { // nil check is not redundant here (though len(s) >= 0 is) 50 } 51 one := 1 52 if s != nil && len(s) == one { // nil check is not redundant here 53 } 54 if s != nil && len(s) == len(m) { // nil check is not redundant here 55 } 56 if s != nil && len(s) != 1 { // nil check is not redundant here 57 } 58 if s != nil && len(s) < 5 { // nil check is not redundant here 59 } 60 if s != nil && len(s) <= 5 { // nil check is not redundant here 61 } 62 if s != nil && len(s) != len(ch) { // nil check is not redundant here 63 } 64 }