regexponce
regexp.{Must}Compile{POSIX}
should be called at once for performance.
Install
You can get regexponce
by go get
command.
$ go get -u github.com/budougumi0617/regexponce/cmd/regexponce
QuickStart
regexponce
run with go vet
as below when Go is 1.12 and higher.
$ go vet -vettool=$(which regexponce) ./...
Analyzer
regexp.Compile
is heavy, therefore the functions should be called jsut once in the process.
Analyzer confirms that the below functions are not called multiple times.
Target functions
Allow condition
- Called target functions in the package scope.
- Called target functions in
init
function.
- Called target functions in
main
function.
- Except if they are called in
for
loop.
- Add staticcheck's style comments
//lint:ignore regexponce REASON
Error condition
- Called target functions in normal function.
- Called target functions in for lop.
The warning sample is below.
package main
import (
"fmt"
"regexp"
)
func f() {
var validID = regexp.MustCompile(`^[a-z]+\[[0-9]+\]$`) // want `regexp.MustCompile must be called only once at initialize`
// lint:ignore regexponce allowed
validID = regexp.MustCompile(`^[a-z]+\[[0-9]+\]$`) // OK because add specify comment.
assign := regexp.MustCompile
assign(`^[a-z]+\[[0-9]+\]$`) // want `regexp.MustCompile must be called only once at initialize`
}
func main() {
var validID = regexp.MustCompile(`^[a-z]+\[[0-9]+\]$`) // OK because main function runs only once.
for i := 0; i < 10; i++ {
validID = regexp.MustCompile(`^[a-z]+\[[0-9]+\]$`) // want `regexp.MustCompile must be called only once at initialize`
}
}
Description
$ regexponce help regexponce
regexponce: Below functions should be called at once for performance.
- regexp.Compile
- regexp.MustCompile
- regexp.CompilePOSIX
- regexp.MustCompilePOSIX
Allow call in init, and main(except for in for loop) functions because each function is called only once.
Contribution
- Fork (https://github.com/budougumi0617/regexponce/fork)
- Create a feature branch
- Commit your changes
- Rebase your local changes against the master branch
- Run test suite with the
go test ./...
command and confirm that it passes
- Run
gofmt -s
- Create new Pull Request
License
MIT
Author
Yoichiro Shimizu(@budougumi0617)