Skip to content

Commit bd29216

Browse files
sayborastpounds
authored andcommitted
Added go-mnd linter (#842)
* Initial commit * Fixed goimports * Update pkg/golinters/mnd.go Co-Authored-By: Bot from GolangCI <[email protected]> * Run goimports * Update pkg/golinters/mnd.go Co-Authored-By: Bot from GolangCI <[email protected]> * Add prefix for goimport local * Run make README.md * Incorporate review comments Add test cases * Update readme * Update the description * Removed subpath in local-prefixes * Update readme
1 parent 0e5b7a0 commit bd29216

24 files changed

+772
-0
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ godox: Tool for detection of FIXME, TODO and other comment keywords [fast: true,
219219
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]
220220
goimports: Goimports does everything that gofmt does. Additionally it checks unused imports [fast: true, auto-fix: true]
221221
golint: Golint differs from gofmt. Gofmt reformats Go source code, whereas golint prints out style mistakes [fast: true, auto-fix: false]
222+
gomnd: checks whether magic number is used [fast: true, auto-fix: false]
222223
gosec (gas): Inspects source code for security problems [fast: true, auto-fix: false]
223224
interfacer: Linter that suggests narrower interface types [fast: true, auto-fix: false]
224225
lll: Reports long lines [fast: true, auto-fix: false]
@@ -480,6 +481,7 @@ golangci-lint help linters
480481
- [funlen](https://github.com/ultraware/funlen) - Tool for detection of long functions
481482
- [whitespace](https://github.com/ultraware/whitespace) - Tool for detection of leading and trailing whitespace
482483
- [wsl](https://github.com/bombsimon/wsl) - Whitespace Linter - Forces you to use empty lines!
484+
- [gomnd](https://github.com/tommy-muehle/go-mnd) - checks whether magic number is used
483485
484486
## Configuration
485487
@@ -1165,6 +1167,7 @@ Thanks to developers and authors of used linters:
11651167
- [matoous](https://github.com/matoous)
11661168
- [ultraware](https://github.com/ultraware)
11671169
- [bombsimon](https://github.com/bombsimon)
1170+
- [tommy-muehle](https://github.com/tommy-muehle)
11681171
11691172
## Changelog
11701173

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ require (
3737
github.com/spf13/viper v1.4.0
3838
github.com/stretchr/testify v1.4.0
3939
github.com/timakin/bodyclose v0.0.0-20190930140734-f7f2e9bca95e
40+
github.com/tommy-muehle/go-mnd v0.0.0-20190903201840-c93e405da530
4041
github.com/ultraware/funlen v0.0.2
4142
github.com/ultraware/whitespace v0.0.4
4243
github.com/uudashr/gocognit v0.0.0-20190926065955-1655d0de0517

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,8 @@ github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81P
232232
github.com/timakin/bodyclose v0.0.0-20190930140734-f7f2e9bca95e h1:RumXZ56IrCj4CL+g1b9OL/oH0QnsF976bC8xQFYUD5Q=
233233
github.com/timakin/bodyclose v0.0.0-20190930140734-f7f2e9bca95e/go.mod h1:Qimiffbc6q9tBWlVV6x0P9sat/ao1xEkREYPPj9hphk=
234234
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
235+
github.com/tommy-muehle/go-mnd v0.0.0-20190903201840-c93e405da530 h1:uUctDnVK0uevT4yglJXAXDG74EkQvGdu6L1pSoZx+T4=
236+
github.com/tommy-muehle/go-mnd v0.0.0-20190903201840-c93e405da530/go.mod h1:dSUh0FtTP8VhvkL1S+gUR1OKd9ZnSaozuI6r3m6wOig=
235237
github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc=
236238
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
237239
github.com/ultraware/funlen v0.0.2 h1:Av96YVBwwNSe4MLR7iI/BIa3VyI7/djnto/pK3Uxbdo=

pkg/golinters/gomnd.go

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package golinters
2+
3+
import (
4+
magic_numbers "github.com/tommy-muehle/go-mnd"
5+
"golang.org/x/tools/go/analysis"
6+
7+
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
8+
)
9+
10+
func NewGomnd() *goanalysis.Linter {
11+
analyzers := []*analysis.Analyzer{
12+
magic_numbers.Analyzer,
13+
}
14+
15+
return goanalysis.NewLinter(
16+
"gomnd",
17+
"checks whether magic number is used",
18+
analyzers,
19+
nil,
20+
).WithLoadMode(goanalysis.LoadModeSyntax)
21+
}

pkg/lint/lintersdb/manager.go

+3
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,9 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
208208
linter.NewConfig(golinters.NewWSL()).
209209
WithPresets(linter.PresetStyle).
210210
WithURL("https://github.com/bombsimon/wsl"),
211+
linter.NewConfig(golinters.NewGomnd()).
212+
WithPresets(linter.PresetStyle).
213+
WithURL("https://github.com/tommy-muehle/go-mnd"),
211214
}
212215

213216
isLocalRun := os.Getenv("GOLANGCI_COM_RUN") == ""

test/testdata/gomnd.go

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//args: -Egomnd
2+
package testdata
3+
4+
import (
5+
"log"
6+
"net/http"
7+
"time"
8+
)
9+
10+
func UseMagicNumber() {
11+
c := &http.Client{
12+
Timeout: 1 * time.Second, // ERROR : "Magic number: 1, in <assign> detected"
13+
}
14+
15+
res, err := c.Get("http://www.google.com")
16+
if err != nil {
17+
log.Fatal(err)
18+
}
19+
if res.StatusCode != 200 { // ERROR : "Magic number: 200, in <condition> detected"
20+
log.Println("Something went wrong")
21+
}
22+
}
23+
24+
func UseNoMagicNumber() {
25+
c := &http.Client{
26+
Timeout: time.Second,
27+
}
28+
29+
res, err := c.Get("http://www.google.com")
30+
if err != nil {
31+
log.Fatal(err)
32+
}
33+
if res.StatusCode != http.StatusOK {
34+
log.Println("Something went wrong")
35+
}
36+
}

vendor/github.com/tommy-muehle/go-mnd/.editorconfig

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/tommy-muehle/go-mnd/.gitignore

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/tommy-muehle/go-mnd/.goreleaser.yml

+28
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/tommy-muehle/go-mnd/.travis.yml

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/tommy-muehle/go-mnd/LICENSE

+21
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/tommy-muehle/go-mnd/README.md

+97
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/tommy-muehle/go-mnd/analyzer.go

+71
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)