Skip to content

Added go-mnd linter #842

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Nov 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ godox: Tool for detection of FIXME, TODO and other comment keywords [fast: true,
gofmt: Gofmt checks whether code was gofmt-ed. By default this tool runs with -s option to check for code simplification [fast: true, auto-fix: true]
goimports: Goimports does everything that gofmt does. Additionally it checks unused imports [fast: true, auto-fix: true]
golint: Golint differs from gofmt. Gofmt reformats Go source code, whereas golint prints out style mistakes [fast: true, auto-fix: false]
gomnd: checks whether magic number is used [fast: true, auto-fix: false]
gosec (gas): Inspects source code for security problems [fast: true, auto-fix: false]
interfacer: Linter that suggests narrower interface types [fast: true, auto-fix: false]
lll: Reports long lines [fast: true, auto-fix: false]
Expand Down Expand Up @@ -480,6 +481,7 @@ golangci-lint help linters
- [funlen](https://github.com/ultraware/funlen) - Tool for detection of long functions
- [whitespace](https://github.com/ultraware/whitespace) - Tool for detection of leading and trailing whitespace
- [wsl](https://github.com/bombsimon/wsl) - Whitespace Linter - Forces you to use empty lines!
- [gomnd](https://github.com/tommy-muehle/go-mnd) - checks whether magic number is used

## Configuration

Expand Down Expand Up @@ -1165,6 +1167,7 @@ Thanks to developers and authors of used linters:
- [matoous](https://github.com/matoous)
- [ultraware](https://github.com/ultraware)
- [bombsimon](https://github.com/bombsimon)
- [tommy-muehle](https://github.com/tommy-muehle)

## Changelog

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ require (
github.com/spf13/viper v1.4.0
github.com/stretchr/testify v1.4.0
github.com/timakin/bodyclose v0.0.0-20190930140734-f7f2e9bca95e
github.com/tommy-muehle/go-mnd v0.0.0-20190903201840-c93e405da530
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we change this to reference a tagged release version?

Copy link
Member Author

@sayboras sayboras Nov 10, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried before, but it gives me the below error (even the tag is available)

go: github.com/tommy-muehle/[email protected]: reading github.com/tommy-muehle/go-mnd/go.mod at revision v1.1.1: unknown revision v1.1.1

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh I got it now, the tag in github.com/tommy-muehle/go-mnd is not prefixed with v, so we can only use pseudo-versions only

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

raised one issue here tommy-muehle/go-mnd#1

github.com/ultraware/funlen v0.0.2
github.com/ultraware/whitespace v0.0.4
github.com/uudashr/gocognit v0.0.0-20190926065955-1655d0de0517
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,8 @@ github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81P
github.com/timakin/bodyclose v0.0.0-20190930140734-f7f2e9bca95e h1:RumXZ56IrCj4CL+g1b9OL/oH0QnsF976bC8xQFYUD5Q=
github.com/timakin/bodyclose v0.0.0-20190930140734-f7f2e9bca95e/go.mod h1:Qimiffbc6q9tBWlVV6x0P9sat/ao1xEkREYPPj9hphk=
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
github.com/tommy-muehle/go-mnd v0.0.0-20190903201840-c93e405da530 h1:uUctDnVK0uevT4yglJXAXDG74EkQvGdu6L1pSoZx+T4=
github.com/tommy-muehle/go-mnd v0.0.0-20190903201840-c93e405da530/go.mod h1:dSUh0FtTP8VhvkL1S+gUR1OKd9ZnSaozuI6r3m6wOig=
github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc=
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
github.com/ultraware/funlen v0.0.2 h1:Av96YVBwwNSe4MLR7iI/BIa3VyI7/djnto/pK3Uxbdo=
Expand Down
21 changes: 21 additions & 0 deletions pkg/golinters/gomnd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package golinters

import (
magic_numbers "github.com/tommy-muehle/go-mnd"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: I would just alias this as mnd.

"golang.org/x/tools/go/analysis"

"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
)

func NewGomnd() *goanalysis.Linter {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Similarly, I would name this NewGoMND to be consistent with standard Go naming conventions.

analyzers := []*analysis.Analyzer{
magic_numbers.Analyzer,
}

return goanalysis.NewLinter(
"gomnd",
"checks whether magic number is used",
analyzers,
nil,
).WithLoadMode(goanalysis.LoadModeSyntax)
}
3 changes: 3 additions & 0 deletions pkg/lint/lintersdb/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,9 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
linter.NewConfig(golinters.NewWSL()).
WithPresets(linter.PresetStyle).
WithURL("https://github.com/bombsimon/wsl"),
linter.NewConfig(golinters.NewGomnd()).
WithPresets(linter.PresetStyle).
WithURL("https://github.com/tommy-muehle/go-mnd"),
}

isLocalRun := os.Getenv("GOLANGCI_COM_RUN") == ""
Expand Down
36 changes: 36 additions & 0 deletions test/testdata/gomnd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//args: -Egomnd
package testdata

import (
"log"
"net/http"
"time"
)

func UseMagicNumber() {
c := &http.Client{
Timeout: 1 * time.Second, // ERROR : "Magic number: 1, in <assign> detected"
}

res, err := c.Get("http://www.google.com")
if err != nil {
log.Fatal(err)
}
if res.StatusCode != 200 { // ERROR : "Magic number: 200, in <condition> detected"
log.Println("Something went wrong")
}
}

func UseNoMagicNumber() {
c := &http.Client{
Timeout: time.Second,
}

res, err := c.Get("http://www.google.com")
if err != nil {
log.Fatal(err)
}
if res.StatusCode != http.StatusOK {
log.Println("Something went wrong")
}
}
12 changes: 12 additions & 0 deletions vendor/github.com/tommy-muehle/go-mnd/.editorconfig

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions vendor/github.com/tommy-muehle/go-mnd/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions vendor/github.com/tommy-muehle/go-mnd/.goreleaser.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions vendor/github.com/tommy-muehle/go-mnd/.travis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions vendor/github.com/tommy-muehle/go-mnd/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

97 changes: 97 additions & 0 deletions vendor/github.com/tommy-muehle/go-mnd/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

71 changes: 71 additions & 0 deletions vendor/github.com/tommy-muehle/go-mnd/analyzer.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading