github.com/amarpal/go-tools@v0.0.0-20240422043104-40142f59f616/staticcheck/sa1000/testdata/src/example.com/CheckRegexps/CheckRegexps.go (about)

     1  package pkg
     2  
     3  import (
     4  	"log"
     5  	"regexp"
     6  )
     7  
     8  const c1 = `[`
     9  const c2 = `(abc)`
    10  
    11  var re1 = regexp.MustCompile(`ab\yef`) //@ diag(`error parsing regexp`)
    12  var re2 = regexp.MustCompile(c1)       //@ diag(`error parsing regexp`)
    13  var re3 = regexp.MustCompile(c2)
    14  var re4 = regexp.MustCompile(
    15  	c1, //@ diag(`error parsing regexp`)
    16  )
    17  
    18  func fn() {
    19  	_, err := regexp.Compile(`foo(`) //@ diag(`error parsing regexp`)
    20  	if err != nil {
    21  		panic(err)
    22  	}
    23  	if re2.MatchString("foo(") {
    24  		log.Println("of course 'foo(' matches 'foo('")
    25  	}
    26  
    27  	regexp.Match("foo(", nil)       //@ diag(`error parsing regexp`)
    28  	regexp.MatchReader("foo(", nil) //@ diag(`error parsing regexp`)
    29  	regexp.MatchString("foo(", "")  //@ diag(`error parsing regexp`)
    30  }
    31  
    32  // must be a basic type to trigger SA4017 (in case of a test failure)
    33  type T string
    34  
    35  func (T) Fn() {}
    36  
    37  // Don't get confused by methods named init
    38  func (T) init() {}
    39  
    40  // this will become a synthetic init function, that we don't want to
    41  // ignore
    42  var _ = regexp.MustCompile("(") //@ diag(`error parsing regexp`)
    43  
    44  func fn2() {
    45  	regexp.MustCompile("foo(").FindAll(nil, 0) //@ diag(`error parsing regexp`)
    46  }