update go-critic (#679)

Go-critic got a new checker `regexpPattern`.
This commit is contained in:
Isaev Denis 2019-09-11 16:56:33 +03:00 committed by GitHub
parent 58845813da
commit 94eaa8f196
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 77 additions and 4 deletions

2
go.mod
View File

@ -7,7 +7,7 @@ require (
github.com/OpenPeeDeeP/depguard v1.0.0 github.com/OpenPeeDeeP/depguard v1.0.0
github.com/StackExchange/wmi v0.0.0-20180116203802-5d049714c4a6 // indirect github.com/StackExchange/wmi v0.0.0-20180116203802-5d049714c4a6 // indirect
github.com/fatih/color v1.6.0 github.com/fatih/color v1.6.0
github.com/go-critic/go-critic v0.3.5-0.20190526074819-1df300866540 github.com/go-critic/go-critic v0.3.5-0.20190904082202-d79a9f0c64db
github.com/go-lintpack/lintpack v0.5.2 github.com/go-lintpack/lintpack v0.5.2
github.com/go-ole/go-ole v1.2.1 // indirect github.com/go-ole/go-ole v1.2.1 // indirect
github.com/gobwas/glob v0.2.3 // indirect github.com/gobwas/glob v0.2.3 // indirect

4
go.sum
View File

@ -10,8 +10,8 @@ github.com/fatih/color v1.6.0 h1:66qjqZk8kalYAvDRtM1AdAJQI0tj4Wrue3Eq3B3pmFU=
github.com/fatih/color v1.6.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4= github.com/fatih/color v1.6.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I= 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/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/go-critic/go-critic v0.3.5-0.20190526074819-1df300866540 h1:djv/qAomOVj8voCHt0M0OYwR/4vfDq1zNKSPKjJCexs= github.com/go-critic/go-critic v0.3.5-0.20190904082202-d79a9f0c64db h1:GYXWx7Vr3+zv833u+8IoXbNnQY0AdXsxAgI0kX7xcwA=
github.com/go-critic/go-critic v0.3.5-0.20190526074819-1df300866540/go.mod h1:+sE8vrLDS2M0pZkBk0wy6+nLdKexVDrl/jBqQOTDThA= github.com/go-critic/go-critic v0.3.5-0.20190904082202-d79a9f0c64db/go.mod h1:+sE8vrLDS2M0pZkBk0wy6+nLdKexVDrl/jBqQOTDThA=
github.com/go-lintpack/lintpack v0.5.2 h1:DI5mA3+eKdWeJ40nU4d6Wc26qmdG8RCi/btYq0TuRN0= github.com/go-lintpack/lintpack v0.5.2 h1:DI5mA3+eKdWeJ40nU4d6Wc26qmdG8RCi/btYq0TuRN0=
github.com/go-lintpack/lintpack v0.5.2/go.mod h1:NwZuYi2nUHho8XEIZ6SIxihrnPoqBTDqfpXvXAN0sXM= github.com/go-lintpack/lintpack v0.5.2/go.mod h1:NwZuYi2nUHho8XEIZ6SIxihrnPoqBTDqfpXvXAN0sXM=
github.com/go-ole/go-ole v1.2.1 h1:2lOsA72HgjxAuMlKpFiCbHTvu44PIVkZ5hqm3RSdI/E= github.com/go-ole/go-ole v1.2.1 h1:2lOsA72HgjxAuMlKpFiCbHTvu44PIVkZ5hqm3RSdI/E=

View File

@ -24,6 +24,9 @@ func init() {
// args[xIndex] and args[yIndex] are equal. // args[xIndex] and args[yIndex] are equal.
newMatcherFunc := func(xIndex, yIndex int) func(*ast.CallExpr) bool { newMatcherFunc := func(xIndex, yIndex int) func(*ast.CallExpr) bool {
return func(call *ast.CallExpr) bool { return func(call *ast.CallExpr) bool {
if len(call.Args) <= xIndex || len(call.Args) <= yIndex {
return false
}
x := call.Args[xIndex] x := call.Args[xIndex]
y := call.Args[yIndex] y := call.Args[yIndex]
return astequal.Expr(x, y) return astequal.Expr(x, y)

View File

@ -0,0 +1,68 @@
package checkers
import (
"go/ast"
"go/constant"
"regexp"
"strings"
"github.com/go-lintpack/lintpack"
"github.com/go-lintpack/lintpack/astwalk"
)
func init() {
var info lintpack.CheckerInfo
info.Name = "regexpPattern"
info.Tags = []string{"diagnostic", "experimental"}
info.Summary = "Detects suspicious regexp patterns"
info.Before = "regexp.MustCompile(`google.com|yandex.ru`)"
info.After = "regexp.MustCompile(`google\\.com|yandex\\.ru`)"
collection.AddChecker(&info, func(ctx *lintpack.CheckerContext) lintpack.FileWalker {
domains := []string{
"com",
"org",
"info",
"net",
"ru",
"de",
}
allDomains := strings.Join(domains, "|")
domainRE := regexp.MustCompile(`[^\\]\.(` + allDomains + `)\b`)
return astwalk.WalkerForExpr(&regexpPatternChecker{
ctx: ctx,
domainRE: domainRE,
})
})
}
type regexpPatternChecker struct {
astwalk.WalkHandler
ctx *lintpack.CheckerContext
domainRE *regexp.Regexp
}
func (c *regexpPatternChecker) VisitExpr(x ast.Expr) {
call, ok := x.(*ast.CallExpr)
if !ok {
return
}
switch qualifiedName(call.Fun) {
case "regexp.Compile", "regexp.CompilePOSIX", "regexp.MustCompile", "regexp.MustCompilePosix":
cv := c.ctx.TypesInfo.Types[call.Args[0]].Value
if cv == nil || cv.Kind() != constant.String {
return
}
s := constant.StringVal(cv)
if m := c.domainRE.FindStringSubmatch(s); m != nil {
c.warnDomain(call.Args[0], m[1])
}
}
}
func (c *regexpPatternChecker) warnDomain(cause ast.Expr, domain string) {
c.ctx.Warn(cause, "'.%s' should probably be '\\.%s'", domain, domain)
}

4
vendor/modules.txt vendored
View File

@ -10,7 +10,7 @@ github.com/davecgh/go-spew/spew
github.com/fatih/color github.com/fatih/color
# github.com/fsnotify/fsnotify v1.4.7 # github.com/fsnotify/fsnotify v1.4.7
github.com/fsnotify/fsnotify github.com/fsnotify/fsnotify
# github.com/go-critic/go-critic v0.3.5-0.20190526074819-1df300866540 # github.com/go-critic/go-critic v0.3.5-0.20190904082202-d79a9f0c64db
github.com/go-critic/go-critic/checkers github.com/go-critic/go-critic/checkers
github.com/go-critic/go-critic/checkers/internal/lintutil github.com/go-critic/go-critic/checkers/internal/lintutil
# github.com/go-lintpack/lintpack v0.5.2 # github.com/go-lintpack/lintpack v0.5.2
@ -187,6 +187,8 @@ github.com/stretchr/testify/require
github.com/timakin/bodyclose/passes/bodyclose github.com/timakin/bodyclose/passes/bodyclose
# github.com/ultraware/funlen v0.0.1 => github.com/golangci/funlen v0.0.0-20190909161642-5e59b9546114 # github.com/ultraware/funlen v0.0.1 => github.com/golangci/funlen v0.0.0-20190909161642-5e59b9546114
github.com/ultraware/funlen github.com/ultraware/funlen
# github.com/ultraware/whitespace v0.0.2
github.com/ultraware/whitespace
# github.com/valyala/bytebufferpool v1.0.0 # github.com/valyala/bytebufferpool v1.0.0
github.com/valyala/bytebufferpool github.com/valyala/bytebufferpool
# github.com/valyala/quicktemplate v1.1.1 # github.com/valyala/quicktemplate v1.1.1