Skip to content

Commit

Permalink
refactor: conversation type checks
Browse files Browse the repository at this point in the history
- removed redundant `types`
- simplified check flow
  • Loading branch information
butuzov committed May 6, 2023
1 parent 6d01c4f commit 1bc8c3c
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 48 deletions.
43 changes: 28 additions & 15 deletions internal/checker/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,15 @@ func (c *Checker) Check(e *ast.CallExpr) *Violation {
// Regular calls (`*ast.SelectorExpr`) like strings.HasPrefix or re.Match are
// handled by this check
case *ast.SelectorExpr:

x, ok := expr.X.(*ast.Ident)

// TODO: add check for the ast.ParenExpr in e.Fun so we can target
// the constructions like this
//
// (&maphash.Hash{}).Write([]byte("foobar"))
//

if !ok {
return nil // can't be mached, so can't be checked.
}
Expand Down Expand Up @@ -63,33 +70,31 @@ func (c *Checker) Check(e *ast.CallExpr) *Violation {
return v.WithAltArgs(argsFixed)
}
}

}
return nil
}

func (c *Checker) handleViolation(v *Violation, ce *ast.CallExpr) (map[int]ast.Expr, bool) {
m := map[int]ast.Expr{}

// We going to check each of elements we mark for checking, in order to find,
// a call that violates our rules.
for _, i := range v.Args {
if i >= len(ce.Args) {
continue
}

call, ok := ce.Args[i].(*ast.CallExpr)
if !ok {
if !ok || !c.isConverterCall(call) || len(call.Args) == 0 {
continue
}

if t := c.Type(call); t.String() != "string" && t.String() != "[]byte" {
continue
}

if string(v.Targets()) != c.types.TypeOf(call.Args[0]).String() {
// checking whats argument
if v.Targets() != c.Type(call.Args[0]).String() {
m[i] = call.Args[0]
}

}

return m, len(m) == len(v.Args)
}

Expand All @@ -110,17 +115,14 @@ func (c *Checker) HandleFunction(pkgName, methodName string) *Violation {
}

func (c *Checker) HandleMethod(receiver ast.Expr, method string) *Violation {
if c.types == nil || !c.types.Types[receiver].IsValue() {
if c.types == nil {
return nil
}
tv := c.types.Types[receiver]

if tv.Type == nil {
// todo(butuzov): logError
tv := c.types.Types[receiver]
if !tv.IsValue() || tv.Type == nil {
return nil
}

if methods, ok := c.Methods[cleanName(tv.Type.String())]; !ok {
} else if methods, ok := c.Methods[cleanName(tv.Type.String())]; !ok {
return nil
} else if violation, ok := methods[method]; ok {
return &violation
Expand All @@ -144,6 +146,17 @@ func (c *Checker) isImported(pkg, name string) bool {
return false
}

// todo: not implemented
func (c *Checker) isConverterCall(ce *ast.CallExpr) bool {
switch ce.Fun.(type) {
case *ast.ArrayType:
return c.types.TypeOf(ce.Fun).String() == "[]byte"
case *ast.Ident:
return c.types.TypeOf(ce.Fun).String() == "string"
}
return false
}

// cleanName will remove * from the name variable if it is a pointer.
func cleanName(name string) string {
if name[0] == '*' {
Expand Down
30 changes: 0 additions & 30 deletions internal/checker/types.go

This file was deleted.

6 changes: 3 additions & 3 deletions internal/checker/violation.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ func (v *Violation) Handle(ce *ast.CallExpr) (m map[int]ast.Expr, ok bool) {
return m, len(m) == len(v.Args)
}

func (v *Violation) Targets() Type {
func (v *Violation) Targets() string {
if !v.StringTargeted {
return Bytes
return "[]byte"
}

return String
return "string"
}

// TODO: not implemented
Expand Down

0 comments on commit 1bc8c3c

Please sign in to comment.