Java: Fix java/jvm-exit false positives for local nested classes in test methods

This commit is contained in:
Napalys Klicius
2025-08-21 14:12:54 +00:00
parent 41a78a0c3d
commit eb6e9b8fe6
3 changed files with 13 additions and 9 deletions

View File

@@ -63,7 +63,15 @@ class SourceMethodNotMainOrTest extends Method {
this.fromSource() and
not this instanceof MainMethod and
not this instanceof LikelyTestMethod and
not this.getEnclosingCallable() instanceof LikelyTestMethod
not (
this.getEnclosingCallable*() instanceof LikelyTestMethod
or
this.getDeclaringType()
.getEnclosingType*()
.(LocalClassOrInterface)
.getLocalTypeDeclStmt()
.getEnclosingCallable() instanceof LikelyTestMethod
)
}
}

View File

@@ -4,7 +4,3 @@
| ExampleRuntimeHalt.java:21:17:21:44 | halt(...) | Avoid calls to Runtime.halt() as this prevents runtime cleanup and makes code harder to reuse. |
| ExampleSystemExit.java:22:17:22:30 | exit(...) | Avoid calls to System.exit() as this prevents runtime cleanup and makes code harder to reuse. |
| ExampleSystemExit.java:25:17:25:30 | exit(...) | Avoid calls to System.exit() as this prevents runtime cleanup and makes code harder to reuse. |
| LocalClassInTestMethod.java:7:25:7:38 | exit(...) | Avoid calls to System.exit() as this prevents runtime cleanup and makes code harder to reuse. |
| LocalClassInTestMethod.java:8:25:8:52 | halt(...) | Avoid calls to Runtime.halt() as this prevents runtime cleanup and makes code harder to reuse. |
| LocalClassInTestMethod.java:20:21:20:34 | exit(...) | Avoid calls to System.exit() as this prevents runtime cleanup and makes code harder to reuse. |
| LocalClassInTestMethod.java:21:21:21:48 | halt(...) | Avoid calls to Runtime.halt() as this prevents runtime cleanup and makes code harder to reuse. |

View File

@@ -4,8 +4,8 @@ public class LocalClassInTestMethod {
void func() {
class NestedLocalClass {
void nestedMethod() {
System.exit(4); // $ SPURIOUS: Alert
Runtime.getRuntime().halt(5); // $ SPURIOUS: Alert
System.exit(4);
Runtime.getRuntime().halt(5);
}
}
}
@@ -17,8 +17,8 @@ public class LocalClassInTestMethod {
class OuterLocalClass {
class NestedLocalClass {
void nestedMethod() {
System.exit(4); // $ SPURIOUS: Alert
Runtime.getRuntime().halt(5); // $ SPURIOUS: Alert
System.exit(4);
Runtime.getRuntime().halt(5);
}
}
}