Skip to content

Commit c1104c2

Browse files
committed
analysis/facts/nilness: handle generic CompositeValue
(cherry picked from commit e682878)
1 parent 68c1c4d commit c1104c2

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

analysis/facts/nilness/nilness.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,11 @@ func impl(pass *analysis.Pass, fn *ir.Function, seenFns map[*ir.Function]struct{
230230
return neverNil
231231
case *ir.TypeAssert, *ir.ChangeInterface, *ir.Field, *ir.Const, *ir.GenericConst, *ir.Index, *ir.MapLookup, *ir.Parameter, *ir.Recv, *ir.TypeSwitch:
232232
return nilly
233+
case *ir.CompositeValue:
234+
// We can get here via composite literals of type parameters, for which typeutil.IsPointerLike doesn't
235+
// currently return false (see https://staticcheck.io/issues/1364). However, we only emit ir.CompositeValue
236+
// for value types, so we know it can't be nil.
237+
return neverNil
233238
default:
234239
panic(fmt.Sprintf("internal error: unhandled type %T", v))
235240
}

analysis/facts/nilness/testdata/src/example.com/Nilness/Nilness.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,7 @@ func fn20() *int {
101101
}
102102
return nil
103103
}
104+
105+
func fn27[T ~struct{ F int }]() T { // want fn27:`never returns nil: \[never\]`
106+
return T{0}
107+
}

0 commit comments

Comments
 (0)