github.com/azazeal/revive@v1.0.9/testdata/unnecessary-stmt.go (about)

     1  package fixtures
     2  
     3  func foo(a, b, c, d int) {
     4  	switch n := node.(type) { // MATCH /switch with only one case can be replaced by an if-then/
     5  	case *ast.SwitchStmt:
     6  		caseSelector := func(n ast.Node) bool {
     7  			_, ok := n.(*ast.CaseClause)
     8  			return ok
     9  		}
    10  		cases := pick(n.Body, caseSelector, nil)
    11  		if len(cases) == 1 {
    12  			cs, ok := cases[0].(*ast.CaseClause)
    13  			if ok && len(cs.List) == 1 {
    14  				w.onFailure(lint.Failure{
    15  					Confidence: 1,
    16  					Node:       n,
    17  					Category:   "style",
    18  					Failure:    "switch can be replaced by an if-then",
    19  				})
    20  			}
    21  		}
    22  	}
    23  }
    24  
    25  func bar() {
    26  	a := 1
    27  
    28  	switch a {
    29  	case 1, 2:
    30  		a++
    31  	}
    32  
    33  loop:
    34  	for {
    35  		switch a {
    36  		case 1:
    37  			a++
    38  			println("one")
    39  			break // MATCH /omit unnecessary break at the end of case clause/
    40  		case 2:
    41  			println("two")
    42  			break loop
    43  		default:
    44  			println("default")
    45  		}
    46  	}
    47  
    48  	return // MATCH /omit unnecessary return statement/
    49  }