+
Skip to content

False positive: function returns non-nil value together with error #365

@manXcoder

Description

@manXcoder

❌ Error case (a.New from another package):

// a/a.go
type A struct {
	V int
}
func New() (*A, error) {
	a := &A{}
	if rand.Int()%2 == 0 {
		return a, errors.New("random error")
	}
	return a, nil
}

// b.go
func test() {
    x, _ := a.New()
    print(x.V) // nilaway reports an error
}

✅ No error case (New in the same package):

type A struct {
	V int
}
func New() (*A, error) {
	a := &A{}
	if rand.Int()%2 == 0 {
		return a, errors.New("random error")
	}
	return a, nil
}

func test() {
    x, _ := New()
    print(x.V) // nilaway does NOT report an error
}

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

      点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载