Handle explicit type conversions as potential nil producers (#166)#400
Open
nickita-khylkouski wants to merge 1 commit intouber-go:mainfrom
Open
Handle explicit type conversions as potential nil producers (#166)#400nickita-khylkouski wants to merge 1 commit intouber-go:mainfrom
nickita-khylkouski wants to merge 1 commit intouber-go:mainfrom
Conversation
Fixes uber-go#166 When parsing a CallExpr, we now check if it represents an explicit type conversion (e.g., (*Struct)(nil) or StructPtr(nil)). If so, we pass-through the nilability of the converted expression, since the result can be nil if and only if the argument is nil and the target type allows nil. Type conversions to non-nilable types (e.g., int(x)) continue to be treated as non-nil since those types cannot contain nil values. This catches nil dereferences from explicit type conversions that were previously false negatives.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #166
Problem
NilAway did not handle explicit type conversions (like (*Struct)(nil) or StructPtr(nil)), resulting in false negatives. These type conversions can produce nil values and should be treated as potential nil producers.
Example:
Solution
Added a check in ParseExprAsProducer to detect when a *ast.CallExpr represents an explicit type conversion (using TypesInfo.Types[expr.Fun].IsType()). When detected:
Testing
[x] All linting checks pass
[x] Full test suite passes
[x] New test cases added for: