forked from go-critic/go-critic
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes go-critic#427
- Loading branch information
Showing
3 changed files
with
64 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
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.Summary = "Detects redundant conversions between string and []byte" | ||
info.Before = `copy(b, []byte(s))` | ||
info.After = `copy(b, s)` | ||
|
||
collection.AddChecker(&info, func(ctx *lintpack.CheckerContext) lintpack.FileWalker { | ||
return astwalk.WalkerForExpr(&stringXbytes{ctx: ctx}) | ||
}) | ||
} | ||
|
||
type stringXbytes struct { | ||
astwalk.WalkHandler | ||
ctx *lintpack.CheckerContext | ||
} | ||
|
||
func (c *stringXbytes) VisitExpr(expr ast.Expr) { | ||
if x, ok := expr.(*ast.CallExpr); ok && qualifiedName(x.Fun) == "copy" { | ||
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])) { | ||
|
||
c.warn(byteCast, strings.TrimSuffix(strings.TrimPrefix(astfmt.Sprint(byteCast), "[]byte("), ")")) | ||
} | ||
} | ||
} | ||
|
||
func (c *stringXbytes) warn(cause *ast.CallExpr, suggestion string) { | ||
c.ctx.Warn(cause, "can simplify `%s` to `%s`", | ||
cause, suggestion) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package checker_test | ||
|
||
func noWarnings() { | ||
var b []byte | ||
var s string | ||
|
||
copy(b, s) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package checker_test | ||
|
||
func warnings() { | ||
var b []byte | ||
var s string | ||
|
||
/*! can simplify `[]byte(s)` to `s` */ | ||
copy(b, []byte(s)) | ||
} |