Code Monkey home page Code Monkey logo

regexponce's Introduction

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)

regexponce's People

Contributors

budougumi0617 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.