Skip to content

Commit

Permalink
Model Class.cast (#731)
Browse files Browse the repository at this point in the history
also call `getOptLibraryModels` only once
  • Loading branch information
XN137 committed Feb 6, 2023
1 parent ed0ef72 commit de74310
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,9 @@ public NullnessHint onDataflowVisitMethodInvocation(
setUnconditionalArgumentNullness(bothUpdates, node.getArguments(), callee, state, apContext);
setConditionalArgumentNullness(
thenUpdates, elseUpdates, node.getArguments(), callee, state, apContext);
OptimizedLibraryModels optLibraryModels = getOptLibraryModels(state.context);
ImmutableSet<Integer> nullImpliesNullIndexes =
getOptLibraryModels(state.context).nullImpliesNullParameters(callee);
optLibraryModels.nullImpliesNullParameters(callee);
if (!nullImpliesNullIndexes.isEmpty()) {
// If the method is marked as having argument dependent nullability and any of the
// corresponding arguments is null, then the return is nullable. If the method is
Expand All @@ -212,10 +213,9 @@ public NullnessHint onDataflowVisitMethodInvocation(
return anyNull ? NullnessHint.HINT_NULLABLE : NullnessHint.FORCE_NONNULL;
}
Types types = state.getTypes();
if (getOptLibraryModels(state.context).hasNonNullReturn(callee, types, !isMethodAnnotated)) {
if (optLibraryModels.hasNonNullReturn(callee, types, !isMethodAnnotated)) {
return NullnessHint.FORCE_NONNULL;
} else if (getOptLibraryModels(state.context)
.hasNullableReturn(callee, types, !isMethodAnnotated)) {
} else if (optLibraryModels.hasNullableReturn(callee, types, !isMethodAnnotated)) {
return NullnessHint.HINT_NULLABLE;
} else {
return NullnessHint.UNKNOWN;
Expand Down Expand Up @@ -612,6 +612,7 @@ private static class DefaultLibraryModels implements LibraryModels {

private static final ImmutableSetMultimap<MethodRef, Integer> NULL_IMPLIES_NULL_PARAMETERS =
new ImmutableSetMultimap.Builder<MethodRef, Integer>()
.put(methodRef("java.lang.Class", "cast(java.lang.Object)"), 0)
.put(methodRef("java.util.Optional", "orElse(T)"), 0)
.put(methodRef("com.google.common.io.Closer", "<C>register(C)"), 0)
.put(methodRef("java.util.Map", "getOrDefault(java.lang.Object,V)"), 1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -411,4 +411,26 @@ public void mapGetOrDefault() {
.addSourceLines("Test.java", sourceLines)
.doTest();
}

@Test
public void defaultLibraryModelsClassCast() {
defaultCompilationHelper
.addSourceLines(
"Test.java",
"package com.uber;",
"import org.jspecify.annotations.Nullable;",
"class Test {",
" void castNullable(@Nullable String s) {",
" // BUG: Diagnostic contains: dereferenced",
" CharSequence.class.cast(s).hashCode();",
" }",
" void castNonnull(String s1, @Nullable String s2) {",
" CharSequence.class.cast(s1).hashCode();",
" if (s2 instanceof CharSequence) {",
" CharSequence.class.cast(s2).hashCode();",
" }",
" }",
"}")
.doTest();
}
}

0 comments on commit de74310

Please sign in to comment.