Skip to content

Commit

Permalink
Validate return types correctly. (#4844)
Browse files Browse the repository at this point in the history
  • Loading branch information
smillst committed Jul 29, 2021
1 parent 83f58dc commit 77a7c93
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
2 changes: 1 addition & 1 deletion checker/tests/nullness/Issue1027.java
Expand Up @@ -19,7 +19,7 @@ class Repr<T> {
void bar(Function<T, String> p) {}
}

@SuppressWarnings("nullness")
@SuppressWarnings({"nullness", "keyfor"})
Repr<@KeyFor("this") String> foo() {
return null;
}
Expand Down
2 changes: 1 addition & 1 deletion checker/tests/nullness/java8inference/Issue1084.java
Expand Up @@ -7,7 +7,7 @@ class MyOpt<T extends Object> {
static <S> MyOpt<@NonNull S> empty() {
throw new RuntimeException();
}

// :: error: (type.argument)
static <S> MyOpt<S> of(S p) {
throw new RuntimeException();
}
Expand Down
Expand Up @@ -4,6 +4,7 @@
import com.sun.source.tree.ClassTree;
import com.sun.source.tree.ExpressionTree;
import com.sun.source.tree.IdentifierTree;
import com.sun.source.tree.MethodTree;
import com.sun.source.tree.NewClassTree;
import com.sun.source.tree.ParameterizedTypeTree;
import com.sun.source.tree.Tree;
Expand Down Expand Up @@ -407,13 +408,13 @@ protected void visitTypeParameterBounds(
}

/**
* If {@code tree} has a type parameter tree, then the tree and its type is returned. Otherwise
* null and {@code type} are returned.
* If {@code tree} has a {@link ParameterizedTypeTree}, then the tree and its type is returned.
* Otherwise null and {@code type} are returned.
*
* @param tree tree to search
* @param type type to return if no parameter type tree is found
* @return if {@code tree} has a type parameter tree, then returns the tree and its type.
* Otherwise, returns null and {@code type}.
* @param type type to return if no {@code ParameterizedTypeTree} is found
* @return if {@code tree} has a {@code ParameterizedTypeTree}, then returns the tree and its
* type. Otherwise, returns null and {@code type}.
*/
private Pair<@Nullable ParameterizedTypeTree, AnnotatedDeclaredType> extractParameterizedTypeTree(
Tree tree, AnnotatedDeclaredType type) {
Expand Down Expand Up @@ -471,6 +472,14 @@ protected void visitTypeParameterBounds(
case TYPE_PARAMETER:
// Nothing to do.
break;
case METHOD:
// If a MethodTree is passed, it's just the return type that is validated.
// See BaseTypeVisitor#validateTypeOf.
MethodTree methodTree = (MethodTree) tree;
if (methodTree.getReturnType() instanceof ParameterizedTypeTree) {
typeargtree = (ParameterizedTypeTree) methodTree.getReturnType();
}
break;
default:
// The parameterized type is the result of some expression tree.
// No need to do anything further.
Expand Down

0 comments on commit 77a7c93

Please sign in to comment.