uber-go / nilaway

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

Fix for nested non-builtin call expression in map access #245

Closed sonalmahajan15 closed 6 months ago

sonalmahajan15 commented 6 months ago

This PR refines the logic of non-literal map access, particularly in the case of built-in call expressions, to not consider the index trackable if there exists a nested call to non-trackable expression (e.g., custom function) from inside the built-in function.

For example,

type MessageBlock struct{}

func (m *MessageBlock) Messages() []*int {
    return []*int{new(int)}
}

func test(msgSet []*MessageBlock) {
    for _, msgBlock := range msgSet {
        _ = *msgBlock.Messages()[len(msgBlock.Messages())-1]
    }
}

the test function was taking 7 iterations to converge, while now after the fix it requires only 3 iterations.

Note: the goal of this PR is to improve performance only, and should not have any effect on the reported errors.

codecov[bot] commented 6 months ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Project coverage is 87.56%. Comparing base (482b433) to head (bf4cd96).

Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #245 +/- ## ======================================= Coverage 87.55% 87.56% ======================================= Files 62 62 Lines 7828 7832 +4 ======================================= + Hits 6854 6858 +4 Misses 796 796 Partials 178 178 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

github-actions[bot] commented 6 months ago

Golden Test

[!NOTE]
✅ NilAway errors reported on standard libraries are identical.

3296 errors on base branch (main, 482b433) 3296 errors on test branch (25130e6)