Skip to content

Commit b6b7807

Browse files
committed
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í <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Michael Matloob <[email protected]>
1 parent 3f1ed9e commit b6b7807

File tree

2 files changed

+6
-8
lines changed

2 files changed

+6
-8
lines changed

go/analysis/passes/stdmethods/stdmethods.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ package stdmethods
88

99
import (
1010
"go/ast"
11-
"go/token"
1211
"go/types"
1312
"strings"
1413

@@ -163,7 +162,7 @@ func matchParams(pass *analysis.Pass, expect []string, actual *types.Tuple, pref
163162
if i >= actual.Len() {
164163
return false
165164
}
166-
if !matchParamType(pass.Fset, pass.Pkg, x, actual.At(i).Type()) {
165+
if !matchParamType(x, actual.At(i).Type()) {
167166
return false
168167
}
169168
}
@@ -174,13 +173,8 @@ func matchParams(pass *analysis.Pass, expect []string, actual *types.Tuple, pref
174173
}
175174

176175
// Does this one type match?
177-
func matchParamType(fset *token.FileSet, pkg *types.Package, expect string, actual types.Type) bool {
176+
func matchParamType(expect string, actual types.Type) bool {
178177
expect = strings.TrimPrefix(expect, "=")
179-
// Strip package name if we're in that package.
180-
if n := len(pkg.Name()); len(expect) > n && expect[:n] == pkg.Name() && expect[n] == '.' {
181-
expect = expect[n+1:]
182-
}
183-
184178
// Overkill but easy.
185179
return typeString(actual) == expect
186180
}

go/analysis/passes/stdmethods/stdmethods_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,7 @@ func Test(t *testing.T) {
1515
testdata := analysistest.TestData()
1616
analysistest.Run(t, testdata, stdmethods.Analyzer, "a")
1717
}
18+
19+
func TestAnalyzeEncodingXML(t *testing.T) {
20+
analysistest.Run(t, "", stdmethods.Analyzer, "encoding/xml")
21+
}

0 commit comments

Comments
 (0)