diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2020-04-04 03:29:12 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-04-03 22:29:12 +0300 |
commit | 4f63f283c47dcf9e705ce5b8e8857f2b42cff8ad (patch) | |
tree | dd5dc2cae6ebae21826ffcce937533559be45a07 /vendor/github.com/mgechev/revive/rule/string-of-int.go | |
parent | 4af7c47b38d382d105726f9553a1a68d46882cbf (diff) | |
download | gitea-4f63f283c47dcf9e705ce5b8e8857f2b42cff8ad.tar.gz gitea-4f63f283c47dcf9e705ce5b8e8857f2b42cff8ad.zip |
Rename scripts to build and add revive command as a new build tool command (#10942)
Co-authored-by: techknowlogick <techknowlogick@gitea.io>
Diffstat (limited to 'vendor/github.com/mgechev/revive/rule/string-of-int.go')
-rw-r--r-- | vendor/github.com/mgechev/revive/rule/string-of-int.go | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/vendor/github.com/mgechev/revive/rule/string-of-int.go b/vendor/github.com/mgechev/revive/rule/string-of-int.go new file mode 100644 index 0000000000..38f453a4aa --- /dev/null +++ b/vendor/github.com/mgechev/revive/rule/string-of-int.go @@ -0,0 +1,95 @@ +package rule + +import ( + "go/ast" + "go/types" + + "github.com/mgechev/revive/lint" +) + +// StringOfIntRule warns when logic expressions contains Boolean literals. +type StringOfIntRule struct{} + +// Apply applies the rule to given file. +func (r *StringOfIntRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { + var failures []lint.Failure + + onFailure := func(failure lint.Failure) { + failures = append(failures, failure) + } + + astFile := file.AST + file.Pkg.TypeCheck() + + w := &lintStringInt{file, onFailure} + ast.Walk(w, astFile) + + return failures +} + +// Name returns the rule name. +func (r *StringOfIntRule) Name() string { + return "string-of-int" +} + +type lintStringInt struct { + file *lint.File + onFailure func(lint.Failure) +} + +func (w *lintStringInt) Visit(node ast.Node) ast.Visitor { + ce, ok := node.(*ast.CallExpr) + if !ok { + return w + } + + if !w.isCallStringCast(ce.Fun) { + return w + } + + if !w.isIntExpression(ce.Args) { + return w + } + + w.onFailure(lint.Failure{ + Confidence: 1, + Node: ce, + Failure: "dubious convertion of an integer into a string, use strconv.Itoa", + }) + + return w +} + +func (w *lintStringInt) isCallStringCast(e ast.Expr) bool { + t := w.file.Pkg.TypeOf(e) + if t == nil { + return false + } + + tb, _ := t.Underlying().(*types.Basic) + + return tb != nil && tb.Kind() == types.String +} + +func (w *lintStringInt) isIntExpression(es []ast.Expr) bool { + if len(es) != 1 { + return false + } + + t := w.file.Pkg.TypeOf(es[0]) + if t == nil { + return false + } + + ut, _ := t.Underlying().(*types.Basic) + if ut == nil || ut.Info()&types.IsInteger == 0 { + return false + } + + switch ut.Kind() { + case types.Byte, types.Rune, types.UntypedRune: + return false + } + + return true +} |