Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Support GODEBUG=gotypesalias=1 #248

Merged
merged 2 commits into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading