Skip to content

Commit 24f89cc

Browse files
committed
SA5010: don't try to reason about generics
1 parent 82145fc commit 24f89cc

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

staticcheck/sa5010/sa5010.go

+10-2
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ func run(pass *analysis.Pass) (any, error) {
5656
msc := &pass.ResultOf[buildir.Analyzer].(*buildir.IR).Pkg.Prog.MethodSets
5757
for _, fn := range pass.ResultOf[buildir.Analyzer].(*buildir.IR).SrcFuncs {
5858
for _, b := range fn.Blocks {
59+
instrLoop:
5960
for _, instr := range b.Instrs {
6061
assert, ok := instr.(*ir.TypeAssert)
6162
if !ok {
@@ -75,12 +76,19 @@ func run(pass *analysis.Pass) (any, error) {
7576

7677
ms := msc.MethodSet(left)
7778
for i := range righti.NumMethods() {
78-
mr := righti.Method(i).Origin()
79+
mr := righti.Method(i)
7980
sel := ms.Lookup(mr.Pkg(), mr.Name())
8081
if sel == nil {
8182
continue
8283
}
83-
ml := sel.Obj().(*types.Func).Origin()
84+
ml := sel.Obj().(*types.Func)
85+
if ml.Origin() != ml || mr.Origin() != mr {
86+
// Give up when we see generics.
87+
//
88+
// TODO(dh): support generics once go/types gets an
89+
// exported API for type unification.
90+
continue instrLoop
91+
}
8492
if types.AssignableTo(ml.Type(), mr.Type()) {
8593
continue
8694
}

staticcheck/sa5010/testdata/go1.18/CheckImpossibleTypeAssertion/CheckImpossibleTypeAssertion.go

+24-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,30 @@ func Fn1[T uint32 | uint64]() {
1010
}
1111

1212
func Fn2[T uint64]() {
13-
// In theory we should flag this, but we don't.
1413
var iface ExampleType[uint32]
14+
// TODO(dh): once we support generics, flag this
1515
_ = iface.(ExampleType[T])
1616
}
17+
18+
type I1[E any] interface {
19+
Do(E)
20+
Moo(E)
21+
}
22+
23+
type I2[E any] interface {
24+
Do(E)
25+
Moo(E)
26+
}
27+
28+
type I3[E any] interface {
29+
Do(E)
30+
Moo()
31+
}
32+
33+
func New[T any]() {
34+
var x I1[T]
35+
_ = x.(I2[T])
36+
37+
// TODO(dh): once we support generics, flag this
38+
_ = x.(I3[T])
39+
}

0 commit comments

Comments
 (0)