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 return any(x)
26 }
27
28 func tpgen4[T ~*int](x T) any {
29 return any(x)
30 }
31
32 func tptest() {
33 _ = tpgen1() == nil
34
35 _ = tpgen3[error](nil) == nil
36
37 // ideally we'd flag this, but the analysis is generic-insensitive at the moment.
38 _ = tpgen3[*int](nil) == nil
39
40 _ = tpgen4[*int](nil) == nil //@ diag(`never true`)
41 }