Skip to content

Commit b832de3

Browse files
authored
Merge pull request #248 from JacobOaks/joaks/supportgotypesalias
Support GODEBUG=gotypesalias=1
2 parents f3c8b3c + d0d7a32 commit b832de3

File tree

6 files changed

+70
-17
lines changed

6 files changed

+70
-17
lines changed

errcheck/analyzer_test.go

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,33 @@ import (
88
)
99

1010
func TestAnalyzer(t *testing.T) {
11-
t.Run("default flags", func(t *testing.T) {
12-
packageDir := filepath.Join(analysistest.TestData(), "src/a/")
13-
_ = analysistest.Run(t, packageDir, Analyzer)
14-
})
11+
// Test with and without the gotypesalias flag,
12+
// which will be set to 1 by default in Go 1.23.
13+
for _, tt := range []string{
14+
"gotypesalias=0",
15+
"gotypesalias=1",
16+
} {
17+
t.Run(tt, func(t *testing.T) {
18+
t.Setenv("GODEBUG", tt)
1519

16-
t.Run("check blanks", func(t *testing.T) {
17-
packageDir := filepath.Join(analysistest.TestData(), "src/blank/")
18-
_ = Analyzer.Flags.Set("blank", "true")
19-
_ = analysistest.Run(t, packageDir, Analyzer)
20-
_ = Analyzer.Flags.Set("blank", "false") // reset it
21-
})
20+
t.Run("default flags", func(t *testing.T) {
21+
packageDir := filepath.Join(analysistest.TestData(), "src/a/")
22+
_ = analysistest.Run(t, packageDir, Analyzer)
23+
})
2224

23-
t.Run("check asserts", func(t *testing.T) {
24-
packageDir := filepath.Join(analysistest.TestData(), "src/assert/")
25-
_ = Analyzer.Flags.Set("assert", "true")
26-
_ = analysistest.Run(t, packageDir, Analyzer)
27-
_ = Analyzer.Flags.Set("assert", "false") // reset it
28-
})
25+
t.Run("check blanks", func(t *testing.T) {
26+
packageDir := filepath.Join(analysistest.TestData(), "src/blank/")
27+
_ = Analyzer.Flags.Set("blank", "true")
28+
_ = analysistest.Run(t, packageDir, Analyzer)
29+
_ = Analyzer.Flags.Set("blank", "false") // reset it
30+
})
31+
32+
t.Run("check asserts", func(t *testing.T) {
33+
packageDir := filepath.Join(analysistest.TestData(), "src/assert/")
34+
_ = Analyzer.Flags.Set("assert", "true")
35+
_ = analysistest.Run(t, packageDir, Analyzer)
36+
_ = Analyzer.Flags.Set("assert", "false") // reset it
37+
})
38+
})
39+
}
2940
}

errcheck/embedded_walker.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ func walkThroughEmbeddedInterfaces(sel *types.Selection) ([]types.Type, bool) {
8484
}
8585

8686
func getTypeAtFieldIndex(startingAt types.Type, fieldIndex int) types.Type {
87-
t := maybeUnname(maybeDereference(startingAt))
87+
t := maybeDereference(maybeUnalias(startingAt))
88+
t = maybeUnname(maybeUnalias(t))
8889
s, ok := t.(*types.Struct)
8990
if !ok {
9091
panic(fmt.Sprintf("cannot get Field of a type that is not a struct, got a %T", t))

errcheck/embedded_walker_121.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//go:build !go1.22
2+
// +build !go1.22
3+
4+
package errcheck
5+
6+
import "go/types"
7+
8+
func maybeUnalias(t types.Type) types.Type {
9+
return t
10+
}

errcheck/embedded_walker_122.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//go:build go1.22
2+
// +build go1.22
3+
4+
package errcheck
5+
6+
import "go/types"
7+
8+
func maybeUnalias(t types.Type) types.Type {
9+
return types.Unalias(t)
10+
}

errcheck/testdata/src/a/main.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,19 @@ func main() {
105105
_ = x.a() // ok, assigned to blank
106106
x.a() // want "unchecked error"
107107

108+
// Methods on alias types and pointers to alias types
109+
x2 := embedtalias{}
110+
_ = x2.a() // ok, assigned to blank
111+
x2.a() // want "unchecked error"
112+
113+
x3 := &embedtalias{}
114+
_ = x3.a() // ok, assigned to blank
115+
x3.a() // want "unchecked error"
116+
117+
var x4 embedtptralias
118+
_ = x4.a() // ok, assigned to blank
119+
x4.a() // want "unchecked error"
120+
108121
// Method call on a struct member
109122
y := u{x}
110123
_ = y.t.a() // ok, assigned to blank

errcheck/testdata/src/a/t.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,11 @@ func (x t) a() error {
1515
type u struct {
1616
t t
1717
}
18+
19+
type embedt struct {
20+
t
21+
}
22+
23+
type embedtalias = embedt
24+
25+
type embedtptralias = *embedt

0 commit comments

Comments
 (0)