regexponce

package module
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 11, 2020 License: MIT Imports: 10 Imported by: 0

README

regexponce

PkgGoDev CircleCI

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

  1. Fork (https://github.com/budougumi0617/regexponce/fork)
  2. Create a feature branch
  3. Commit your changes
  4. Rebase your local changes against the master branch
  5. Run test suite with the go test ./... command and confirm that it passes
  6. Run gofmt -s
  7. Create new Pull Request

License

MIT

Author

Yoichiro Shimizu(@budougumi0617)

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Analyzer = &analysis.Analyzer{
	Name: "regexponce",
	Doc:  doc,
	Run:  run,
	Requires: []*analysis.Analyzer{
		buildssa.Analyzer,
		commentmap.Analyzer,
	},
}

Analyzer is check correct call of regexp package.

Functions

This section is empty.

Types

This section is empty.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL