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

[ContractHandler] Handle @Contract with empty value. #450

Merged
merged 3 commits into from
Feb 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public NullnessHint onDataflowVisitMethodInvocation(
Preconditions.checkNotNull(callee);
// Check to see if this method has an @Contract annotation
String contractString = NullabilityUtil.getAnnotationValue(callee, CONTRACT_ANNOTATION_NAME);
if (contractString != null) {
if (contractString != null && contractString.trim().length() > 0) {
// Found a contract, lets parse it.
String[] clauses = contractString.split(";");
for (String clause : clauses) {
Expand Down
40 changes: 40 additions & 0 deletions nullaway/src/test/java/com/uber/nullaway/NullAwayTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,46 @@ public void contractNonVarArg() {
.doTest();
}

@Test
public void contractPureOnlyIgnored() {
compilationHelper
.setArgs(
Arrays.asList(
"-d",
temporaryFolder.getRoot().getAbsolutePath(),
"-XepOpt:NullAway:AnnotatedPackages=com.uber"))
.addSourceLines(
"PureLibrary.java",
"package com.example.library;",
"import org.jetbrains.annotations.Contract;",
"public class PureLibrary {",
" @Contract(",
" pure = true",
" )",
" public static String pi() {",
" // Meh, close enough...",
" return Double.toString(3.14);",
" }",
"}")
.addSourceLines(
"Test.java",
"package com.uber;",
"import com.example.library.PureLibrary;",
"import javax.annotation.Nullable;",
"class Test {",
" String piValue() {",
" String pi = PureLibrary.pi();",
" // Note: we must trigger dataflow to ensure that",
" // ContractHandler.onDataflowVisitMethodInvocation is called",
" if (pi != null) {",
" return pi;",
" }",
" return Integer.toString(3);",
" }",
"}")
.doTest();
}

@Test
public void testEnumInit() {
compilationHelper
Expand Down