Skip to content

Commit

Permalink
#9 Fix missing magic number in assign statement
Browse files Browse the repository at this point in the history
  • Loading branch information
tommy-muehle committed May 18, 2020
1 parent a8c3345 commit a815e84
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 11 deletions.
33 changes: 22 additions & 11 deletions checks/assign.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,33 @@ func NewAssignAnalyzer(pass *analysis.Pass, config *config.Config) *AssignAnalyz
func (a *AssignAnalyzer) NodeFilter() []ast.Node {
return []ast.Node{
(*ast.KeyValueExpr)(nil),
(*ast.AssignStmt)(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 a.isMagicNumber(x) {
a.pass.Reportf(x.Pos(), reportMsg, x.Value, AssignCheck)
switch expr := n.(type) {
case *ast.KeyValueExpr:
switch x := expr.Value.(type) {
case *ast.BasicLit:
if a.isMagicNumber(x) {
a.pass.Reportf(x.Pos(), reportMsg, x.Value, AssignCheck)
}
case *ast.BinaryExpr:
a.checkBinaryExpr(x)
}
case *ast.AssignStmt:
for _, e := range expr.Rhs {
switch y := e.(type) {
case *ast.UnaryExpr:
switch x := y.X.(type) {
case *ast.BasicLit:
if a.isMagicNumber(x) {
a.pass.Reportf(x.Pos(), reportMsg, x.Value, AssignCheck)
}
}
}
}
case *ast.BinaryExpr:
a.checkBinaryExpr(x)
}
}

Expand Down
6 changes: 6 additions & 0 deletions testdata/src/assign/assign.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package assign

import (
"fmt"
"net/http"
"time"
)
Expand Down Expand Up @@ -28,3 +29,8 @@ func example3() *http.Client {

return c
}

func example4() {
res := -12 // want "Magic number: 12"
fmt.Println(res)
}

0 comments on commit a815e84

Please sign in to comment.