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

47 lines
797 B
Go

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