From 1ac15487b257512b661419b44745b444d22a35d8 Mon Sep 17 00:00:00 2001 From: Trevor Pounds <trevor.pounds@gmail.com> Date: Sun, 12 Jan 2020 17:28:04 -0500 Subject: [PATCH] Update to go-critic v0.4.1. --- go.mod | 2 +- go.sum | 4 +- .../go-critic/checkers/mapKey_checker.go | 124 ++++++++++++++++++ vendor/modules.txt | 2 +- 4 files changed, 128 insertions(+), 4 deletions(-) create mode 100644 vendor/github.com/go-critic/go-critic/checkers/mapKey_checker.go diff --git a/go.mod b/go.mod index 447e062b..5a71ed9d 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 74222108..80d7e17c 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/vendor/github.com/go-critic/go-critic/checkers/mapKey_checker.go b/vendor/github.com/go-critic/go-critic/checkers/mapKey_checker.go new file mode 100644 index 00000000..de3e781e --- /dev/null +++ b/vendor/github.com/go-critic/go-critic/checkers/mapKey_checker.go @@ -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) +} diff --git a/vendor/modules.txt b/vendor/modules.txt index 05d6565d..a97d0de5 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -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