From b6b7807791df8aa3e230578a98256106c4f912ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=AD?= Date: Mon, 18 Mar 2019 19:56:56 +0000 Subject: [PATCH] go/analysis: make stdmethods happy on encoding/xml MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Historically, vet had always been unhappy about encoding/xml itself: method MarshalXML(e *xml.Encoder, start xml.StartElement) error should have signature MarshalXML(*xml.Encoder, xml.StartElement) error This dates back to the time when vet couldn't depend on type information. It compared the type expressions directly as strings, which was a problem when the code was in encoding/xml itself. There, the function parameters are *Encoder and StartElement, not *xml.Encoder and xml.StartElement. However, vet has been depending on type information for a while, so this restriction no longer makes sense. The analyzer almost got it right, but the only stopgap was a piece of the old code that tried to compare type expression strings. Remove it; typeString already deals with these edge cases for us. To ensure vet remains happy with encoding/xml, add a very simple test for it. The package now has zero reports, so the fact that its source has zero "// want" comments is appropriate. Finally, remove some long unused parameters from matchParamType. Change-Id: Iab3ed57da7bc4a80522ae21e62b67e7828b97c89 Reviewed-on: https://go-review.googlesource.com/c/tools/+/168058 Run-TryBot: Daniel Martí TryBot-Result: Gobot Gobot Reviewed-by: Michael Matloob --- go/analysis/passes/stdmethods/stdmethods.go | 10 ++-------- go/analysis/passes/stdmethods/stdmethods_test.go | 4 ++++ 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/go/analysis/passes/stdmethods/stdmethods.go b/go/analysis/passes/stdmethods/stdmethods.go index 83495112..72530a0e 100644 --- a/go/analysis/passes/stdmethods/stdmethods.go +++ b/go/analysis/passes/stdmethods/stdmethods.go @@ -8,7 +8,6 @@ package stdmethods import ( "go/ast" - "go/token" "go/types" "strings" @@ -163,7 +162,7 @@ func matchParams(pass *analysis.Pass, expect []string, actual *types.Tuple, pref if i >= actual.Len() { return false } - if !matchParamType(pass.Fset, pass.Pkg, x, actual.At(i).Type()) { + if !matchParamType(x, actual.At(i).Type()) { return false } } @@ -174,13 +173,8 @@ func matchParams(pass *analysis.Pass, expect []string, actual *types.Tuple, pref } // Does this one type match? -func matchParamType(fset *token.FileSet, pkg *types.Package, expect string, actual types.Type) bool { +func matchParamType(expect string, actual types.Type) bool { expect = strings.TrimPrefix(expect, "=") - // Strip package name if we're in that package. - if n := len(pkg.Name()); len(expect) > n && expect[:n] == pkg.Name() && expect[n] == '.' { - expect = expect[n+1:] - } - // Overkill but easy. return typeString(actual) == expect } diff --git a/go/analysis/passes/stdmethods/stdmethods_test.go b/go/analysis/passes/stdmethods/stdmethods_test.go index fe4fe53c..60b1a53c 100644 --- a/go/analysis/passes/stdmethods/stdmethods_test.go +++ b/go/analysis/passes/stdmethods/stdmethods_test.go @@ -15,3 +15,7 @@ func Test(t *testing.T) { testdata := analysistest.TestData() analysistest.Run(t, testdata, stdmethods.Analyzer, "a") } + +func TestAnalyzeEncodingXML(t *testing.T) { + analysistest.Run(t, "", stdmethods.Analyzer, "encoding/xml") +}