-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(S1068): delete field writes as well (#917)
* Introduce failing test cases * Remove field writes if they are not read * Indicate noncompliance
- Loading branch information
1 parent
a0fbddc
commit c67cbd8
Showing
3 changed files
with
36 additions
and
0 deletions.
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
sorald/src/main/java/sorald/processor/UnusedPrivateFieldProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,29 @@ | ||
package sorald.processor; | ||
|
||
import java.util.List; | ||
import sorald.annotations.ProcessorAnnotation; | ||
import spoon.reflect.code.CtFieldWrite; | ||
import spoon.reflect.code.CtStatement; | ||
import spoon.reflect.declaration.CtClass; | ||
import spoon.reflect.declaration.CtField; | ||
import spoon.reflect.visitor.filter.TypeFilter; | ||
|
||
@ProcessorAnnotation(key = "S1068", description = "Unused \"private\" fields should be removed") | ||
public class UnusedPrivateFieldProcessor extends SoraldAbstractProcessor<CtField<?>> { | ||
@Override | ||
protected void repairInternal(CtField<?> element) { | ||
removeAllUnusedPrivateFieldWrites(element); | ||
element.delete(); | ||
} | ||
|
||
/** Removes all writes to the given field that are not read anywhere. */ | ||
private static void removeAllUnusedPrivateFieldWrites(CtField<?> field) { | ||
CtClass<?> parentClass = field.getParent(CtClass.class); | ||
List<CtFieldWrite<?>> unusedFieldWrites = | ||
parentClass.filterChildren(new TypeFilter<>(CtFieldWrite.class)).list(); | ||
for (CtFieldWrite<?> unusedFieldWrite : unusedFieldWrites) { | ||
CtStatement statement = unusedFieldWrite.getParent(CtStatement.class); | ||
statement.delete(); | ||
} | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...esources/processor_test_files/S1068_UnusedPrivateField/PrivateFieldWrittenButNotRead.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
public class PrivateFieldWrittenButNotRead { | ||
private int x; // Noncompliant | ||
|
||
private String foo; // Noncompliant | ||
|
||
public PrivateFieldWrittenButNotRead(int x, int y) { | ||
this.x = x + y; | ||
} | ||
|
||
public void initialize(String bar) { | ||
foo = bar; | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
...processor_test_files/S1068_UnusedPrivateField/PrivateFieldWrittenButNotRead.java.expected
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
public class PrivateFieldWrittenButNotRead { | ||
|
||
public PrivateFieldWrittenButNotRead(int x, int y) { } | ||
|
||
public void initialize(String bar) { } | ||
} |