Skip to content

Commit cce3365

Browse files
committed
Java: Resolve spurious VisibleForTestingAbuse alerts for inner class access patterns
1 parent 9c5a794 commit cce3365

File tree

3 files changed

+8
-6
lines changed

3 files changed

+8
-6
lines changed

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,15 @@
1515
import java
1616

1717
/**
18-
* Holds if a `Callable` is within some `RefType` (including through lambdas and inner classes)
18+
* Holds if a `Callable` is within the same type hierarchy as `RefType`
19+
* (including through lambdas, inner classes, and outer classes)
1920
*/
2021
predicate isWithinType(Callable c, RefType t) {
21-
c.getDeclaringType() = t
22-
or
23-
c.getDeclaringType().getEnclosingType*() = t
22+
// Either the callable is in the target type, or they share a common enclosing type
23+
exists(RefType commonType |
24+
(c.getDeclaringType() = commonType or c.getDeclaringType().getEnclosingType*() = commonType) and
25+
(t = commonType or t.getEnclosingType*() = commonType)
26+
)
2427
}
2528

2629
/**

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
| 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 |
1515
| 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 |
1616
| 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 |
17-
| packagetwo/Annotated.java:89:20:89:34 | getSize(...) | Access of $@ annotated with VisibleForTesting found in production code. | packagetwo/Annotated.java:79:13:79:19 | getSize | element |
1817
| 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 |
1918
| 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 |
2019
| 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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ int getSize() {
8686

8787
private void useInnerClass() {
8888
InnerTestClass inner = new InnerTestClass();
89-
int size = inner.getSize(); // $ SPURIOUS: Alert
89+
int size = inner.getSize();
9090
String value = inner.data;
9191
}
9292
}

0 commit comments

Comments
 (0)