nilaway icon indicating copy to clipboard operation
nilaway copied to clipboard

Support validator patterns

Open yuxincs opened this issue 4 months ago • 0 comments

NilAway currently does not track the side effects of validator functions:

//nilable(a)
func validate(a *int) bool {
	return a != nil
}

//nilable(a)
func foo(a *int) {
	if validate(a) {
		// `a` should be nonnil now.
		print(*a)
	}
}

But NilAway complains

unexpected diagnostic: Potential nil panic detected. Observed nil flow from source to dereference point: 
        	- foo.go:31:10: function parameter `a` dereferenced

It could also be the case that the validate function validates certain fields of a struct and returns a boolean variable. Tracking this kind of side effects could be expensive, and we should think of a smart way to handle this in an efficient manner.

yuxincs avatar Mar 05 '24 15:03 yuxincs