diff options
author | Will Chandler <wchandler@gitlab.com> | 2023-08-29 22:41:40 +0300 |
---|---|---|
committer | Will Chandler <wchandler@gitlab.com> | 2023-08-30 16:39:24 +0300 |
commit | 534faf1e47f9e324afa38069393aef75d19a53ea (patch) | |
tree | 27a61be31398aee7bdbfcefd318ce9b98df40b2b /tools/golangci-lint/gitaly | |
parent | eaef853599d9b4b388924b99a7872761413a22b0 (diff) |
lint: Match bare function calls
Currently `Matcher` will only match against selector expressions, e.g.
`fmt.Println`. A bare function call, e.g. `Println`, will not be
matched.
Update `Matcher` to handle both package qualified and bare function
calls.
Also, fix the test comment for `NewUnavailable` to include `want`. Prior
to updating `Matcher` this condition was not being tested and was
silently failing. We leave off the URL at the end of the report for ease
of matching, this was being interpreted as a regexp by the `analysis`
package and is a pain to escape properly.
Diffstat (limited to 'tools/golangci-lint/gitaly')
-rw-r--r-- | tools/golangci-lint/gitaly/matcher.go | 15 | ||||
-rw-r--r-- | tools/golangci-lint/gitaly/testdata/src/unavailable_code/unavailable_code_test.go | 2 |
2 files changed, 13 insertions, 4 deletions
diff --git a/tools/golangci-lint/gitaly/matcher.go b/tools/golangci-lint/gitaly/matcher.go index 9b180a726..ec7620792 100644 --- a/tools/golangci-lint/gitaly/matcher.go +++ b/tools/golangci-lint/gitaly/matcher.go @@ -33,6 +33,8 @@ var funcNamePattern = regexp.MustCompile(`^\(?([^\\)].*)\)?\.(.*)$`) // "gitlab.com/gitlab-org/gitaly/v15/internal/structerr.NewInternal", // - A function of a struct inside a package: // "(*gitlab.com/gitlab-org/gitaly/v15/internal/structerr.Error).Unwrap", +// - A local function call: +// "New(1)", // // This Matcher doesn't support interface match (yet). func (m *Matcher) MatchFunction(call *ast.CallExpr, rules []string) bool { @@ -76,11 +78,18 @@ func (m *Matcher) functionName(call *ast.CallExpr) string { } func (m *Matcher) getFunction(call *ast.CallExpr) (*types.Func, bool) { - sel, ok := call.Fun.(*ast.SelectorExpr) - if !ok { + var ident *ast.Ident + + switch ty := call.Fun.(type) { + case *ast.SelectorExpr: + ident = ty.Sel + case *ast.Ident: + ident = ty + default: return nil, false } - fn, ok := m.typesInfo.ObjectOf(sel.Sel).(*types.Func) + + fn, ok := m.typesInfo.ObjectOf(ident).(*types.Func) if !ok { return nil, false } diff --git a/tools/golangci-lint/gitaly/testdata/src/unavailable_code/unavailable_code_test.go b/tools/golangci-lint/gitaly/testdata/src/unavailable_code/unavailable_code_test.go index 5493e3f7a..b107aa4ae 100644 --- a/tools/golangci-lint/gitaly/testdata/src/unavailable_code/unavailable_code_test.go +++ b/tools/golangci-lint/gitaly/testdata/src/unavailable_code/unavailable_code_test.go @@ -17,5 +17,5 @@ func errorWrapOkay() { } func errorWrapNotOkay() { - _ = NewUnavailable("hello world") // please avoid using the Unavailable status code: https://gitlab.com/gitlab-org/gitaly/-/blob/master/STYLE.md?plain=0#unavailable-code + _ = NewUnavailable("hello world") // want "please avoid using the Unavailable status code.*" } |