aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/jirfag
diff options
context:
space:
mode:
authorDmitry Vyukov <dvyukov@google.com>2020-07-04 11:12:55 +0200
committerDmitry Vyukov <dvyukov@google.com>2020-07-04 15:05:30 +0200
commitc7d7f10bdff703e4a3c0414e8a33d4e45c91eb35 (patch)
tree0dff0ee1f98dbfa3ad8776112053a450d176592b /vendor/github.com/jirfag
parent9573094ce235bd9afe88f5da27a47dd6bcc1e13b (diff)
go.mod: vendor golangci-lint
Diffstat (limited to 'vendor/github.com/jirfag')
-rw-r--r--vendor/github.com/jirfag/go-printf-func-name/pkg/analyzer/analyzer.go74
1 files changed, 74 insertions, 0 deletions
diff --git a/vendor/github.com/jirfag/go-printf-func-name/pkg/analyzer/analyzer.go b/vendor/github.com/jirfag/go-printf-func-name/pkg/analyzer/analyzer.go
new file mode 100644
index 000000000..7937dd433
--- /dev/null
+++ b/vendor/github.com/jirfag/go-printf-func-name/pkg/analyzer/analyzer.go
@@ -0,0 +1,74 @@
+package analyzer
+
+import (
+ "go/ast"
+ "strings"
+
+ "golang.org/x/tools/go/analysis/passes/inspect"
+ "golang.org/x/tools/go/ast/inspector"
+
+ "golang.org/x/tools/go/analysis"
+)
+
+var Analyzer = &analysis.Analyzer{
+ Name: "goprintffuncname",
+ Doc: "Checks that printf-like functions are named with `f` at the end.",
+ Run: run,
+ Requires: []*analysis.Analyzer{inspect.Analyzer},
+}
+
+func run(pass *analysis.Pass) (interface{}, error) {
+ inspector := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
+ nodeFilter := []ast.Node{
+ (*ast.FuncDecl)(nil),
+ }
+
+ inspector.Preorder(nodeFilter, func(node ast.Node) {
+ funcDecl := node.(*ast.FuncDecl)
+
+ if res := funcDecl.Type.Results; res != nil && len(res.List) != 0 {
+ return
+ }
+
+ params := funcDecl.Type.Params.List
+ if len(params) < 2 { // [0] must be format (string), [1] must be args (...interface{})
+ return
+ }
+
+ formatParamType, ok := params[len(params)-2].Type.(*ast.Ident)
+ if !ok { // first param type isn't identificator so it can't be of type "string"
+ return
+ }
+
+ if formatParamType.Name != "string" { // first param (format) type is not string
+ return
+ }
+
+ if formatParamNames := params[len(params)-2].Names; len(formatParamNames) == 0 || formatParamNames[len(formatParamNames)-1].Name != "format" {
+ return
+ }
+
+ argsParamType, ok := params[len(params)-1].Type.(*ast.Ellipsis)
+ if !ok { // args are not ellipsis (...args)
+ return
+ }
+
+ elementType, ok := argsParamType.Elt.(*ast.InterfaceType)
+ if !ok { // args are not of interface type, but we need interface{}
+ return
+ }
+
+ if elementType.Methods != nil && len(elementType.Methods.List) != 0 {
+ return // has >= 1 method in interface, but we need an empty interface "interface{}"
+ }
+
+ if strings.HasSuffix(funcDecl.Name.Name, "f") {
+ return
+ }
+
+ pass.Reportf(node.Pos(), "printf-like formatting function '%s' should be named '%sf'",
+ funcDecl.Name.Name, funcDecl.Name.Name)
+ })
+
+ return nil, nil
+}