Skip to content

Commit

Permalink
Always annotate arrays now that we place type-use annotations in the …
Browse files Browse the repository at this point in the history
…right place.

PiperOrigin-RevId: 407667606
  • Loading branch information
cpovirk authored and Error Prone Team committed Nov 4, 2021
1 parent 0fc9146 commit 99cdb15
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 97 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -208,13 +208,6 @@ && getOnlyElement(statements) == returnTree
// Buggy code, but adding @Nullable just makes it worse.
return;
}
if (beingConservative && state.getTypes().isArray(returnType)) {
/*
* Type-annotation syntax on arrays can be confusing, and this refactoring doesn't get it
* right yet.
*/
return;
}
if (beingConservative && returnType.getKind() == TYPEVAR) {
/*
* Consider AbstractFuture.getDoneValue: It returns a literal `null`, but it shouldn't be
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,78 @@ public void testOnlyIfAlreadyInScopeAndItIs() {
.doTest();
}

@Test
public void testArrayDeclaration() {
createRefactoringTestHelper()
.addInputLines(
"in/com/google/errorprone/bugpatterns/nullness/LiteralNullReturnTest.java",
"package com.google.errorprone.bugpatterns.nullness;",
"import javax.annotation.Nullable;",
"public class LiteralNullReturnTest {",
" public String[] getMessage(boolean b) {",
" return b ? null : new String[0];",
" }",
"}")
.addOutputLines(
"out/com/google/errorprone/bugpatterns/nullness/LiteralNullReturnTest.java",
"package com.google.errorprone.bugpatterns.nullness;",
"import javax.annotation.Nullable;",
"public class LiteralNullReturnTest {",
" @Nullable public String[] getMessage(boolean b) {",
" return b ? null : new String[0];",
" }",
"}")
.doTest();
}

@Test
public void testArrayTypeUse() {
createRefactoringTestHelper()
.addInputLines(
"in/com/google/errorprone/bugpatterns/nullness/LiteralNullReturnTest.java",
"package com.google.errorprone.bugpatterns.nullness;",
"import org.checkerframework.checker.nullness.qual.Nullable;",
"public class LiteralNullReturnTest {",
" public String[] getMessage(boolean b) {",
" return b ? null : new String[0];",
" }",
"}")
.addOutputLines(
"out/com/google/errorprone/bugpatterns/nullness/LiteralNullReturnTest.java",
"package com.google.errorprone.bugpatterns.nullness;",
"import org.checkerframework.checker.nullness.qual.Nullable;",
"public class LiteralNullReturnTest {",
" public String @Nullable [] getMessage(boolean b) {",
" return b ? null : new String[0];",
" }",
"}")
.doTest();
}

@Test
public void testArrayTypeUseTwoDimensional() {
createRefactoringTestHelper()
.addInputLines(
"in/com/google/errorprone/bugpatterns/nullness/LiteralNullReturnTest.java",
"package com.google.errorprone.bugpatterns.nullness;",
"import org.checkerframework.checker.nullness.qual.Nullable;",
"public class LiteralNullReturnTest {",
" public String[][] getMessage(boolean b, String[][] s) {",
" return b ? null : s;",
" }",
"}")
.addOutputLines(
"out/com/google/errorprone/bugpatterns/nullness/LiteralNullReturnTest.java",
"package com.google.errorprone.bugpatterns.nullness;",
"import org.checkerframework.checker.nullness.qual.Nullable;",
"public class LiteralNullReturnTest {",
" public String @Nullable [][] getMessage(boolean b, String[][] s) {",
" return b ? null : s;",
" }",
"}")
.doTest();
}

@Test
public void testLimitation_staticFinalFieldInitializedLater() {
createCompilationTestHelper()
Expand Down Expand Up @@ -553,20 +625,6 @@ public void testNegativeCases_onlyStatementIsNullReturn() {
.doTest();
}

@Test
public void testNegativeCases_array() {
createCompilationTestHelper()
.addSourceLines(
"com/google/errorprone/bugpatterns/nullness/LiteralNullReturnTest.java",
"package com.google.errorprone.bugpatterns.nullness;",
"public class LiteralNullReturnTest {",
" public String[] getMessage(boolean b) {",
" return b ? null : new String[0];",
" }",
"}")
.doTest();
}

@Test
public void testNegativeCases_typeVariableUsage() {
createCompilationTestHelper()
Expand Down Expand Up @@ -1463,78 +1521,6 @@ public void testAggressive_onlyStatementIsNullReturn() {
.doTest();
}

@Test
public void testAggressive_arrayDeclaration() {
createAggressiveRefactoringTestHelper()
.addInputLines(
"in/com/google/errorprone/bugpatterns/nullness/LiteralNullReturnTest.java",
"package com.google.errorprone.bugpatterns.nullness;",
"import javax.annotation.Nullable;",
"public class LiteralNullReturnTest {",
" public String[] getMessage(boolean b) {",
" return b ? null : new String[0];",
" }",
"}")
.addOutputLines(
"out/com/google/errorprone/bugpatterns/nullness/LiteralNullReturnTest.java",
"package com.google.errorprone.bugpatterns.nullness;",
"import javax.annotation.Nullable;",
"public class LiteralNullReturnTest {",
" @Nullable public String[] getMessage(boolean b) {",
" return b ? null : new String[0];",
" }",
"}")
.doTest();
}

@Test
public void testAggressive_arrayTypeUse() {
createAggressiveRefactoringTestHelper()
.addInputLines(
"in/com/google/errorprone/bugpatterns/nullness/LiteralNullReturnTest.java",
"package com.google.errorprone.bugpatterns.nullness;",
"import org.checkerframework.checker.nullness.qual.Nullable;",
"public class LiteralNullReturnTest {",
" public String[] getMessage(boolean b) {",
" return b ? null : new String[0];",
" }",
"}")
.addOutputLines(
"out/com/google/errorprone/bugpatterns/nullness/LiteralNullReturnTest.java",
"package com.google.errorprone.bugpatterns.nullness;",
"import org.checkerframework.checker.nullness.qual.Nullable;",
"public class LiteralNullReturnTest {",
" public String @Nullable [] getMessage(boolean b) {",
" return b ? null : new String[0];",
" }",
"}")
.doTest();
}

@Test
public void testAggressive_arrayTypeUseTwoDimensional() {
createAggressiveRefactoringTestHelper()
.addInputLines(
"in/com/google/errorprone/bugpatterns/nullness/LiteralNullReturnTest.java",
"package com.google.errorprone.bugpatterns.nullness;",
"import org.checkerframework.checker.nullness.qual.Nullable;",
"public class LiteralNullReturnTest {",
" public String[][] getMessage(boolean b, String[][] s) {",
" return b ? null : s;",
" }",
"}")
.addOutputLines(
"out/com/google/errorprone/bugpatterns/nullness/LiteralNullReturnTest.java",
"package com.google.errorprone.bugpatterns.nullness;",
"import org.checkerframework.checker.nullness.qual.Nullable;",
"public class LiteralNullReturnTest {",
" public String @Nullable [][] getMessage(boolean b, String[][] s) {",
" return b ? null : s;",
" }",
"}")
.doTest();
}

@Test
public void testAggressive_typeVariableUsage() {
createAggressiveCompilationTestHelper()
Expand Down Expand Up @@ -1579,8 +1565,4 @@ private CompilationTestHelper createAggressiveCompilationTestHelper() {
private BugCheckerRefactoringTestHelper createRefactoringTestHelper() {
return BugCheckerRefactoringTestHelper.newInstance(ReturnMissingNullable.class, getClass());
}

private BugCheckerRefactoringTestHelper createAggressiveRefactoringTestHelper() {
return createRefactoringTestHelper().setArgs("-XepOpt:Nullness:Conservative=false");
}
}

0 comments on commit 99cdb15

Please sign in to comment.