Skip to content

Commit f97a804

Browse files
komem3timothy-king
authored andcommitted
go/analysis/passes/nilness: fixed slice not being considered as non-nil
The initialized slice will always be non-nil. But, nilness could not detect it, so I fixed it. Fixes golang/go#45177 Change-Id: I3bad2b8dee16331c4a24d2e93fb4baacd98f30ec GitHub-Last-Rev: f3a532e GitHub-Pull-Request: #291 Reviewed-on: https://go-review.googlesource.com/c/tools/+/303890 Run-TryBot: Tim King <[email protected]> Reviewed-by: Dmitri Shuralyov <[email protected]> Reviewed-by: Tim King <[email protected]> gopls-CI: kokoro <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Run-TryBot: Dmitri Shuralyov <[email protected]>
1 parent 13bcb69 commit f97a804

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

go/analysis/passes/nilness/nilness.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ func (n nilness) String() string { return nilnessStrings[n+1] }
251251
// or unknown given the dominating stack of facts.
252252
func nilnessOf(stack []fact, v ssa.Value) nilness {
253253
switch v := v.(type) {
254-
// unwrap ChangeInterface values recursively, to detect if underlying
254+
// unwrap ChangeInterface and Slice values recursively, to detect if underlying
255255
// values have any facts recorded or are otherwise known with regard to nilness.
256256
//
257257
// This work must be in addition to expanding facts about
@@ -265,6 +265,10 @@ func nilnessOf(stack []fact, v ssa.Value) nilness {
265265
if underlying := nilnessOf(stack, v.X); underlying != unknown {
266266
return underlying
267267
}
268+
case *ssa.Slice:
269+
if underlying := nilnessOf(stack, v.X); underlying != unknown {
270+
return underlying
271+
}
268272
case *ssa.SliceToArrayPointer:
269273
nn := nilnessOf(stack, v.X)
270274
if slice2ArrayPtrLen(v) > 0 {

go/analysis/passes/nilness/testdata/src/a/a.go

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,6 @@ func f9(x interface {
130130
b()
131131
c()
132132
}) {
133-
134133
x.b() // we don't catch this panic because we don't have any facts yet
135134
xx := interface {
136135
a()
@@ -155,11 +154,27 @@ func f9(x interface {
155154
}
156155
}
157156

157+
func f10() {
158+
s0 := make([]string, 0)
159+
if s0 == nil { // want "impossible condition: non-nil == nil"
160+
print(0)
161+
}
162+
163+
var s1 []string
164+
if s1 == nil { // want "tautological condition: nil == nil"
165+
print(0)
166+
}
167+
s2 := s1[:][:]
168+
if s2 == nil { // want "tautological condition: nil == nil"
169+
print(0)
170+
}
171+
}
172+
158173
func unknown() bool {
159174
return false
160175
}
161176

162-
func f10(a interface{}) {
177+
func f11(a interface{}) {
163178
switch a.(type) {
164179
case nil:
165180
return
@@ -170,7 +185,7 @@ func f10(a interface{}) {
170185
}
171186
}
172187

173-
func f11(a interface{}) {
188+
func f12(a interface{}) {
174189
switch a {
175190
case nil:
176191
return
@@ -190,7 +205,7 @@ type innerY struct {
190205
value int
191206
}
192207

193-
func f12() {
208+
func f13() {
194209
var d *Y
195210
print(d.value) // want "nil dereference in field selection"
196211
}

0 commit comments

Comments
 (0)