-
Notifications
You must be signed in to change notification settings - Fork 291
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enforce correct type-use annotation locations for nested types #1045
Open
armughan11
wants to merge
3
commits into
uber:master
Choose a base branch
from
armughan11:type-use-location-nested-class
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+178
−20
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -89,6 +89,7 @@ | |
import com.sun.tools.javac.code.Symbol.ClassSymbol; | ||
import com.sun.tools.javac.code.Symbol.VarSymbol; | ||
import com.sun.tools.javac.code.Type; | ||
import com.sun.tools.javac.code.Types; | ||
import com.sun.tools.javac.processing.JavacProcessingEnvironment; | ||
import com.sun.tools.javac.tree.JCTree; | ||
import com.uber.nullaway.ErrorMessage.MessageTypes; | ||
|
@@ -1475,10 +1476,19 @@ | |
if (initializer != null) { | ||
if (!symbol.type.isPrimitive() && !skipDueToFieldAnnotation(symbol)) { | ||
if (mayBeNullExpr(state, initializer)) { | ||
ErrorMessage errorMessage = | ||
new ErrorMessage( | ||
MessageTypes.ASSIGN_FIELD_NULLABLE, | ||
"assigning @Nullable expression to @NonNull field"); | ||
ErrorMessage errorMessage; | ||
if (hasNestedClass(state.getTypes(), symbol.type)) { | ||
errorMessage = | ||
new ErrorMessage( | ||
MessageTypes.NULLABLE_ON_WRONG_NESTED_CLASS_LEVEL, | ||
"Type-use nullability annotations should be applied on inner class"); | ||
} else { | ||
errorMessage = | ||
new ErrorMessage( | ||
MessageTypes.ASSIGN_FIELD_NULLABLE, | ||
"assigning @Nullable expression to @NonNull field"); | ||
} | ||
|
||
return errorBuilder.createErrorDescriptionForNullAssignment( | ||
errorMessage, initializer, buildDescription(tree), state, symbol); | ||
} | ||
|
@@ -1487,6 +1497,22 @@ | |
return Description.NO_MATCH; | ||
} | ||
|
||
public static boolean hasNestedClass(Types types, Type type) { | ||
if (types == null || type == null) { | ||
return false; | ||
} | ||
Comment on lines
+1501
to
+1503
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If these null checks are necessary the parameters should be |
||
if (type.getKind() == TypeKind.DECLARED && type.isParameterized()) { | ||
return false; | ||
} | ||
|
||
Type enclosingType = type.getEnclosingType(); | ||
if (enclosingType == null) { | ||
return false; | ||
} | ||
|
||
return !types.isSameType(enclosingType, Type.noType); | ||
} | ||
|
||
/** | ||
* Check if an inner class's annotation means this Compilation Unit is partially annotated. | ||
* | ||
|
This file contains 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
This file contains 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can't understand this check and why it occurs here? I think the check should occur independent of whether the initializer assignment is valid. If we need to possibly report multiple error messages from this method, we can use the
state.reportMatch
API to report the errors and then just always returnDescription.NO_MATCH
.