aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/sonatard/noctx/ngfunc
diff options
context:
space:
mode:
authorDmitry Vyukov <dvyukov@google.com>2020-09-15 18:05:35 +0200
committerDmitry Vyukov <dvyukov@google.com>2020-09-15 19:34:30 +0200
commit712de1c63d9db97c81af68cd0dc4372c53d2e57a (patch)
treeae1761fec52c3ae4ddd003a4130ddbda8d0a2d69 /vendor/github.com/sonatard/noctx/ngfunc
parent298a69c38dd5c8a9bbd7a022e88f4ddbcf885e16 (diff)
vendor/github.com/golangci/golangci-lint: update to v1.31
Diffstat (limited to 'vendor/github.com/sonatard/noctx/ngfunc')
-rw-r--r--vendor/github.com/sonatard/noctx/ngfunc/main.go57
-rw-r--r--vendor/github.com/sonatard/noctx/ngfunc/report.go29
-rw-r--r--vendor/github.com/sonatard/noctx/ngfunc/types.go65
3 files changed, 151 insertions, 0 deletions
diff --git a/vendor/github.com/sonatard/noctx/ngfunc/main.go b/vendor/github.com/sonatard/noctx/ngfunc/main.go
new file mode 100644
index 000000000..cfeb0f001
--- /dev/null
+++ b/vendor/github.com/sonatard/noctx/ngfunc/main.go
@@ -0,0 +1,57 @@
+package ngfunc
+
+import (
+ "go/types"
+
+ "github.com/gostaticanalysis/analysisutil"
+ "golang.org/x/tools/go/analysis"
+ "golang.org/x/tools/go/analysis/passes/buildssa"
+)
+
+func Run(pass *analysis.Pass) (interface{}, error) {
+ ngFuncNames := []string{
+ "net/http.Get",
+ "net/http.Head",
+ "net/http.Post",
+ "net/http.PostForm",
+ "(*net/http.Client).Get",
+ "(*net/http.Client).Head",
+ "(*net/http.Client).Post",
+ "(*net/http.Client).PostForm",
+ }
+
+ ngFuncs := typeFuncs(pass, ngFuncNames)
+ if len(ngFuncs) == 0 {
+ return nil, nil
+ }
+
+ reportFuncs := ngCalledFuncs(pass, ngFuncs)
+ report(pass, reportFuncs)
+
+ return nil, nil
+}
+
+func ngCalledFuncs(pass *analysis.Pass, ngFuncs []*types.Func) []*Report {
+ var reports []*Report
+
+ srcFuncs := pass.ResultOf[buildssa.Analyzer].(*buildssa.SSA).SrcFuncs
+ for _, sf := range srcFuncs {
+ for _, b := range sf.Blocks {
+ for _, instr := range b.Instrs {
+ for _, ngFunc := range ngFuncs {
+ if analysisutil.Called(instr, nil, ngFunc) {
+ ngCalledFunc := &Report{
+ Instruction: instr,
+ function: ngFunc,
+ }
+ reports = append(reports, ngCalledFunc)
+
+ break
+ }
+ }
+ }
+ }
+ }
+
+ return reports
+}
diff --git a/vendor/github.com/sonatard/noctx/ngfunc/report.go b/vendor/github.com/sonatard/noctx/ngfunc/report.go
new file mode 100644
index 000000000..e50051798
--- /dev/null
+++ b/vendor/github.com/sonatard/noctx/ngfunc/report.go
@@ -0,0 +1,29 @@
+package ngfunc
+
+import (
+ "fmt"
+ "go/token"
+ "go/types"
+
+ "golang.org/x/tools/go/analysis"
+ "golang.org/x/tools/go/ssa"
+)
+
+type Report struct {
+ Instruction ssa.Instruction
+ function *types.Func
+}
+
+func (n *Report) Pos() token.Pos {
+ return n.Instruction.Pos()
+}
+
+func (n *Report) Message() string {
+ return fmt.Sprintf("%s must not be called", n.function.FullName())
+}
+
+func report(pass *analysis.Pass, reports []*Report) {
+ for _, report := range reports {
+ pass.Reportf(report.Pos(), report.Message())
+ }
+}
diff --git a/vendor/github.com/sonatard/noctx/ngfunc/types.go b/vendor/github.com/sonatard/noctx/ngfunc/types.go
new file mode 100644
index 000000000..f1877386c
--- /dev/null
+++ b/vendor/github.com/sonatard/noctx/ngfunc/types.go
@@ -0,0 +1,65 @@
+package ngfunc
+
+import (
+ "fmt"
+ "go/types"
+ "strings"
+
+ "github.com/gostaticanalysis/analysisutil"
+ "golang.org/x/tools/go/analysis"
+)
+
+var errNotFound = fmt.Errorf("function not found")
+
+func typeFuncs(pass *analysis.Pass, funcs []string) []*types.Func {
+ fs := make([]*types.Func, 0, len(funcs))
+
+ for _, fn := range funcs {
+ f, err := typeFunc(pass, fn)
+ if err != nil {
+ continue
+ }
+
+ fs = append(fs, f)
+ }
+
+ return fs
+}
+
+func typeFunc(pass *analysis.Pass, funcName string) (*types.Func, error) {
+ ss := strings.Split(strings.TrimSpace(funcName), ".")
+
+ switch len(ss) {
+ case 2:
+ // package function: pkgname.Func
+ f, ok := analysisutil.ObjectOf(pass, ss[0], ss[1]).(*types.Func)
+ if !ok || f == nil {
+ return nil, errNotFound
+ }
+
+ return f, nil
+ case 3:
+ // method: (*pkgname.Type).Method
+ pkgname := strings.TrimLeft(ss[0], "(")
+ typename := strings.TrimRight(ss[1], ")")
+
+ if pkgname != "" && pkgname[0] == '*' {
+ pkgname = pkgname[1:]
+ typename = "*" + typename
+ }
+
+ typ := analysisutil.TypeOf(pass, pkgname, typename)
+ if typ == nil {
+ return nil, errNotFound
+ }
+
+ m := analysisutil.MethodOf(typ, ss[2])
+ if m == nil {
+ return nil, errNotFound
+ }
+
+ return m, nil
+ }
+
+ return nil, errNotFound
+}