Skip to content

Commit

Permalink
checkers: followup for go-critic#773 (go-critic#774)
Browse files Browse the repository at this point in the history
- Use "style" tag instead of "diagnostic"
- Use byteCast.Args[0] instead of strings woodoo

Signed-off-by: Iskander Sharipov <[email protected]>
  • Loading branch information
quasilyte authored Jan 18, 2019
1 parent f54bdb6 commit 170d65c
Showing 1 changed file with 3 additions and 5 deletions.
8 changes: 3 additions & 5 deletions checkers/stringXbytes_checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,16 @@ package checkers

import (
"go/ast"
"strings"

"github.com/go-lintpack/lintpack"
"github.com/go-lintpack/lintpack/astwalk"
"github.com/go-toolsmith/astfmt"
"github.com/go-toolsmith/typep"
)

func init() {
var info lintpack.CheckerInfo
info.Name = "stringXbytes"
info.Tags = []string{"diagnostic", "experimental"}
info.Tags = []string{"style", "experimental"}
info.Summary = "Detects redundant conversions between string and []byte"
info.Before = `copy(b, []byte(s))`
info.After = `copy(b, s)`
Expand All @@ -36,12 +34,12 @@ func (c *stringXbytes) VisitExpr(expr ast.Expr) {
typep.IsTypeExpr(c.ctx.TypesInfo, byteCast.Fun) &&
typep.HasStringProp(c.ctx.TypesInfo.TypeOf(byteCast.Args[0])) {

c.warn(byteCast, strings.TrimSuffix(strings.TrimPrefix(astfmt.Sprint(byteCast), "[]byte("), ")"))
c.warn(byteCast, byteCast.Args[0])
}
}
}

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

0 comments on commit 170d65c

Please sign in to comment.