Skip to content

Commit

Permalink
checkers: make stringXbytes more linear (go-critic#775)
Browse files Browse the repository at this point in the history
  • Loading branch information
cristaloleg authored Jan 18, 2019
1 parent 170d65c commit 84e9e83
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions checkers/stringXbytes_checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,21 @@ type stringXbytes struct {
}

func (c *stringXbytes) VisitExpr(expr ast.Expr) {
if x, ok := expr.(*ast.CallExpr); ok && qualifiedName(x.Fun) == "copy" {
src := x.Args[1]
x, ok := expr.(*ast.CallExpr)
if !ok || qualifiedName(x.Fun) != "copy" {
return
}

src := x.Args[1]

if byteCast, ok := src.(*ast.CallExpr); ok &&
typep.IsTypeExpr(c.ctx.TypesInfo, byteCast.Fun) &&
typep.HasStringProp(c.ctx.TypesInfo.TypeOf(byteCast.Args[0])) {
byteCast, ok := src.(*ast.CallExpr)
if ok && typep.IsTypeExpr(c.ctx.TypesInfo, byteCast.Fun) &&
typep.HasStringProp(c.ctx.TypesInfo.TypeOf(byteCast.Args[0])) {

c.warn(byteCast, byteCast.Args[0])
}
c.warn(byteCast, byteCast.Args[0])
}
}

func (c *stringXbytes) warn(cause *ast.CallExpr, suggestion ast.Expr) {
c.ctx.Warn(cause, "can simplify `%s` to `%s`",
cause, suggestion)
c.ctx.Warn(cause, "can simplify `%s` to `%s`", cause, suggestion)
}

0 comments on commit 84e9e83

Please sign in to comment.