github.com/amarpal/go-tools@v0.0.0-20240422043104-40142f59f616/simple/s1033/testdata/src/example.com/CheckGuardedDelete/LintGuardedDelete.go (about)

     1  // Package pkg ...
     2  package pkg
     3  
     4  func fn(m map[int]int) {
     5  	if _, ok := m[0]; ok { //@ diag(`unnecessary guard`)
     6  		delete(m, 0)
     7  	}
     8  	if _, ok := m[0]; !ok {
     9  		delete(m, 0)
    10  	}
    11  	if _, ok := m[0]; ok {
    12  		println("deleting")
    13  		delete(m, 0)
    14  	}
    15  	if v, ok := m[0]; ok && v > 0 {
    16  		delete(m, 0)
    17  	}
    18  
    19  	var key int
    20  	if _, ok := m[key]; ok { //@ diag(`unnecessary guard`)
    21  		delete(m, key)
    22  	}
    23  	if _, ok := m[key]; ok {
    24  		delete(m, 0)
    25  	}
    26  	if _, ok := m[key]; ok {
    27  		delete(m, key)
    28  	} else {
    29  		println("not deleted")
    30  	}
    31  
    32  	var ok bool
    33  	if _, ok = m[key]; ok {
    34  		delete(m, 0)
    35  	}
    36  	if ok {
    37  		println("deleted")
    38  	}
    39  
    40  	delete := func(a, b interface{}) {}
    41  	if _, ok := m[0]; ok {
    42  		delete(m, 0)
    43  	}
    44  }