
* 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
65 lines
1.1 KiB
Go
65 lines
1.1 KiB
Go
package checks
|
|
|
|
import (
|
|
"go/ast"
|
|
|
|
"golang.org/x/tools/go/analysis"
|
|
)
|
|
|
|
const OperationCheck = "operation"
|
|
|
|
type OperationAnalyzer struct {
|
|
pass *analysis.Pass
|
|
}
|
|
|
|
func NewOperationAnalyzer(pass *analysis.Pass) *OperationAnalyzer {
|
|
return &OperationAnalyzer{
|
|
pass: pass,
|
|
}
|
|
}
|
|
|
|
func (a *OperationAnalyzer) NodeFilter() []ast.Node {
|
|
return []ast.Node{
|
|
(*ast.AssignStmt)(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) {
|
|
case *ast.BinaryExpr:
|
|
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)
|
|
}
|
|
}
|
|
}
|
|
|
|
func (a *OperationAnalyzer) checkBinaryExpr(expr *ast.BinaryExpr) {
|
|
switch x := expr.X.(type) {
|
|
case *ast.BasicLit:
|
|
if isMagicNumber(x) {
|
|
a.pass.Reportf(x.Pos(), reportMsg, x.Value, OperationCheck)
|
|
}
|
|
}
|
|
|
|
switch y := expr.Y.(type) {
|
|
case *ast.BasicLit:
|
|
if isMagicNumber(y) {
|
|
a.pass.Reportf(y.Pos(), reportMsg, y.Value, OperationCheck)
|
|
}
|
|
}
|
|
}
|