Debian Codegraph @

staticcheck/sa4023/testdata/go1.18/CheckTypedNilInterface/generics.go

raw
1 package pkg
2
3 func tpgen1[T *int]() T {
4 return (T)(nil)
5 }
6
7 func bar() {
8 if tpgen1() == nil {
9 }
10 }
11
12 func tpfn1[T any](x T) {
13 if any(x) == nil {
14 // this is entirely possible if baz is instantiated with an interface type for T. For example: baz[error](nil)
15 }
16 }
17
18 func tpfn2[T ~int](x T) {
19 if any(x) == nil { //@ diag(`this comparison is never true`)
20 // this is not possible, because T only accepts concrete types
21 }
22 }
23
24 func tpgen3[T any](x T) any {
25 _ = x // not a trivial function
26 return any(x)
27 }
28
29 func tpgen4[T ~*int](x T) any {
30 _ = x // not a trivial function
31 return any(x)
32 }
33
34 func tptest() {
35 _ = tpgen1() == nil
36
37 _ = tpgen3[error](nil) == nil
38
39 // ideally we'd flag this, but the analysis is generic-insensitive at the moment.
40 _ = tpgen3[*int](nil) == nil
41
42 _ = tpgen4[*int](nil) == nil //@ diag(`never true`)
43 }