diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/nullness/ReturnMissingNullable.java b/core/src/main/java/com/google/errorprone/bugpatterns/nullness/ReturnMissingNullable.java index 1ec82ff5c26..8d92f91907c 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/nullness/ReturnMissingNullable.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/nullness/ReturnMissingNullable.java @@ -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; @@ -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 FAILS_IF_PASSED_FALSE = diff --git a/core/src/test/java/com/google/errorprone/bugpatterns/nullness/ReturnMissingNullableTest.java b/core/src/test/java/com/google/errorprone/bugpatterns/nullness/ReturnMissingNullableTest.java index e6296f36a95..f0390d5d47d 100644 --- a/core/src/test/java/com/google/errorprone/bugpatterns/nullness/ReturnMissingNullableTest.java +++ b/core/src/test/java/com/google/errorprone/bugpatterns/nullness/ReturnMissingNullableTest.java @@ -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()