uber-go / nilaway

Static analysis tool to detect potential nil panics in Go code
Apache License 2.0
3.2k stars 66 forks source link

Refine tracking of slicing index #268

Open sonalmahajan15 opened 3 months ago

sonalmahajan15 commented 3 months ago

For the below case, NilAway correctly tracks the implication of slicing an empty slice and does not report an error.

var x []int
_ = x[:0]

However, when the slicing index is a non-literal, like below, NilAway currently reports a false positive. Refine tracking of non-literal indices to understand such code patterns and avoid false positives. Update func (r *RootAssertionNode) isZeroSlicing(expr *ast.SliceExpr) bool { ... } method to add the support.

var x []int
_ = x[:min(len(x), 100)] // FP: unassigned variable `x` sliced into