github.com/chenfeining/golangci-lint@v1.0.2-0.20230730162517-14c6c67868df/test/ruleguard/dup.go (about)

     1  // go:build ruleguard
     2  package ruleguard
     3  
     4  import "github.com/quasilyte/go-ruleguard/dsl"
     5  
     6  // Suppose that we want to report the duplicated left and right operands of binary operations.
     7  //
     8  // But if the operand has some side effects, this rule can cause false positives:
     9  // `f() && f()` can make sense (although it's not the best piece of code).
    10  //
    11  // This is where *filters* come to the rescue.
    12  func DupSubExpr(m dsl.Matcher) {
    13  	// All filters are written as a Where() argument.
    14  	// In our case, we need to assert that $x is "pure".
    15  	// It can be achieved by checking the m["x"] member Pure field.
    16  	m.Match(`$x || $x`,
    17  		`$x && $x`,
    18  		`$x | $x`,
    19  		`$x & $x`).
    20  		Where(m["x"].Pure).
    21  		Report(`suspicious identical LHS and RHS`)
    22  }