A nil pointer, map or slice is used in a way that panics

tx0/go:nil-dereferencerecommendedwarningbug

What it finds

A pointer, map or slice that is nil on every path to a use that panics on nil: a field read or written through a nil pointer, *p, or an element written into a nil map or slice.

``go var counts map[string]int for _, w := range words { counts[w]++ // panics: counts was never made } ``

The rule follows each function through the shared IR with the nullness analysis, keeping apart up to four paths, so a variable assigned on some paths and checked before use on the others is not reported. A value is nil only by a nil literal, by the zero value of a type written as a pointer, map, slice, channel, function or interface, or because the path took a comparison with nil. Variables whose address is taken, or that a function literal assigns, may change behind the analysis' back and are never nil to it. Functions with constructs the lowering does not understand are skipped, and so are paths where the analysis gives up. So are functions that defer something named like recover: a panic there is usually the point, as in tests of recovery.

From the rule's source, nil_dereference.rs.

Example from the rule's tests
package p import "unsafe" type T struct{ n int } func count(words []string) map[string]int {	var counts map[string]int	for _, w := range words {		counts[w]++tx0 reports: counts is nil here, so assigning an element to it panics	}	return counts} func made(words []string) map[string]int {	counts := make(map[string]int)	for _, w := range words {		counts[w]++	}	return counts} func field() int {	var t *T	return t.ntx0 reports: t is nil here, so dereferencing it panics} func checked(t *T) int {	if t == nil {		return t.ntx0 reports: t is nil here, so dereferencing it panics	}	return t.n} func assignedOnSomePaths(ok bool) int {	var t *T	if ok {		t = &T{}	}	return t.n} func assignedOnEveryPath(ok bool) int {	var t *T	if ok {		t = &T{n: 1}	} else {		t = new(T)	}	return t.n} func correlated(ok bool) int {	var t *T	if ok {		t = &T{}	}	if ok {		return t.n	}	return 0} func guarded(xs []*T) int {	var last *T	for _, x := range xs {		if last != nil {			last.n++		}		last = x	}	return 0} func byPointer(load func(**T)) int {	var t *T	load(&t)	return t.n} func inClosure() int {	var t *T	set := func() { t = &T{} }	set()	return t.n} func deref() int {	var p *int	return *ptx0 reports: p is nil here, so dereferencing it panics} func method() {	var t *T	t.String()} func (t *T) String() string { return "" } func size() uintptr {	var t *T	return unsafe.Sizeof(*t)} func intended(p *parser) {	defer p.recover()	var a []int	a[1] = 2}
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/go:nil-dereference" = "warning"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

// tx0: ignore[tx0/go:nil-dereference] reason="..."

tx0 explain tx0/go:nil-dereference shows the same in the terminal. Found a false positive? Report it.