sayboras bd2921655a Added go-mnd linter (#842)
* Initial commit

* Fixed goimports

* Update pkg/golinters/mnd.go

Co-Authored-By: Bot from GolangCI <42910462+golangcibot@users.noreply.github.com>

* Run goimports

* Update pkg/golinters/mnd.go

Co-Authored-By: Bot from GolangCI <42910462+golangcibot@users.noreply.github.com>

* 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
2019-11-15 19:08:43 -05:00

58 lines
1015 B
Go

package checks
import (
"go/ast"
"golang.org/x/tools/go/analysis"
)
const AssignCheck = "assign"
type AssignAnalyzer struct {
pass *analysis.Pass
}
func NewAssignAnalyzer(pass *analysis.Pass) *AssignAnalyzer {
return &AssignAnalyzer{
pass: pass,
}
}
func (a *AssignAnalyzer) NodeFilter() []ast.Node {
return []ast.Node{
(*ast.KeyValueExpr)(nil),
}
}
func (a *AssignAnalyzer) Check(n ast.Node) {
expr, ok := n.(*ast.KeyValueExpr)
if !ok {
return
}
switch x := expr.Value.(type) {
case *ast.BasicLit:
if isMagicNumber(x) {
a.pass.Reportf(x.Pos(), reportMsg, x.Value, AssignCheck)
}
case *ast.BinaryExpr:
a.checkBinaryExpr(x)
}
}
func (a *AssignAnalyzer) checkBinaryExpr(expr *ast.BinaryExpr) {
switch x := expr.X.(type) {
case *ast.BasicLit:
if isMagicNumber(x) {
a.pass.Reportf(x.Pos(), reportMsg, x.Value, AssignCheck)
}
}
switch y := expr.Y.(type) {
case *ast.BasicLit:
if isMagicNumber(y) {
a.pass.Reportf(y.Pos(), reportMsg, y.Value, AssignCheck)
}
}
}