diff options
| author | Dmitry Vyukov <dvyukov@google.com> | 2021-02-22 20:37:25 +0100 |
|---|---|---|
| committer | Dmitry Vyukov <dvyukov@google.com> | 2021-02-22 21:02:12 +0100 |
| commit | fcc6d71be2c3ce7d9305c04fc2e87af554571bac (patch) | |
| tree | b01dbb3d1e2988e28ea158d2d543d603ec0b9569 /vendor/github.com/tommy-muehle | |
| parent | 8f23c528ad5a943b9ffec5dcaf332fd0f614006e (diff) | |
go.mod: update golangci-lint to v1.37
Diffstat (limited to 'vendor/github.com/tommy-muehle')
26 files changed, 568 insertions, 251 deletions
diff --git a/vendor/github.com/tommy-muehle/go-mnd/.goreleaser.yml b/vendor/github.com/tommy-muehle/go-mnd/.goreleaser.yml deleted file mode 100644 index 0986ff2f0..000000000 --- a/vendor/github.com/tommy-muehle/go-mnd/.goreleaser.yml +++ /dev/null @@ -1,30 +0,0 @@ -builds: - - - main: ./cmd/mnd/main.go - binary: mnd - goos: - - windows - - darwin - - linux - goarch: - - amd64 - ldflags: -s -w -X main.version={{.Version}} -X main.commit={{.Commit}} -X main.buildTime={{.Date}}`. - -archives: - - - format: tar.gz - format_overrides: - - goos: windows - format: zip - -brews: - - - name: mnd - github: - owner: tommy-muehle - name: homebrew-tap - folder: Formula - homepage: https://github.com/tommy-muehle/go-mnd - description: Magic number detector for Go - test: | - system "#{bin}/mnd --version" diff --git a/vendor/github.com/tommy-muehle/go-mnd/.travis.yml b/vendor/github.com/tommy-muehle/go-mnd/.travis.yml deleted file mode 100644 index fd76a2fc6..000000000 --- a/vendor/github.com/tommy-muehle/go-mnd/.travis.yml +++ /dev/null @@ -1,12 +0,0 @@ -language: go - -go: - - 1.13.x - - 1.12.x - - tip - -script: - - go test -v ./... - -notifications: - email: false diff --git a/vendor/github.com/tommy-muehle/go-mnd/README.md b/vendor/github.com/tommy-muehle/go-mnd/README.md deleted file mode 100644 index a85ed780c..000000000 --- a/vendor/github.com/tommy-muehle/go-mnd/README.md +++ /dev/null @@ -1,111 +0,0 @@ -# go-mnd - Magic number detector for Golang - -A vet analyzer to detect magic numbers. - -> **What is a magic number?** -> A magic number is a numeric literal that is not defined as a constant, but which may change, and therefore can be hard to update. It's considered a bad programming practice to use numbers directly in any source code without an explanation. It makes programs harder to read, understand, and maintain. - -## Project status - -[](https://travis-ci.org/tommy-muehle/go-mnd) -[](https://goreportcard.com/report/github.com/tommy-muehle/go-mnd) - -## Install - -This analyzer requires Golang in version >= 1.12 because it's depends on the **go/analysis** API. - -``` -go get github.com/tommy-muehle/go-mnd/cmd/mnd -``` - -To install with [Homebrew](https://brew.sh/), run: - -``` -brew tap tommy-muehle/tap && brew install tommy-muehle/tap/mnd -``` - -On Windows download the [latest release](https://github.com/tommy-muehle/go-mnd/releases). - -## Usage - -[](https://asciinema.org/a/231021) - -``` -go vet -vettool $(which mnd) ./... -``` - -or directly - -``` -mnd ./... -``` - -The ```-checks``` option let's you define a comma separated list of checks. - -The ```-ignored-numbers``` option let's you define a comma separated list of numbers to ignore. - -The ```-excludes``` option let's you define a comma separated list of regexp patterns to exclude. - -## Checks - -By default this detector analyses arguments, assigns, cases, conditions, operations and return statements. - -* argument - -``` -t := http.StatusText(200) -``` - -* assign - -``` -c := &http.Client{ - Timeout: 5 * time.Second, -} -``` - -* case - -``` -switch x { - case 3: -} -``` - -* condition - -``` -if x > 7 { -} -``` - -* operation - -``` -var x, y int -y = 10 * x -``` - -* return - -``` -return 3 -``` - -## Excludes - -By default the numbers 0 and 1 as well as test files are excluded! - -### Further known excludes - -The function "Date" in the "Time" package. - -``` -t := time.Date(2017, time.September, 26, 12, 13, 14, 0, time.UTC) -``` - -Additional custom excludes can be defined via option flag. - -## License - -The MIT License (MIT). Please see [LICENSE](LICENSE) for more information. diff --git a/vendor/github.com/tommy-muehle/go-mnd/go.mod b/vendor/github.com/tommy-muehle/go-mnd/go.mod deleted file mode 100644 index 5119a7cc1..000000000 --- a/vendor/github.com/tommy-muehle/go-mnd/go.mod +++ /dev/null @@ -1,11 +0,0 @@ -module github.com/tommy-muehle/go-mnd - -go 1.12 - -require ( - github.com/davecgh/go-spew v1.1.1 // indirect - github.com/stretchr/objx v0.1.1 // indirect - github.com/stretchr/testify v1.3.0 - golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4 // indirect - golang.org/x/tools v0.0.0-20190221204921-83362c3779f5 -) diff --git a/vendor/github.com/tommy-muehle/go-mnd/go.sum b/vendor/github.com/tommy-muehle/go-mnd/go.sum deleted file mode 100644 index bdc021ab2..000000000 --- a/vendor/github.com/tommy-muehle/go-mnd/go.sum +++ /dev/null @@ -1,18 +0,0 @@ -github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= -github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= -github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= -github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= -github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= -golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= -golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= -golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= -golang.org/x/tools v0.0.0-20190221204921-83362c3779f5 h1:ev5exjGDsOo0NPTB0qdCcE53BfWl1IICJlhgXgfT9fM= -golang.org/x/tools v0.0.0-20190221204921-83362c3779f5/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY= -google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= diff --git a/vendor/github.com/tommy-muehle/go-mnd/.editorconfig b/vendor/github.com/tommy-muehle/go-mnd/v2/.editorconfig index 316b8cae0..fe2c20fb0 100644 --- a/vendor/github.com/tommy-muehle/go-mnd/.editorconfig +++ b/vendor/github.com/tommy-muehle/go-mnd/v2/.editorconfig @@ -9,4 +9,13 @@ insert_final_newline = true trim_trailing_whitespace = true [*.md] -trim_trailing_whitespace = false
\ No newline at end of file +trim_trailing_whitespace = false + +[*.json] +indent_size = 2 + +[*.{yaml,yml}] +indent_size = 2 + +[Makefile] +indent_style = tab diff --git a/vendor/github.com/tommy-muehle/go-mnd/v2/.gitattributes b/vendor/github.com/tommy-muehle/go-mnd/v2/.gitattributes new file mode 100644 index 000000000..005358190 --- /dev/null +++ b/vendor/github.com/tommy-muehle/go-mnd/v2/.gitattributes @@ -0,0 +1,9 @@ +/.gitattributes export-ignore +/.gitignore export-ignore +/.editorconfig export-ignore +/.goreleaser.yml export-ignore +/.github/ export-ignore +/examples/ export-ignore +/testdata/ export-ignore +/tools/ export-ignore +/Makefile export-ignore diff --git a/vendor/github.com/tommy-muehle/go-mnd/.gitignore b/vendor/github.com/tommy-muehle/go-mnd/v2/.gitignore index edd9d60a7..abc11b330 100644 --- a/vendor/github.com/tommy-muehle/go-mnd/.gitignore +++ b/vendor/github.com/tommy-muehle/go-mnd/v2/.gitignore @@ -1,2 +1,3 @@ build/ dist/ +coverage.txt diff --git a/vendor/github.com/tommy-muehle/go-mnd/v2/.goreleaser.yml b/vendor/github.com/tommy-muehle/go-mnd/v2/.goreleaser.yml new file mode 100644 index 000000000..e0a87b838 --- /dev/null +++ b/vendor/github.com/tommy-muehle/go-mnd/v2/.goreleaser.yml @@ -0,0 +1,27 @@ +builds: + - main: ./cmd/mnd/main.go + binary: mnd + goos: + - windows + - darwin + - linux + goarch: + - amd64 + ldflags: -s -w -X main.version={{.Version}} -X main.commit={{.Commit}} -X main.buildTime={{.Date}}`. + +archives: + - format: tar.gz + format_overrides: + - goos: windows + format: zip + +brews: + - name: mnd + github: + owner: tommy-muehle + name: homebrew-tap + folder: Formula + homepage: https://github.com/tommy-muehle/go-mnd + description: Magic number detector for Go + test: | + system "#{bin}/mnd --version" diff --git a/vendor/github.com/tommy-muehle/go-mnd/v2/Dockerfile b/vendor/github.com/tommy-muehle/go-mnd/v2/Dockerfile new file mode 100644 index 000000000..bb8e2b7f4 --- /dev/null +++ b/vendor/github.com/tommy-muehle/go-mnd/v2/Dockerfile @@ -0,0 +1,17 @@ +ARG GO_VERSION=1.15 + +FROM golang:${GO_VERSION}-alpine AS builder +RUN apk add --update --no-cache make git curl gcc libc-dev +RUN mkdir -p /build +WORKDIR /build +COPY . /build/ +RUN go mod download +RUN go build -o go-mnd cmd/mnd/main.go + +FROM golang:${GO_VERSION}-alpine +RUN apk add --update --no-cache bash git gcc libc-dev +COPY --from=builder /build/go-mnd /bin/go-mnd +COPY entrypoint.sh /bin/entrypoint.sh +VOLUME /app +WORKDIR /app +ENTRYPOINT ["/bin/entrypoint.sh"] diff --git a/vendor/github.com/tommy-muehle/go-mnd/LICENSE b/vendor/github.com/tommy-muehle/go-mnd/v2/LICENSE index 8825fad20..8825fad20 100644 --- a/vendor/github.com/tommy-muehle/go-mnd/LICENSE +++ b/vendor/github.com/tommy-muehle/go-mnd/v2/LICENSE diff --git a/vendor/github.com/tommy-muehle/go-mnd/v2/Makefile b/vendor/github.com/tommy-muehle/go-mnd/v2/Makefile new file mode 100644 index 000000000..b8a32316b --- /dev/null +++ b/vendor/github.com/tommy-muehle/go-mnd/v2/Makefile @@ -0,0 +1,32 @@ +GIT_TAG?= $(shell git describe --abbrev=0) + +GO_VERSION = 1.15 +BUILDFLAGS := '-w -s' + +IMAGE_REPO = "tommymuehle" +BIN = "go-mnd" + +clean: + rm -rf build dist coverage.txt + +test: + go test -race ./... + +test-coverage: + go test -race -coverprofile=coverage.txt -covermode=atomic -coverpkg=./checks,./config + +build: + go build -o build/$(BIN) cmd/mnd/main.go + +image: + @echo "Building the Docker image..." + docker build --rm -t $(IMAGE_REPO)/$(BIN):$(GIT_TAG) --build-arg GO_VERSION=$(GO_VERSION) . + docker tag $(IMAGE_REPO)/$(BIN):$(GIT_TAG) $(IMAGE_REPO)/$(BIN):$(GIT_TAG) + docker tag $(IMAGE_REPO)/$(BIN):$(GIT_TAG) $(IMAGE_REPO)/$(BIN):latest + +image-push: image + @echo "Pushing the Docker image..." + docker push $(IMAGE_REPO)/$(BIN):$(GIT_TAG) + docker push $(IMAGE_REPO)/$(BIN):latest + +.PHONY: clean test test-coverage build image image-push diff --git a/vendor/github.com/tommy-muehle/go-mnd/v2/README.md b/vendor/github.com/tommy-muehle/go-mnd/v2/README.md new file mode 100644 index 000000000..fe679e138 --- /dev/null +++ b/vendor/github.com/tommy-muehle/go-mnd/v2/README.md @@ -0,0 +1,230 @@ +# go-mnd - Magic number detector for Golang + +<img align="right" width="250px" src="https://github.com/tommy-muehle/go-mnd/blob/master/images/logo.png"> + +A vet analyzer to detect magic numbers. + +> **What is a magic number?** +> A magic number is a numeric literal that is not defined as a constant, but which may change, and therefore can be hard to update. It's considered a bad programming practice to use numbers directly in any source code without an explanation. It makes programs harder to read, understand, and maintain. + +## Project status + + +[](https://goreportcard.com/report/github.com/tommy-muehle/go-mnd) +[](https://codecov.io/gh/tommy-muehle/go-mnd) + +## Install + +### Local + +This analyzer requires Golang in version >= 1.12 because it's depends on the **go/analysis** API. + +``` +go get -u github.com/tommy-muehle/go-mnd/cmd/mnd +``` + +### Github action + +You can run go-mnd as a GitHub action as follows: + +``` +name: Example workflow +on: + push: + branches: + - master + pull_request: + branches: + - master +jobs: + tests: + runs-on: ubuntu-latest + env: + GO111MODULE: on + steps: + - name: Checkout Source + uses: actions/checkout@v2 + - name: Run go-mnd + uses: tommy-muehle/go-mnd@master + with: + args: ./... +``` + +### GitLab CI + +You can run go-mnd inside a GitLab CI pipeline as follows: + +``` +stages: + - lint + +go:lint:mnd: + stage: lint + needs: [] + image: golang:latest + before_script: + - go get -u github.com/tommy-muehle/go-mnd/cmd/mnd + - go mod tidy + - go mod vendor + script: + - go vet -vettool $(which mnd) ./... +``` + +### Homebrew + +To install with [Homebrew](https://brew.sh/), run: + +``` +brew tap tommy-muehle/tap && brew install tommy-muehle/tap/mnd +``` + +### Docker + +To get the latest available Docker image: + +``` +docker pull tommymuehle/go-mnd +``` + +### Windows + +On Windows download the [latest release](https://github.com/tommy-muehle/go-mnd/releases). + +## Usage + +[](https://asciinema.org/a/231021) + +``` +go vet -vettool $(which mnd) ./... +``` + +or directly + +``` +mnd ./... +``` + +or via Docker + +``` +docker run --rm -v "$PWD":/app -w /app tommymuehle/go-mnd:latest ./... +``` + +## Options + +The ```-checks``` option let's you define a comma separated list of checks. + +The ```-ignored-numbers``` option let's you define a comma separated list of numbers to ignore. +For example: `-ignored-numbers=1000,10_000,3.14159264` + +The ```-ignored-functions``` option let's you define a comma separated list of function name regexp patterns to exclude. +For example: `-ignored-functions=math.*,http.StatusText` + +The ```-ignored-files``` option let's you define a comma separated list of filename regexp patterns to exclude. +For example: `-ignored-files=magic_.*.go,.*_numbers.go` + +## Checks + +By default this detector analyses arguments, assigns, cases, conditions, operations and return statements. + +* argument + +``` +t := http.StatusText(200) +``` + +* assign + +``` +c := &http.Client{ + Timeout: 5 * time.Second, +} +``` + +* case + +``` +switch x { + case 3: +} +``` + +* condition + +``` +if x > 7 { +} +``` + +* operation + +``` +var x, y int +y = 10 * x +``` + +* return + +``` +return 3 +``` + +## Excludes + +By default the numbers 0 and 1 as well as test files are excluded! + +### Further known excludes + +The function "Date" in the "Time" package. + +``` +t := time.Date(2017, time.September, 26, 12, 13, 14, 0, time.UTC) +``` + +Additional custom excludes can be defined via option flag. + +## Development + +### Build + +You can build the binary with: + +``` +make +``` + +### Tests + +You can run all unit tests using: + +``` +make test +``` + +And with coverage report: + +``` +make test-coverage +``` + +### Docker image + +You can also build locally the docker image by using the command: + +``` +make image +``` + +## Stickers + +<p style="float: left;"> + <img alt="Stickers image" width="200px" src="https://github.com/tommy-muehle/go-mnd/blob/master/images/stickers.jpg" /> + <img alt="Sticker image" width="200px" src="https://github.com/tommy-muehle/go-mnd/blob/master/images/sticker.jpg" /> +</p> + +Just drop me a message via Twitter DM or email if you want some go-mnd stickers +for you or your Gopher usergroup. + +## License + +The MIT License (MIT). Please see [LICENSE](LICENSE) for more information. diff --git a/vendor/github.com/tommy-muehle/go-mnd/v2/action.yml b/vendor/github.com/tommy-muehle/go-mnd/v2/action.yml new file mode 100644 index 000000000..3a1f8eb11 --- /dev/null +++ b/vendor/github.com/tommy-muehle/go-mnd/v2/action.yml @@ -0,0 +1,19 @@ +name: 'go-mnd' +description: 'Runs the Golang magic number detector' +author: '@tommy-muehle' + +inputs: + args: + description: 'Arguments for go-mnd' + required: true + default: '-h' + +runs: + using: 'docker' + image: 'Dockerfile' + args: + - ${{ inputs.args }} + +branding: + icon: 'check-circle' + color: 'blue' diff --git a/vendor/github.com/tommy-muehle/go-mnd/analyzer.go b/vendor/github.com/tommy-muehle/go-mnd/v2/analyzer.go index 9930170f2..9153e0bc3 100644 --- a/vendor/github.com/tommy-muehle/go-mnd/analyzer.go +++ b/vendor/github.com/tommy-muehle/go-mnd/v2/analyzer.go @@ -3,9 +3,10 @@ package magic_numbers import ( "flag" "go/ast" + "strings" - "github.com/tommy-muehle/go-mnd/checks" - "github.com/tommy-muehle/go-mnd/config" + "github.com/tommy-muehle/go-mnd/v2/checks" + "github.com/tommy-muehle/go-mnd/v2/config" "golang.org/x/tools/go/analysis" "golang.org/x/tools/go/analysis/passes/inspect" @@ -30,8 +31,10 @@ type Checker interface { func options() flag.FlagSet { options := flag.NewFlagSet("", flag.ExitOnError) - options.String("excludes", "", "comma separated list of patterns to exclude from analysis") - options.String("ignored-numbers", "", "comma separated list of numbers excluded from analysis") + options.String("excludes", "", "deprecated: use ignored-files instead") + options.String("ignored-files", "", "comma separated list of file patterns to exclude from analysis") + options.String("ignored-functions", "", "comma separated list of function patterns to exclude from analysis") + options.String("ignored-numbers", "", "comma separated list of numbers to exclude from analysis") options.String( "checks", checks.ArgumentCheck+","+ @@ -47,9 +50,24 @@ func options() flag.FlagSet { } func run(pass *analysis.Pass) (interface{}, error) { + var ignoredFiles string + + ignoredFiles = strings.Join( + []string{ + pass.Analyzer.Flags.Lookup("excludes").Value.String(), // is deprecated + pass.Analyzer.Flags.Lookup("ignored-files").Value.String(), + }, + ",", + ) + + if ignoredFiles == "," { + ignoredFiles = "" + } + conf := config.WithOptions( config.WithCustomChecks(pass.Analyzer.Flags.Lookup("checks").Value.String()), - config.WithExcludes(pass.Analyzer.Flags.Lookup("excludes").Value.String()), + config.WithIgnoredFiles(ignoredFiles), + config.WithIgnoredFunctions(pass.Analyzer.Flags.Lookup("ignored-functions").Value.String()), config.WithIgnoredNumbers(pass.Analyzer.Flags.Lookup("ignored-numbers").Value.String()), ) @@ -57,18 +75,23 @@ func run(pass *analysis.Pass) (interface{}, error) { if conf.IsCheckEnabled(checks.ArgumentCheck) { checker = append(checker, checks.NewArgumentAnalyzer(pass, conf)) } + if conf.IsCheckEnabled(checks.CaseCheck) { checker = append(checker, checks.NewCaseAnalyzer(pass, conf)) } + if conf.IsCheckEnabled(checks.ConditionCheck) { checker = append(checker, checks.NewConditionAnalyzer(pass, conf)) } + if conf.IsCheckEnabled(checks.OperationCheck) { checker = append(checker, checks.NewOperationAnalyzer(pass, conf)) } + if conf.IsCheckEnabled(checks.ReturnCheck) { checker = append(checker, checks.NewReturnAnalyzer(pass, conf)) } + if conf.IsCheckEnabled(checks.AssignCheck) { checker = append(checker, checks.NewAssignAnalyzer(pass, conf)) } @@ -76,8 +99,9 @@ func run(pass *analysis.Pass) (interface{}, error) { i := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) for _, c := range checker { + c := c i.Preorder(c.NodeFilter(), func(node ast.Node) { - for _, exclude := range conf.Excludes { + for _, exclude := range conf.IgnoredFiles { if exclude.MatchString(pass.Fset.Position(node.Pos()).Filename) { return } diff --git a/vendor/github.com/tommy-muehle/go-mnd/checks/argument.go b/vendor/github.com/tommy-muehle/go-mnd/v2/checks/argument.go index 34cc1d09f..f0c5d71d2 100644 --- a/vendor/github.com/tommy-muehle/go-mnd/checks/argument.go +++ b/vendor/github.com/tommy-muehle/go-mnd/v2/checks/argument.go @@ -3,19 +3,19 @@ package checks import ( "go/ast" "go/token" + "strconv" + "sync" "golang.org/x/tools/go/analysis" - config "github.com/tommy-muehle/go-mnd/config" + "github.com/tommy-muehle/go-mnd/v2/config" ) const ArgumentCheck = "argument" -// Known excludes for the argument check. -var argumentExcludes = map[string]string{ - // package: function - "time": "Date", // https://golang.org/pkg/time/#Date -} +// constantDefinitions is used to save lines (by number) which contain a constant definition. +var constantDefinitions = map[string]bool{} +var mu sync.RWMutex type ArgumentAnalyzer struct { config *config.Config @@ -31,21 +31,47 @@ func NewArgumentAnalyzer(pass *analysis.Pass, config *config.Config) *ArgumentAn func (a *ArgumentAnalyzer) NodeFilter() []ast.Node { return []ast.Node{ + (*ast.GenDecl)(nil), (*ast.CallExpr)(nil), } } func (a *ArgumentAnalyzer) Check(n ast.Node) { - expr, ok := n.(*ast.CallExpr) - if !ok { - return + switch expr := n.(type) { + case *ast.CallExpr: + a.checkCallExpr(expr) + case *ast.GenDecl: + if expr.Tok == token.CONST { + pos := a.pass.Fset.Position(expr.TokPos) + + mu.Lock() + constantDefinitions[pos.Filename+":"+strconv.Itoa(pos.Line)] = true + mu.Unlock() + } } +} + +func (a *ArgumentAnalyzer) checkCallExpr(expr *ast.CallExpr) { + pos := a.pass.Fset.Position(expr.Pos()) - // Don't check if package and function combination is excluded - if s, ok := expr.Fun.(*ast.SelectorExpr); ok && a.isExcluded(s) { + mu.RLock() + ok := constantDefinitions[pos.Filename+":"+strconv.Itoa(pos.Line)] + mu.RUnlock() + + if ok { return } + switch f := expr.Fun.(type) { + case *ast.SelectorExpr: + switch prefix := f.X.(type) { + case *ast.Ident: + if a.config.IsIgnoredFunction(prefix.Name + "." + f.Sel.Name) { + return + } + } + } + for i, arg := range expr.Args { switch x := arg.(type) { case *ast.BasicLit: @@ -71,21 +97,6 @@ func (a *ArgumentAnalyzer) Check(n ast.Node) { } } -func (a *ArgumentAnalyzer) isExcluded(expr *ast.SelectorExpr) bool { - var p string - - switch x := expr.X.(type) { - case *ast.Ident: - p = x.Name - } - - if v, ok := argumentExcludes[p]; ok && v == expr.Sel.Name { - return true - } - - return false -} - func (a *ArgumentAnalyzer) checkBinaryExpr(expr *ast.BinaryExpr) { switch x := expr.X.(type) { case *ast.BasicLit: diff --git a/vendor/github.com/tommy-muehle/go-mnd/checks/assign.go b/vendor/github.com/tommy-muehle/go-mnd/v2/checks/assign.go index 8699ce17f..f930d0880 100644 --- a/vendor/github.com/tommy-muehle/go-mnd/checks/assign.go +++ b/vendor/github.com/tommy-muehle/go-mnd/v2/checks/assign.go @@ -6,7 +6,7 @@ import ( "golang.org/x/tools/go/analysis" - config "github.com/tommy-muehle/go-mnd/config" + config "github.com/tommy-muehle/go-mnd/v2/config" ) const AssignCheck = "assign" @@ -26,22 +26,42 @@ func NewAssignAnalyzer(pass *analysis.Pass, config *config.Config) *AssignAnalyz func (a *AssignAnalyzer) NodeFilter() []ast.Node { return []ast.Node{ (*ast.KeyValueExpr)(nil), + (*ast.AssignStmt)(nil), } } func (a *AssignAnalyzer) Check(n ast.Node) { - expr, ok := n.(*ast.KeyValueExpr) - if !ok { - return + switch expr := n.(type) { + case *ast.KeyValueExpr: + switch x := expr.Value.(type) { + case *ast.BasicLit: + if a.isMagicNumber(x) { + a.pass.Reportf(x.Pos(), reportMsg, x.Value, AssignCheck) + } + case *ast.BinaryExpr: + a.checkBinaryExpr(x) + } + case *ast.AssignStmt: + for _, e := range expr.Rhs { + switch y := e.(type) { + case *ast.UnaryExpr: + a.checkUnaryExpr(y) + case *ast.BinaryExpr: + switch x := y.Y.(type) { + case *ast.UnaryExpr: + a.checkUnaryExpr(x) + } + } + } } +} - switch x := expr.Value.(type) { +func (a *AssignAnalyzer) checkUnaryExpr(expr *ast.UnaryExpr) { + switch x := expr.X.(type) { case *ast.BasicLit: if a.isMagicNumber(x) { a.pass.Reportf(x.Pos(), reportMsg, x.Value, AssignCheck) } - case *ast.BinaryExpr: - a.checkBinaryExpr(x) } } diff --git a/vendor/github.com/tommy-muehle/go-mnd/checks/case.go b/vendor/github.com/tommy-muehle/go-mnd/v2/checks/case.go index d7993ede0..228cab4b8 100644 --- a/vendor/github.com/tommy-muehle/go-mnd/checks/case.go +++ b/vendor/github.com/tommy-muehle/go-mnd/v2/checks/case.go @@ -6,7 +6,7 @@ import ( "golang.org/x/tools/go/analysis" - config "github.com/tommy-muehle/go-mnd/config" + config "github.com/tommy-muehle/go-mnd/v2/config" ) const CaseCheck = "case" diff --git a/vendor/github.com/tommy-muehle/go-mnd/checks/checks.go b/vendor/github.com/tommy-muehle/go-mnd/v2/checks/checks.go index deff0c7bf..deff0c7bf 100644 --- a/vendor/github.com/tommy-muehle/go-mnd/checks/checks.go +++ b/vendor/github.com/tommy-muehle/go-mnd/v2/checks/checks.go diff --git a/vendor/github.com/tommy-muehle/go-mnd/checks/condition.go b/vendor/github.com/tommy-muehle/go-mnd/v2/checks/condition.go index b61bc0de9..20f892ede 100644 --- a/vendor/github.com/tommy-muehle/go-mnd/checks/condition.go +++ b/vendor/github.com/tommy-muehle/go-mnd/v2/checks/condition.go @@ -6,7 +6,7 @@ import ( "golang.org/x/tools/go/analysis" - config "github.com/tommy-muehle/go-mnd/config" + config "github.com/tommy-muehle/go-mnd/v2/config" ) const ConditionCheck = "condition" diff --git a/vendor/github.com/tommy-muehle/go-mnd/checks/operation.go b/vendor/github.com/tommy-muehle/go-mnd/v2/checks/operation.go index f1f8cf445..ddf3a0363 100644 --- a/vendor/github.com/tommy-muehle/go-mnd/checks/operation.go +++ b/vendor/github.com/tommy-muehle/go-mnd/v2/checks/operation.go @@ -6,7 +6,7 @@ import ( "golang.org/x/tools/go/analysis" - config "github.com/tommy-muehle/go-mnd/config" + config "github.com/tommy-muehle/go-mnd/v2/config" ) const OperationCheck = "operation" @@ -26,28 +26,32 @@ func NewOperationAnalyzer(pass *analysis.Pass, config *config.Config) *Operation func (a *OperationAnalyzer) NodeFilter() []ast.Node { return []ast.Node{ (*ast.AssignStmt)(nil), + (*ast.ParenExpr)(nil), } } func (a *OperationAnalyzer) Check(n ast.Node) { - stmt, ok := n.(*ast.AssignStmt) - if !ok { - return - } - - for _, expr := range stmt.Rhs { - switch x := expr.(type) { + switch expr := n.(type) { + case *ast.ParenExpr: + switch x := expr.X.(type) { case *ast.BinaryExpr: - switch xExpr := x.X.(type) { - case *ast.BinaryExpr: - a.checkBinaryExpr(xExpr) - } - switch yExpr := x.Y.(type) { + a.checkBinaryExpr(x) + } + case *ast.AssignStmt: + for _, y := range expr.Rhs { + switch x := y.(type) { case *ast.BinaryExpr: - a.checkBinaryExpr(yExpr) - } + switch xExpr := x.X.(type) { + case *ast.BinaryExpr: + a.checkBinaryExpr(xExpr) + } + switch yExpr := x.Y.(type) { + case *ast.BinaryExpr: + a.checkBinaryExpr(yExpr) + } - a.checkBinaryExpr(x) + a.checkBinaryExpr(x) + } } } } diff --git a/vendor/github.com/tommy-muehle/go-mnd/checks/return.go b/vendor/github.com/tommy-muehle/go-mnd/v2/checks/return.go index be7f54697..bc53940c7 100644 --- a/vendor/github.com/tommy-muehle/go-mnd/checks/return.go +++ b/vendor/github.com/tommy-muehle/go-mnd/v2/checks/return.go @@ -6,7 +6,7 @@ import ( "golang.org/x/tools/go/analysis" - config "github.com/tommy-muehle/go-mnd/config" + config "github.com/tommy-muehle/go-mnd/v2/config" ) const ReturnCheck = "return" @@ -41,6 +41,24 @@ func (a *ReturnAnalyzer) Check(n ast.Node) { if a.isMagicNumber(x) { a.pass.Reportf(x.Pos(), reportMsg, x.Value, ReturnCheck) } + case *ast.BinaryExpr: + a.checkBinaryExpr(x) + } + } +} + +func (a *ReturnAnalyzer) checkBinaryExpr(expr *ast.BinaryExpr) { + switch x := expr.X.(type) { + case *ast.BasicLit: + if a.isMagicNumber(x) { + a.pass.Reportf(x.Pos(), reportMsg, x.Value, ReturnCheck) + } + } + + switch y := expr.Y.(type) { + case *ast.BasicLit: + if a.isMagicNumber(y) { + a.pass.Reportf(y.Pos(), reportMsg, y.Value, ReturnCheck) } } } diff --git a/vendor/github.com/tommy-muehle/go-mnd/config/config.go b/vendor/github.com/tommy-muehle/go-mnd/v2/config/config.go index 35c82eaf2..a4681e37d 100644 --- a/vendor/github.com/tommy-muehle/go-mnd/config/config.go +++ b/vendor/github.com/tommy-muehle/go-mnd/v2/config/config.go @@ -6,9 +6,10 @@ import ( ) type Config struct { - Checks map[string]bool - IgnoredNumbers map[string]struct{} - Excludes []*regexp.Regexp + Checks map[string]bool + IgnoredNumbers map[string]struct{} + IgnoredFunctions []*regexp.Regexp + IgnoredFiles []*regexp.Regexp } type Option func(config *Config) @@ -17,31 +18,50 @@ func DefaultConfig() *Config { return &Config{ Checks: map[string]bool{}, IgnoredNumbers: map[string]struct{}{ - "0": {}, - "1": {}, + "0": {}, + "0.0": {}, + "1": {}, + "1.0": {}, }, - Excludes: []*regexp.Regexp{ + IgnoredFiles: []*regexp.Regexp{ regexp.MustCompile(`_test.go`), }, + IgnoredFunctions: []*regexp.Regexp{ + regexp.MustCompile(`time.Date`), + }, } } func WithOptions(options ...Option) *Config { c := DefaultConfig() + for _, option := range options { option(c) } + return c } -func WithExcludes(excludes string) Option { +func WithIgnoredFunctions(excludes string) Option { + return func(config *Config) { + if excludes == "" { + return + } + + for _, exclude := range strings.Split(excludes, ",") { + config.IgnoredFunctions = append(config.IgnoredFunctions, regexp.MustCompile(exclude)) + } + } +} + +func WithIgnoredFiles(excludes string) Option { return func(config *Config) { if excludes == "" { return } for _, exclude := range strings.Split(excludes, ",") { - config.Excludes = append(config.Excludes, regexp.MustCompile(exclude)) + config.IgnoredFiles = append(config.IgnoredFiles, regexp.MustCompile(exclude)) } } } @@ -53,7 +73,7 @@ func WithIgnoredNumbers(numbers string) Option { } for _, number := range strings.Split(numbers, ",") { - config.IgnoredNumbers[number] = struct{}{} + config.IgnoredNumbers[config.removeDigitSeparator(number)] = struct{}{} } } } @@ -79,6 +99,20 @@ func (c *Config) IsCheckEnabled(name string) bool { } func (c *Config) IsIgnoredNumber(number string) bool { - _, ok := c.IgnoredNumbers[number] + _, ok := c.IgnoredNumbers[c.removeDigitSeparator(number)] return ok } + +func (c *Config) IsIgnoredFunction(f string) bool { + for _, pattern := range c.IgnoredFunctions { + if pattern.MatchString(f) { + return true + } + } + + return false +} + +func (c *Config) removeDigitSeparator(number string) string { + return strings.Replace(number, "_", "", -1) +} diff --git a/vendor/github.com/tommy-muehle/go-mnd/v2/entrypoint.sh b/vendor/github.com/tommy-muehle/go-mnd/v2/entrypoint.sh new file mode 100644 index 000000000..cabc2f63d --- /dev/null +++ b/vendor/github.com/tommy-muehle/go-mnd/v2/entrypoint.sh @@ -0,0 +1,7 @@ +#!/usr/bin/env bash + +# Expand the arguments into an array of strings. This is required because the GitHub action +# provides all arguments concatenated as a single string. +ARGS=("$@") + +/bin/go-mnd "${ARGS[*]}" diff --git a/vendor/github.com/tommy-muehle/go-mnd/v2/go.mod b/vendor/github.com/tommy-muehle/go-mnd/v2/go.mod new file mode 100644 index 000000000..8e7c18e22 --- /dev/null +++ b/vendor/github.com/tommy-muehle/go-mnd/v2/go.mod @@ -0,0 +1,9 @@ +module github.com/tommy-muehle/go-mnd/v2 + +go 1.12 + +require ( + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/stretchr/testify v1.3.0 + golang.org/x/tools v0.0.0-20200329025819-fd4102a86c65 +) diff --git a/vendor/github.com/tommy-muehle/go-mnd/v2/go.sum b/vendor/github.com/tommy-muehle/go-mnd/v2/go.sum new file mode 100644 index 000000000..991a43759 --- /dev/null +++ b/vendor/github.com/tommy-muehle/go-mnd/v2/go.sum @@ -0,0 +1,28 @@ +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/mod v0.2.0 h1:KU7oHjnv3XNWfa5COkzUifxZmxp1TyI7ImMXqFxLwvQ= +golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.0.0-20200329025819-fd4102a86c65 h1:1KSbntBked74wYsKq0jzXYy7ZwcjAUtrl7EmPE97Iiw= +golang.org/x/tools v0.0.0-20200329025819-fd4102a86c65/go.mod h1:Sl4aGygMT6LrqrWclx+PTx3U+LnKx/seiNR+3G19Ar8= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= |
