Is there a go linter that can find "nil pointer dereference" potential panic?

Viewed 1215

Here is simple example:

package main

import "fmt"

type A struct {
    Name *string
}

type B struct {
    A *A
}

func main() {
    deref(&B{})
}

func deref(b *B) {
    fmt.Println(*b.A.Name) // panic: nil pointer dereference
}

I'm looking for a solution that can find this type of situations. There is a "nillness check" in Jetbrains Goland but it doesn't inspect this issue.

1 Answers

I asked your question somewhere else [1], and got this response:

Detecting all nil pointer dereferences is impossible without false positives, and we don't generally tolerate false positives. It would also be fairly expensive to compute.

Detecting trivial nil pointer dereferences like this one isn't worth it, because even with minor additions to the code they would no longer be easy to detect.

We do have SA5011 which flags a specific kind of potential nil pointer dereference, though this wouldn't help with your example.

  1. https://github.com/dominikh/go-tools/issues/1035
Related