-
Notifications
You must be signed in to change notification settings - Fork 300
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add flag to indicate only @NullMarked code should be checked (#1117)
Fixes #574 We add a new flag `OnlyNullMarked` to indicate that NullAway can only check code that is `@NullMarked`, and hence it is ok to omit the (previously required) `AnnotatedPackages` flag. We currently require _exactly_ one of either `AnnotatedPackages` or `OnlyNullMarked` to be passed in; if both flags are omitted or both flags are passed, we fail. As JSpecify and `@NullMarked` become more widely adopted, we may change the policy to allow neither flag to be passed (as NullAway already checks `@NullMarked` code out of the box). But for now, we require one of the flags to be passed, to avoid confusion.
- Loading branch information
Showing
4 changed files
with
88 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
nullaway/src/test/java/com/uber/nullaway/ErrorProneCLIFlagsConfigTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package com.uber.nullaway; | ||
|
||
import static com.uber.nullaway.ErrorProneCLIFlagsConfig.ANNOTATED_PACKAGES_ONLY_NULLMARKED_ERROR_MSG; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import com.google.errorprone.CompilationTestHelper; | ||
import java.util.List; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.JUnit4; | ||
|
||
@RunWith(JUnit4.class) | ||
public class ErrorProneCLIFlagsConfigTest extends NullAwayTestsBase { | ||
|
||
@Test | ||
public void noFlagsFails() { | ||
CompilationTestHelper compilationTestHelper = | ||
makeTestHelperWithArgs(List.of()) | ||
.addSourceLines("Stub.java", "package com.uber; class Stub {}"); | ||
AssertionError e = assertThrows(AssertionError.class, () -> compilationTestHelper.doTest()); | ||
assertTrue(e.getMessage().contains(ANNOTATED_PACKAGES_ONLY_NULLMARKED_ERROR_MSG)); | ||
} | ||
|
||
@Test | ||
public void onlyNullMarkedOk() { | ||
makeTestHelperWithArgs(List.of("-XepOpt:NullAway:OnlyNullMarked")) | ||
.addSourceLines( | ||
"Test.java", | ||
"package foo.baz;", | ||
"import org.jspecify.annotations.NullMarked;", | ||
"@NullMarked", | ||
"class Marked {", | ||
" // BUG: Diagnostic contains: @NonNull field uninit not initialized", | ||
" Object uninit;", | ||
"}") | ||
.doTest(); | ||
} | ||
|
||
@Test | ||
public void onlyNullMarkedFalseFails() { | ||
CompilationTestHelper compilationTestHelper = | ||
makeTestHelperWithArgs(List.of("-XepOpt:NullAway:OnlyNullMarked=false")) | ||
.addSourceLines("Stub.java", "package com.uber; class Stub {}"); | ||
AssertionError e = assertThrows(AssertionError.class, () -> compilationTestHelper.doTest()); | ||
assertTrue(e.getMessage().contains(ANNOTATED_PACKAGES_ONLY_NULLMARKED_ERROR_MSG)); | ||
} | ||
|
||
@Test | ||
public void bothAnnotatedPackagesAndOnlyNullMarkedFails() { | ||
CompilationTestHelper compilationTestHelper = | ||
makeTestHelperWithArgs( | ||
List.of( | ||
"-XepOpt:NullAway:OnlyNullMarked", | ||
"-XepOpt:NullAway:AnnotatedPackages=com.uber")) | ||
.addSourceLines("Stub.java", "package com.uber; class Stub {}"); | ||
AssertionError e = assertThrows(AssertionError.class, () -> compilationTestHelper.doTest()); | ||
assertTrue(e.getMessage().contains(ANNOTATED_PACKAGES_ONLY_NULLMARKED_ERROR_MSG)); | ||
} | ||
} |