Skip to content

Commit 99cdb15

Browse files
cpovirkError Prone Team
authored and
Error Prone Team
committed
Always annotate arrays now that we place type-use annotations in the right place.
PiperOrigin-RevId: 407667606
1 parent 0fc9146 commit 99cdb15

File tree

2 files changed

+72
-97
lines changed

2 files changed

+72
-97
lines changed

core/src/main/java/com/google/errorprone/bugpatterns/nullness/ReturnMissingNullable.java

-7
Original file line numberDiff line numberDiff line change
@@ -208,13 +208,6 @@ && getOnlyElement(statements) == returnTree
208208
// Buggy code, but adding @Nullable just makes it worse.
209209
return;
210210
}
211-
if (beingConservative && state.getTypes().isArray(returnType)) {
212-
/*
213-
* Type-annotation syntax on arrays can be confusing, and this refactoring doesn't get it
214-
* right yet.
215-
*/
216-
return;
217-
}
218211
if (beingConservative && returnType.getKind() == TYPEVAR) {
219212
/*
220213
* Consider AbstractFuture.getDoneValue: It returns a literal `null`, but it shouldn't be

core/src/test/java/com/google/errorprone/bugpatterns/nullness/ReturnMissingNullableTest.java

+72-90
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,78 @@ public void testOnlyIfAlreadyInScopeAndItIs() {
433433
.doTest();
434434
}
435435

436+
@Test
437+
public void testArrayDeclaration() {
438+
createRefactoringTestHelper()
439+
.addInputLines(
440+
"in/com/google/errorprone/bugpatterns/nullness/LiteralNullReturnTest.java",
441+
"package com.google.errorprone.bugpatterns.nullness;",
442+
"import javax.annotation.Nullable;",
443+
"public class LiteralNullReturnTest {",
444+
" public String[] getMessage(boolean b) {",
445+
" return b ? null : new String[0];",
446+
" }",
447+
"}")
448+
.addOutputLines(
449+
"out/com/google/errorprone/bugpatterns/nullness/LiteralNullReturnTest.java",
450+
"package com.google.errorprone.bugpatterns.nullness;",
451+
"import javax.annotation.Nullable;",
452+
"public class LiteralNullReturnTest {",
453+
" @Nullable public String[] getMessage(boolean b) {",
454+
" return b ? null : new String[0];",
455+
" }",
456+
"}")
457+
.doTest();
458+
}
459+
460+
@Test
461+
public void testArrayTypeUse() {
462+
createRefactoringTestHelper()
463+
.addInputLines(
464+
"in/com/google/errorprone/bugpatterns/nullness/LiteralNullReturnTest.java",
465+
"package com.google.errorprone.bugpatterns.nullness;",
466+
"import org.checkerframework.checker.nullness.qual.Nullable;",
467+
"public class LiteralNullReturnTest {",
468+
" public String[] getMessage(boolean b) {",
469+
" return b ? null : new String[0];",
470+
" }",
471+
"}")
472+
.addOutputLines(
473+
"out/com/google/errorprone/bugpatterns/nullness/LiteralNullReturnTest.java",
474+
"package com.google.errorprone.bugpatterns.nullness;",
475+
"import org.checkerframework.checker.nullness.qual.Nullable;",
476+
"public class LiteralNullReturnTest {",
477+
" public String @Nullable [] getMessage(boolean b) {",
478+
" return b ? null : new String[0];",
479+
" }",
480+
"}")
481+
.doTest();
482+
}
483+
484+
@Test
485+
public void testArrayTypeUseTwoDimensional() {
486+
createRefactoringTestHelper()
487+
.addInputLines(
488+
"in/com/google/errorprone/bugpatterns/nullness/LiteralNullReturnTest.java",
489+
"package com.google.errorprone.bugpatterns.nullness;",
490+
"import org.checkerframework.checker.nullness.qual.Nullable;",
491+
"public class LiteralNullReturnTest {",
492+
" public String[][] getMessage(boolean b, String[][] s) {",
493+
" return b ? null : s;",
494+
" }",
495+
"}")
496+
.addOutputLines(
497+
"out/com/google/errorprone/bugpatterns/nullness/LiteralNullReturnTest.java",
498+
"package com.google.errorprone.bugpatterns.nullness;",
499+
"import org.checkerframework.checker.nullness.qual.Nullable;",
500+
"public class LiteralNullReturnTest {",
501+
" public String @Nullable [][] getMessage(boolean b, String[][] s) {",
502+
" return b ? null : s;",
503+
" }",
504+
"}")
505+
.doTest();
506+
}
507+
436508
@Test
437509
public void testLimitation_staticFinalFieldInitializedLater() {
438510
createCompilationTestHelper()
@@ -553,20 +625,6 @@ public void testNegativeCases_onlyStatementIsNullReturn() {
553625
.doTest();
554626
}
555627

556-
@Test
557-
public void testNegativeCases_array() {
558-
createCompilationTestHelper()
559-
.addSourceLines(
560-
"com/google/errorprone/bugpatterns/nullness/LiteralNullReturnTest.java",
561-
"package com.google.errorprone.bugpatterns.nullness;",
562-
"public class LiteralNullReturnTest {",
563-
" public String[] getMessage(boolean b) {",
564-
" return b ? null : new String[0];",
565-
" }",
566-
"}")
567-
.doTest();
568-
}
569-
570628
@Test
571629
public void testNegativeCases_typeVariableUsage() {
572630
createCompilationTestHelper()
@@ -1463,78 +1521,6 @@ public void testAggressive_onlyStatementIsNullReturn() {
14631521
.doTest();
14641522
}
14651523

1466-
@Test
1467-
public void testAggressive_arrayDeclaration() {
1468-
createAggressiveRefactoringTestHelper()
1469-
.addInputLines(
1470-
"in/com/google/errorprone/bugpatterns/nullness/LiteralNullReturnTest.java",
1471-
"package com.google.errorprone.bugpatterns.nullness;",
1472-
"import javax.annotation.Nullable;",
1473-
"public class LiteralNullReturnTest {",
1474-
" public String[] getMessage(boolean b) {",
1475-
" return b ? null : new String[0];",
1476-
" }",
1477-
"}")
1478-
.addOutputLines(
1479-
"out/com/google/errorprone/bugpatterns/nullness/LiteralNullReturnTest.java",
1480-
"package com.google.errorprone.bugpatterns.nullness;",
1481-
"import javax.annotation.Nullable;",
1482-
"public class LiteralNullReturnTest {",
1483-
" @Nullable public String[] getMessage(boolean b) {",
1484-
" return b ? null : new String[0];",
1485-
" }",
1486-
"}")
1487-
.doTest();
1488-
}
1489-
1490-
@Test
1491-
public void testAggressive_arrayTypeUse() {
1492-
createAggressiveRefactoringTestHelper()
1493-
.addInputLines(
1494-
"in/com/google/errorprone/bugpatterns/nullness/LiteralNullReturnTest.java",
1495-
"package com.google.errorprone.bugpatterns.nullness;",
1496-
"import org.checkerframework.checker.nullness.qual.Nullable;",
1497-
"public class LiteralNullReturnTest {",
1498-
" public String[] getMessage(boolean b) {",
1499-
" return b ? null : new String[0];",
1500-
" }",
1501-
"}")
1502-
.addOutputLines(
1503-
"out/com/google/errorprone/bugpatterns/nullness/LiteralNullReturnTest.java",
1504-
"package com.google.errorprone.bugpatterns.nullness;",
1505-
"import org.checkerframework.checker.nullness.qual.Nullable;",
1506-
"public class LiteralNullReturnTest {",
1507-
" public String @Nullable [] getMessage(boolean b) {",
1508-
" return b ? null : new String[0];",
1509-
" }",
1510-
"}")
1511-
.doTest();
1512-
}
1513-
1514-
@Test
1515-
public void testAggressive_arrayTypeUseTwoDimensional() {
1516-
createAggressiveRefactoringTestHelper()
1517-
.addInputLines(
1518-
"in/com/google/errorprone/bugpatterns/nullness/LiteralNullReturnTest.java",
1519-
"package com.google.errorprone.bugpatterns.nullness;",
1520-
"import org.checkerframework.checker.nullness.qual.Nullable;",
1521-
"public class LiteralNullReturnTest {",
1522-
" public String[][] getMessage(boolean b, String[][] s) {",
1523-
" return b ? null : s;",
1524-
" }",
1525-
"}")
1526-
.addOutputLines(
1527-
"out/com/google/errorprone/bugpatterns/nullness/LiteralNullReturnTest.java",
1528-
"package com.google.errorprone.bugpatterns.nullness;",
1529-
"import org.checkerframework.checker.nullness.qual.Nullable;",
1530-
"public class LiteralNullReturnTest {",
1531-
" public String @Nullable [][] getMessage(boolean b, String[][] s) {",
1532-
" return b ? null : s;",
1533-
" }",
1534-
"}")
1535-
.doTest();
1536-
}
1537-
15381524
@Test
15391525
public void testAggressive_typeVariableUsage() {
15401526
createAggressiveCompilationTestHelper()
@@ -1579,8 +1565,4 @@ private CompilationTestHelper createAggressiveCompilationTestHelper() {
15791565
private BugCheckerRefactoringTestHelper createRefactoringTestHelper() {
15801566
return BugCheckerRefactoringTestHelper.newInstance(ReturnMissingNullable.class, getClass());
15811567
}
1582-
1583-
private BugCheckerRefactoringTestHelper createAggressiveRefactoringTestHelper() {
1584-
return createRefactoringTestHelper().setArgs("-XepOpt:Nullness:Conservative=false");
1585-
}
15861568
}

0 commit comments

Comments
 (0)