Skip to content

Commit d3e0182

Browse files
committed
Java: Enchanced isWithinType to also include lambdas, inner classes etc.
1 parent fdb60bf commit d3e0182

File tree

3 files changed

+10
-10
lines changed

3 files changed

+10
-10
lines changed

java/ql/src/Violations of Best Practice/Implementation Hiding/VisibleForTestingAbuse.ql

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,13 @@
1515
import java
1616

1717
/**
18-
* A `Callable` is within some `RefType`
18+
* A `Callable` is within some `RefType` (including through lambdas and inner classes)
1919
*/
20-
predicate isWithinType(Callable c, RefType t) { c.getDeclaringType() = t }
20+
predicate isWithinType(Callable c, RefType t) {
21+
c.getDeclaringType() = t
22+
or
23+
c.getDeclaringType().getEnclosingType*() = t
24+
}
2125

2226
/**
2327
* A `Callable` is within same package as the `RefType`

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@
66
| packageone/SourcePackage.java:26:31:26:42 | Annotated.m2 | Access of $@ annotated with VisibleForTesting found in production code. | packagetwo/Annotated.java:11:26:11:27 | m2 | element |
77
| packageone/SourcePackage.java:29:28:29:46 | fPublic(...) | Access of $@ annotated with VisibleForTesting found in production code. | packagetwo/Annotated.java:26:23:26:29 | fPublic | element |
88
| packageone/SourcePackage.java:30:28:30:49 | fProtected(...) | Access of $@ annotated with VisibleForTesting found in production code. | packagetwo/Annotated.java:31:26:31:35 | fProtected | element |
9-
| packagetwo/Annotated.java:49:31:49:31 | m | Access of $@ annotated with VisibleForTesting found in production code. | packagetwo/Annotated.java:7:19:7:19 | m | element |
10-
| packagetwo/Annotated.java:54:26:54:28 | f(...) | Access of $@ annotated with VisibleForTesting found in production code. | packagetwo/Annotated.java:16:16:16:16 | f | element |
11-
| packagetwo/Annotated.java:64:28:64:28 | m | Access of $@ annotated with VisibleForTesting found in production code. | packagetwo/Annotated.java:7:19:7:19 | m | element |
12-
| packagetwo/Annotated.java:69:26:69:28 | f(...) | Access of $@ annotated with VisibleForTesting found in production code. | packagetwo/Annotated.java:16:16:16:16 | f | element |
139
| packagetwo/Source.java:8:20:8:30 | Annotated.m | Access of $@ annotated with VisibleForTesting found in production code. | packagetwo/Annotated.java:7:19:7:19 | m | element |
1410
| packagetwo/Source.java:14:17:14:29 | f(...) | Access of $@ annotated with VisibleForTesting found in production code. | packagetwo/Annotated.java:16:16:16:16 | f | element |
1511
| packagetwo/Source.java:20:28:20:47 | new AnnotatedClass(...) | Access of $@ annotated with VisibleForTesting found in production code. | packageone/AnnotatedClass.java:4:14:4:27 | AnnotatedClass | element |

java/ql/test/query-tests/VisibleForTestingAbuse/packagetwo/Annotated.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,12 @@ private static void resetPriorities() {
4646

4747
private static void resetPriorities2() {
4848
Runnable task = () -> {
49-
String priority = m; // $ SPURIOUS: Alert
49+
String priority = m;
5050
String priority1 = m1;
5151
String priority2 = m2;
5252
String priority3 = m3;
5353

54-
int result = f(); // $ SPURIOUS: Alert
54+
int result = f();
5555
int resultPrivate = fPrivate();
5656
int resultPublic = fPublic();
5757
int resultProtected = fProtected();
@@ -61,12 +61,12 @@ private static void resetPriorities2() {
6161

6262
private static class InnerClass {
6363
void useVisibleForMembers() {
64-
String field = m; // $ SPURIOUS: Alert
64+
String field = m;
6565
String field1 = m1;
6666
String field2 = m2;
6767
String field3 = m3;
6868

69-
int method = f(); // $ SPURIOUS: Alert
69+
int method = f();
7070
int methodPrivate = fPrivate();
7171
int methodPublic = fPublic();
7272
int methodProtected = fProtected();

0 commit comments

Comments
 (0)