Skip to content

Commit

Permalink
Merge pull request #248 from JacobOaks/joaks/supportgotypesalias
Browse files Browse the repository at this point in the history
Support GODEBUG=gotypesalias=1
  • Loading branch information
dtcaciuc authored Jul 2, 2024
2 parents f3c8b3c + d0d7a32 commit b832de3
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 17 deletions.
43 changes: 27 additions & 16 deletions errcheck/analyzer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,33 @@ import (
)

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

t.Run("check blanks", func(t *testing.T) {
packageDir := filepath.Join(analysistest.TestData(), "src/blank/")
_ = Analyzer.Flags.Set("blank", "true")
_ = analysistest.Run(t, packageDir, Analyzer)
_ = Analyzer.Flags.Set("blank", "false") // reset it
})
t.Run("default flags", func(t *testing.T) {
packageDir := filepath.Join(analysistest.TestData(), "src/a/")
_ = analysistest.Run(t, packageDir, Analyzer)
})

t.Run("check asserts", func(t *testing.T) {
packageDir := filepath.Join(analysistest.TestData(), "src/assert/")
_ = Analyzer.Flags.Set("assert", "true")
_ = analysistest.Run(t, packageDir, Analyzer)
_ = Analyzer.Flags.Set("assert", "false") // reset it
})
t.Run("check blanks", func(t *testing.T) {
packageDir := filepath.Join(analysistest.TestData(), "src/blank/")
_ = Analyzer.Flags.Set("blank", "true")
_ = analysistest.Run(t, packageDir, Analyzer)
_ = Analyzer.Flags.Set("blank", "false") // reset it
})

t.Run("check asserts", func(t *testing.T) {
packageDir := filepath.Join(analysistest.TestData(), "src/assert/")
_ = Analyzer.Flags.Set("assert", "true")
_ = analysistest.Run(t, packageDir, Analyzer)
_ = Analyzer.Flags.Set("assert", "false") // reset it
})
})
}
}
3 changes: 2 additions & 1 deletion errcheck/embedded_walker.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ func walkThroughEmbeddedInterfaces(sel *types.Selection) ([]types.Type, bool) {
}

func getTypeAtFieldIndex(startingAt types.Type, fieldIndex int) types.Type {
t := maybeUnname(maybeDereference(startingAt))
t := maybeDereference(maybeUnalias(startingAt))
t = maybeUnname(maybeUnalias(t))
s, ok := t.(*types.Struct)
if !ok {
panic(fmt.Sprintf("cannot get Field of a type that is not a struct, got a %T", t))
Expand Down
10 changes: 10 additions & 0 deletions errcheck/embedded_walker_121.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//go:build !go1.22
// +build !go1.22

package errcheck

import "go/types"

func maybeUnalias(t types.Type) types.Type {
return t
}
10 changes: 10 additions & 0 deletions errcheck/embedded_walker_122.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//go:build go1.22
// +build go1.22

package errcheck

import "go/types"

func maybeUnalias(t types.Type) types.Type {
return types.Unalias(t)
}
13 changes: 13 additions & 0 deletions errcheck/testdata/src/a/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,19 @@ func main() {
_ = x.a() // ok, assigned to blank
x.a() // want "unchecked error"

// Methods on alias types and pointers to alias types
x2 := embedtalias{}
_ = x2.a() // ok, assigned to blank
x2.a() // want "unchecked error"

x3 := &embedtalias{}
_ = x3.a() // ok, assigned to blank
x3.a() // want "unchecked error"

var x4 embedtptralias
_ = x4.a() // ok, assigned to blank
x4.a() // want "unchecked error"

// Method call on a struct member
y := u{x}
_ = y.t.a() // ok, assigned to blank
Expand Down
8 changes: 8 additions & 0 deletions errcheck/testdata/src/a/t.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,11 @@ func (x t) a() error {
type u struct {
t t
}

type embedt struct {
t
}

type embedtalias = embedt

type embedtptralias = *embedt

0 comments on commit b832de3

Please sign in to comment.