Skip to content

Commit

Permalink
fix: look for cross-referenced fields in Java
Browse files Browse the repository at this point in the history
  • Loading branch information
morgante committed Jun 7, 2024
1 parent 21cb2b9 commit df22205
Showing 1 changed file with 32 additions and 2 deletions.
34 changes: 32 additions & 2 deletions .grit/patterns/java/unused_private_fields.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@ tags: [java]

Unused private fields constitute dead code and should therefore be removed.


```grit
language java
class_body($declarations) where {
$declarations <: contains bubble($declarations) {
field_declaration($modifiers) as $field where {
$field <: contains variable_declarator($name) where {
$declarations <: not contains $name until field_declaration(),
$declarations <: not contains $name until $field,
$name <: not or {
`serialVersionUID`,
`serialPersistentFields`,
Expand Down Expand Up @@ -81,3 +80,34 @@ public class MyClass {
private native static void doSomethingNative();
}
```

## Does not remove fields used in other fields

```java
public class Test {

private static final String HELLO = "Hello, ";
private static final String WORLD = "World";

private static final String HELLO_WORLD = HELLO + WORLD;
private static final String REMOVE_THIS = "Remove this";

public static void main(String[] args) {
System.out.println(HELLO_WORLD);
}
}
```

```java
public class Test {

private static final String HELLO = "Hello, ";
private static final String WORLD = "World";

private static final String HELLO_WORLD = HELLO + WORLD;

public static void main(String[] args) {
System.out.println(HELLO_WORLD);
}
}
```

0 comments on commit df22205

Please sign in to comment.