Skip to content

Commit

Permalink
Recognize that Runtime.halt and exit never return.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 686917388
  • Loading branch information
cpovirk authored and Error Prone Team committed Oct 17, 2024
1 parent 1d04094 commit 82a2168
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import static com.google.errorprone.matchers.Matchers.anyMethod;
import static com.google.errorprone.matchers.Matchers.anyOf;
import static com.google.errorprone.matchers.Matchers.expressionStatement;
import static com.google.errorprone.matchers.Matchers.instanceMethod;
import static com.google.errorprone.matchers.Matchers.staticMethod;
import static com.google.errorprone.util.ASTHelpers.constValue;
import static com.google.errorprone.util.ASTHelpers.findEnclosingMethod;
Expand Down Expand Up @@ -107,6 +108,7 @@ public class ReturnMissingNullable extends BugChecker implements CompilationUnit
*/
.onDescendantOfAny("org.junit.Assert", "junit.framework.Assert")
.named("fail"),
instanceMethod().onDescendantOf("java.lang.Runtime").namedAnyOf("exit", "halt"),
staticMethod().onClass("java.lang.System").named("exit")));

private static final Matcher<StatementTree> FAILS_IF_PASSED_FALSE =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1498,6 +1498,40 @@ public String getMessage() {
.doTest();
}

@Test
public void negativeCases_unreachableRuntimeExit() {
createCompilationTestHelper()
.addSourceLines(
"com/google/errorprone/bugpatterns/nullness/LiteralNullReturnTest.java",
"""
package com.google.errorprone.bugpatterns.nullness;
class LiteralNullReturnTest {
public String getMessage() {
Runtime.getRuntime().exit(1);
return null;
}
}
""")
.doTest();
}

@Test
public void negativeCases_unreachableRuntimeHalt() {
createCompilationTestHelper()
.addSourceLines(
"com/google/errorprone/bugpatterns/nullness/LiteralNullReturnTest.java",
"""
package com.google.errorprone.bugpatterns.nullness;
class LiteralNullReturnTest {
public String getMessage() {
Runtime.getRuntime().halt(1);
return null;
}
}
""")
.doTest();
}

@Test
public void negativeCases_unreachableAssertFail() {
createCompilationTestHelper()
Expand Down

0 comments on commit 82a2168

Please sign in to comment.