Update to go-critic v0.4.1.

This commit is contained in:
Trevor Pounds 2020-01-12 17:28:04 -05:00
parent b32e16dfcb
commit 1ac15487b2
4 changed files with 128 additions and 4 deletions

2
go.mod
View File

@ -6,7 +6,7 @@ require (
github.com/OpenPeeDeeP/depguard v1.0.1
github.com/bombsimon/wsl/v2 v2.0.0
github.com/fatih/color v1.7.0
github.com/go-critic/go-critic v0.4.0
github.com/go-critic/go-critic v0.4.1
github.com/go-lintpack/lintpack v0.5.2
github.com/gofrs/flock v0.0.0-20190320160742-5135e617513b
github.com/golangci/check v0.0.0-20180506172741-cfe4005ccda2

4
go.sum
View File

@ -33,8 +33,8 @@ github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5Kwzbycv
github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/go-critic/go-critic v0.4.0 h1:sXD3pix0wDemuPuSlrXpJNNYXlUiKiysLrtPVQmxkzI=
github.com/go-critic/go-critic v0.4.0/go.mod h1:7/14rZGnZbY6E38VEGk2kVhoq6itzc1E68facVDK23g=
github.com/go-critic/go-critic v0.4.1 h1:4DTQfT1wWwLg/hzxwD9bkdhDQrdJtxe6DUTadPlrIeE=
github.com/go-critic/go-critic v0.4.1/go.mod h1:7/14rZGnZbY6E38VEGk2kVhoq6itzc1E68facVDK23g=
github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
github.com/go-lintpack/lintpack v0.5.2 h1:DI5mA3+eKdWeJ40nU4d6Wc26qmdG8RCi/btYq0TuRN0=
github.com/go-lintpack/lintpack v0.5.2/go.mod h1:NwZuYi2nUHho8XEIZ6SIxihrnPoqBTDqfpXvXAN0sXM=

View File

@ -0,0 +1,124 @@
package checkers
import (
"go/ast"
"go/types"
"strings"
"github.com/go-critic/go-critic/checkers/internal/lintutil"
"github.com/go-lintpack/lintpack"
"github.com/go-lintpack/lintpack/astwalk"
"github.com/go-toolsmith/astcast"
"github.com/go-toolsmith/astp"
"github.com/go-toolsmith/typep"
)
func init() {
var info lintpack.CheckerInfo
info.Name = "mapKey"
info.Tags = []string{"diagnostic", "experimental"}
info.Summary = "Detects suspicious map literal keys"
info.Before = `
_ = map[string]int{
"foo": 1,
"bar ": 2,
}`
info.After = `
_ = map[string]int{
"foo": 1,
"bar": 2,
}`
collection.AddChecker(&info, func(ctx *lintpack.CheckerContext) lintpack.FileWalker {
return astwalk.WalkerForExpr(&mapKeyChecker{ctx: ctx})
})
}
type mapKeyChecker struct {
astwalk.WalkHandler
ctx *lintpack.CheckerContext
astSet lintutil.AstSet
}
func (c *mapKeyChecker) VisitExpr(expr ast.Expr) {
lit := astcast.ToCompositeLit(expr)
if len(lit.Elts) < 2 {
return
}
typ, ok := c.ctx.TypesInfo.TypeOf(lit).Underlying().(*types.Map)
if !ok {
return
}
if !typep.HasStringKind(typ.Key().Underlying()) {
return
}
c.checkWhitespace(lit)
c.checkDuplicates(lit)
}
func (c *mapKeyChecker) checkDuplicates(lit *ast.CompositeLit) {
c.astSet.Clear()
for _, elt := range lit.Elts {
kv := astcast.ToKeyValueExpr(elt)
if astp.IsBasicLit(kv.Key) {
// Basic lits are handled by the compiler.
continue
}
if !typep.SideEffectFree(c.ctx.TypesInfo, kv.Key) {
continue
}
if !c.astSet.Insert(kv.Key) {
c.warnDupKey(kv.Key)
}
}
}
func (c *mapKeyChecker) checkWhitespace(lit *ast.CompositeLit) {
var whitespaceKey ast.Node
for _, elt := range lit.Elts {
key := astcast.ToBasicLit(astcast.ToKeyValueExpr(elt).Key)
if len(key.Value) < len(`" "`) {
continue
}
// s is unquoted string literal value.
s := key.Value[len(`"`) : len(key.Value)-len(`"`)]
if !strings.Contains(s, " ") {
continue
}
if whitespaceKey != nil {
// Already seen something with a whitespace.
// More than one entry => not suspicious.
return
}
if s == " " {
// If space is used as a key, maybe this map
// has something to do with spaces. Give up.
return
}
// Check if it has exactly 1 space prefix or suffix.
bad := strings.HasPrefix(s, " ") && !strings.HasPrefix(s, " ") ||
strings.HasSuffix(s, " ") && !strings.HasSuffix(s, " ")
if !bad {
// These spaces can be a padding,
// or a legitimate part of a key. Give up.
return
}
whitespaceKey = key
}
if whitespaceKey != nil {
c.warnWhitespace(whitespaceKey)
}
}
func (c *mapKeyChecker) warnWhitespace(key ast.Node) {
c.ctx.Warn(key, "suspucious whitespace in %s key", key)
}
func (c *mapKeyChecker) warnDupKey(key ast.Node) {
c.ctx.Warn(key, "suspicious duplicate %s key", key)
}

2
vendor/modules.txt vendored
View File

@ -12,7 +12,7 @@ github.com/davecgh/go-spew/spew
github.com/fatih/color
# github.com/fsnotify/fsnotify v1.4.7
github.com/fsnotify/fsnotify
# github.com/go-critic/go-critic v0.4.0
# github.com/go-critic/go-critic v0.4.1
github.com/go-critic/go-critic/checkers
github.com/go-critic/go-critic/checkers/internal/lintutil
# github.com/go-lintpack/lintpack v0.5.2