Skip to content

Commit

Permalink
Let checkers extend the error message for return statements.
Browse files Browse the repository at this point in the history
  • Loading branch information
cpovirk committed Dec 30, 2020
1 parent 1e9cd33 commit 508185c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1843,7 +1843,10 @@ public Void visitLambdaExpression(LambdaExpressionTree node, Void p) {
if (ret.getKind() != TypeKind.VOID) {
visitorState.setAssignmentContext(Pair.of((Tree) node, ret));
commonAssignmentCheck(
ret, (ExpressionTree) node.getBody(), "return.type.incompatible");
ret,
(ExpressionTree) node.getBody(),
"return.type.incompatible",
extraArgForReturnTypeError(node.getBody()));
}
}

Expand Down Expand Up @@ -1908,7 +1911,11 @@ public Void visitReturn(ReturnTree node, Void p) {
if (ret != null) {
visitorState.setAssignmentContext(Pair.of((Tree) node, ret));

commonAssignmentCheck(ret, node.getExpression(), "return.type.incompatible");
commonAssignmentCheck(
ret,
node.getExpression(),
"return.type.incompatible",
extraArgForReturnTypeError(node.getExpression()));
}
return super.visitReturn(node, p);
} finally {
Expand Down Expand Up @@ -2790,6 +2797,10 @@ protected final void commonAssignmentCheckEndDiagnostic(
}
}

protected String extraArgForReturnTypeError(Tree returnedExpression) {
return "";
}

/**
* Class that creates string representations of {@link AnnotatedTypeMirror}s which are only
* verbose if required to differentiate the two types.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ unary.increment.type.incompatible=increment result incompatible with variable de
unary.decrement.type.incompatible=decrement result incompatible with variable declared type.%nfound : %s%nrequired: %s
enhancedfor.type.incompatible=incompatible types in enhanced for loop.%nfound : %s%nrequired: %s
vector.copyinto.type.incompatible=incompatible component type in Vector.copyinto.%nfound : %s%nrequired: %s
return.type.incompatible=incompatible types in return.%ntype of expression: %s%nmethod return type: %s
return.type.incompatible=incompatible types in return.%n%stype of expression: %s%nmethod return type: %s
annotation.type.incompatible=incompatible types in annotation.%nfound : %s%nrequired: %s
conditional.type.incompatible=incompatible types in conditional expression.%nfound : %s%nrequired: %s
type.argument.type.incompatible=incompatible type argument for type parameter %s of %s.%nfound : %s%nrequired: %s
Expand Down

0 comments on commit 508185c

Please sign in to comment.