Skip to content

Commit 681e568

Browse files
committed
Java: Fix java/jvm-exit false positives for local nested classes in test methods
1 parent 41a78a0 commit 681e568

File tree

3 files changed

+12
-9
lines changed

3 files changed

+12
-9
lines changed

java/ql/src/Violations of Best Practice/Undesirable Calls/CallsToSystemExit.ql

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,14 @@ class SourceMethodNotMainOrTest extends Method {
6363
this.fromSource() and
6464
not this instanceof MainMethod and
6565
not this instanceof LikelyTestMethod and
66-
not this.getEnclosingCallable() instanceof LikelyTestMethod
66+
not (
67+
this.getEnclosingCallable*() instanceof LikelyTestMethod
68+
or
69+
exists(LikelyTestMethod testMethod |
70+
this.getDeclaringType().getEnclosingType*().(LocalClassOrInterface)
71+
.getLocalTypeDeclStmt().getEnclosingCallable() = testMethod
72+
)
73+
)
6774
}
6875
}
6976

java/ql/test/query-tests/CallsToSystemExit/CallsToSystemExit.expected

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,3 @@
44
| ExampleRuntimeHalt.java:21:17:21:44 | halt(...) | Avoid calls to Runtime.halt() as this prevents runtime cleanup and makes code harder to reuse. |
55
| ExampleSystemExit.java:22:17:22:30 | exit(...) | Avoid calls to System.exit() as this prevents runtime cleanup and makes code harder to reuse. |
66
| ExampleSystemExit.java:25:17:25:30 | exit(...) | Avoid calls to System.exit() as this prevents runtime cleanup and makes code harder to reuse. |
7-
| LocalClassInTestMethod.java:7:25:7:38 | exit(...) | Avoid calls to System.exit() as this prevents runtime cleanup and makes code harder to reuse. |
8-
| LocalClassInTestMethod.java:8:25:8:52 | halt(...) | Avoid calls to Runtime.halt() as this prevents runtime cleanup and makes code harder to reuse. |
9-
| LocalClassInTestMethod.java:20:21:20:34 | exit(...) | Avoid calls to System.exit() as this prevents runtime cleanup and makes code harder to reuse. |
10-
| LocalClassInTestMethod.java:21:21:21:48 | halt(...) | Avoid calls to Runtime.halt() as this prevents runtime cleanup and makes code harder to reuse. |

java/ql/test/query-tests/CallsToSystemExit/LocalClassInTestMethod.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ class OuterLocalClass {
44
void func() {
55
class NestedLocalClass {
66
void nestedMethod() {
7-
System.exit(4); // $ SPURIOUS: Alert
8-
Runtime.getRuntime().halt(5); // $ SPURIOUS: Alert
7+
System.exit(4);
8+
Runtime.getRuntime().halt(5);
99
}
1010
}
1111
}
@@ -17,8 +17,8 @@ public void testNestedCase2() {
1717
class OuterLocalClass {
1818
class NestedLocalClass {
1919
void nestedMethod() {
20-
System.exit(4); // $ SPURIOUS: Alert
21-
Runtime.getRuntime().halt(5); // $ SPURIOUS: Alert
20+
System.exit(4);
21+
Runtime.getRuntime().halt(5);
2222
}
2323
}
2424
}

0 commit comments

Comments
 (0)