go/analysis: make stdmethods happy on encoding/xml

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í <mvdan@mvdan.cc>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Michael Matloob <matloob@golang.org>
This commit is contained in:
Daniel Martí 2019-03-18 19:56:56 +00:00
parent 3f1ed9edd1
commit b6b7807791
2 changed files with 6 additions and 8 deletions

View File

@ -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
}

View File

@ -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")
}