Skip to content
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

Should ASTHelpers.getSymbol(Tree) be annotated with @Nullable ? #3760

Closed
XN137 opened this issue Feb 6, 2023 · 2 comments
Closed

Should ASTHelpers.getSymbol(Tree) be annotated with @Nullable ? #3760

XN137 opened this issue Feb 6, 2023 · 2 comments

Comments

@XN137
Copy link

XN137 commented Feb 6, 2023

according to the method javadoc it can return null in multiple cases:

/**
* Gets the symbol for a tree. Returns null if this tree does not have a symbol because it is of
* the wrong type, if {@code tree} is null, or if the symbol cannot be found due to a compilation
* error.
*/
// TODO(eaftan): refactor other code that accesses symbols to use this method
public static Symbol getSymbol(Tree tree) {

one of them being that it calls getDeclaredSymbol which is marked @Nullable already:

/**
* Gets the symbol declared by a tree. Returns null if {@code tree} does not declare a symbol or
* is null.
*/
@Nullable
public static Symbol getDeclaredSymbol(Tree tree) {

this seems kind of inconsistent.

there are of course multiple current usage spots where from the context + outer checks it is clear that it will not return null for example around AssignmentTree.getVariable() here:

Symbol rhsSymbol = getSymbol(assignmentTree.getExpression());
// If the RHS of the assignment is a variable with the same name as the field, just remove
// the assignment.
String assigneeName = getSymbol(assignmentTree.getVariable()).getSimpleName().toString();
if (rhsSymbol != null
&& assignmentTree.getExpression() instanceof IdentifierTree
&& rhsSymbol.getSimpleName().contentEquals(assigneeName)) {

but if the goal of the @Nullable annotation is to systematically force proper null handling maybe the annotation should be added.

WDYT ?
if it is an intentional decision, maybe there should be a comment about it to make it look less inconsistent.

@graememorgan
Copy link
Member

Yes, I think so. I wonder if I/we accidentally deleted that at some point.

copybara-service bot pushed a commit that referenced this issue Feb 9, 2023
Fixes external #3760.

PiperOrigin-RevId: 508166642
copybara-service bot pushed a commit that referenced this issue Feb 9, 2023
Fixes external #3760.

PiperOrigin-RevId: 508315637
@XN137
Copy link
Author

XN137 commented Feb 10, 2023

thanks for the quick feedback

closing since fixed by e08edcd

@XN137 XN137 closed this as completed Feb 10, 2023
msridhar pushed a commit to uber/NullAway that referenced this issue Mar 10, 2023
This method will return `@Nullable` in the next Error Prone release: google/error-prone#3760 This change prepares for that by adding appropriate null checks.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants