Skip to content

Commit 59e22f6

Browse files
authored
Merge pull request #17101 from owen-mc/java/dead-ref-types-junit-4-5
Java: Fix FPs in `java/unused-reference-type` for JUnit 4-style tests
2 parents d75da82 + 1cb5f35 commit 59e22f6

File tree

8 files changed

+17
-0
lines changed

8 files changed

+17
-0
lines changed

java/ql/src/Violations of Best Practice/Dead Code/DeadRefTypes.ql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ predicate dead(RefType dead) {
4949
not dead instanceof AnonymousClass and
5050
// Exclude classes that look like they may be reflectively constructed.
5151
not dead.getAnAnnotation() instanceof ReflectiveAccessAnnotation and
52+
not dead.getAMethod().getAnAnnotation() instanceof ReflectiveAccessAnnotation and
5253
// Insist all source ancestors are dead as well.
5354
forall(RefType t | t.fromSource() and t = getASuperTypePlus(dead) | dead(t)) and
5455
// Exclude compiler generated classes (e.g. declaring type of adapter functions in Kotlin)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: minorAnalysis
3+
---
4+
* The query "Unused classes and interfaces" (`java/unused-reference-type`) now recognizes that if a method of a class has an annotation then it may be accessed reflectively. This should remove false positive alerts, especially for JUnit 4-style tests annotated with `@test`.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class ClassWithAnnotatedMethod {
2+
@MyAnnotation
3+
void doNothing() { }
4+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
@MyAnnotation
2+
class ClassWithAnnotation {
3+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
| UnusedClass.java:1:7:1:17 | UnusedClass | Unused class: UnusedClass is not referenced within this codebase. If not used as an external API it should be removed. |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Violations of Best Practice/Dead Code/DeadRefTypes.ql
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
public @interface MyAnnotation {
2+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
class UnusedClass {}

0 commit comments

Comments
 (0)