github.com/golangci/go-tools@v0.0.0-20190318060251-af6baa5dc196/simple/testdata/src/LintNilCheckAroundRange/LintNilCheckAroundRange.go (about)

     1  package pkg
     2  
     3  import "fmt"
     4  
     5  func main() {
     6  	str := []string{}
     7  
     8  	// range outside nil check should not match
     9  	for _, s := range str {
    10  		s = s + "B"
    11  	}
    12  
    13  	// body with multiple statements should not match
    14  	if str != nil {
    15  		str = append(str, "C")
    16  		for _, s := range str {
    17  			s = s + "D"
    18  		}
    19  	}
    20  
    21  	if str != nil { // MATCH /unnecessary nil check around range/
    22  		for _, s := range str {
    23  			s = s + "A"
    24  		}
    25  	}
    26  
    27  	var nilMap map[string]int
    28  	if nilMap != nil { // MATCH /unnecessary nil check around range/
    29  		for key, value := range nilMap {
    30  			nilMap[key] = value + 1
    31  		}
    32  	}
    33  
    34  	// range over channel can have nil check, as it is required to avoid blocking
    35  	var nilChan chan int
    36  	if nilChan != nil {
    37  		for v := range nilChan {
    38  			fmt.Println(v)
    39  		}
    40  	}
    41  }