8000 Detecting Nil-Initiated Fields in Constructors · Issue #301 · uber-go/nilaway · GitHub
[go: up one dir, main page]

Skip to content

Detecting Nil-Initiated Fields in Constructors #301

Open

Description

@k-omotani

When running nilaway on the following code, I noticed that fields initialized as nil in the constructor are not being detected. The output is as follows:

package main

import (
	"fmt"
)

func main() {
	myInterface1 := NewMyInterface1()
	fmt.Println(myInterface1.Get().String())

	myInterface2 := NewMyInterface2()
	fmt.Println(myInterface2.Get().String())
}

type MyInterface interface {
	Get() Value
}

type Value interface {
	String() Value
}

type MyStruct1 struct {
	v Value
}

func NewMyInterface1() MyInterface {
	return &MyStruct1{
		v: nil,
	}
}

func (m MyStruct1) Get() Value {
	return m.v
}

type MyStruct2 struct{}

func (m MyStruct2) Get() Value {
	return nil
}

func NewMyInterface2() MyInterface {
	return &MyStruct2{}
}

From nilaway, I get the following output:

error: Potential nil panic detected. Observed nil flow from source to dereference point:
	- nilaway-sample/main.go:40:9: literal `nil` returned from `Get()` in position 0
	- nilaway-sample/main.go:16:2: returned as result 0 from interface method `MyInterface.Get()` (implemented by `MyStruct2.Get()`)
	- nilaway-sample/main.go:12:14: result 0 of `Get()` called `String()`

Question

In the case of MyStruct1, where a field is initialized as nil in the constructor, it seems that nilaway does not detect it. Is there a way to make nilaway detect fields initialized as nil in constructors?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      0