-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
203 additions
and
15 deletions.
There are no files selected for viewing
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
72 changes: 72 additions & 0 deletions
72
...-core/src/main/java/de/firemage/autograder/core/check/complexity/RedundantAssignment.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,72 @@ | ||
package de.firemage.autograder.core.check.complexity; | ||
|
||
import de.firemage.autograder.core.LocalizedMessage; | ||
import de.firemage.autograder.core.ProblemType; | ||
import de.firemage.autograder.core.check.ExecutableCheck; | ||
import de.firemage.autograder.core.check.unnecessary.UnusedCodeElementCheck; | ||
import de.firemage.autograder.core.dynamic.DynamicAnalysis; | ||
import de.firemage.autograder.core.integrated.IntegratedCheck; | ||
import de.firemage.autograder.core.integrated.SpoonUtil; | ||
import de.firemage.autograder.core.integrated.StaticAnalysis; | ||
import spoon.processing.AbstractProcessor; | ||
import spoon.reflect.code.CtAssignment; | ||
import spoon.reflect.code.CtLocalVariable; | ||
import spoon.reflect.code.CtLoop; | ||
import spoon.reflect.code.CtStatement; | ||
import spoon.reflect.code.CtStatementList; | ||
import spoon.reflect.code.CtVariableRead; | ||
import spoon.reflect.code.CtVariableWrite; | ||
import spoon.reflect.declaration.CtMethod; | ||
import spoon.reflect.reference.CtLocalVariableReference; | ||
|
||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
@ExecutableCheck(reportedProblems = { ProblemType.REDUNDANT_ASSIGNMENT }) | ||
public class RedundantAssignment extends IntegratedCheck { | ||
@Override | ||
protected void check(StaticAnalysis staticAnalysis, DynamicAnalysis dynamicAnalysis) { | ||
staticAnalysis.processWith(new AbstractProcessor<CtAssignment<?, ?>>() { | ||
@Override | ||
public void process(CtAssignment<?, ?> ctAssignment) { | ||
if (ctAssignment.isImplicit() | ||
|| !ctAssignment.getPosition().isValidPosition() | ||
|| !(ctAssignment.getAssigned() instanceof CtVariableWrite<?> ctVariableWrite) | ||
|| !(ctVariableWrite.getVariable() instanceof CtLocalVariableReference<?> ctLocalVariableReference)) { | ||
return; | ||
} | ||
|
||
if (!(ctAssignment.getParent() instanceof CtStatementList) | ||
|| ctLocalVariableReference.getDeclaration() == null | ||
|| !(ctAssignment.getParent().getParent() instanceof CtMethod<?>)) { | ||
return; | ||
} | ||
|
||
List<CtStatement> followingStatements = SpoonUtil.getNextStatements(ctAssignment); | ||
|
||
CtLocalVariable<?> ctLocalVariable = ctLocalVariableReference.getDeclaration(); | ||
|
||
if (UnusedCodeElementCheck.isUnused(ctLocalVariable, true)) { | ||
return; | ||
} | ||
|
||
|
||
if (followingStatements.stream().noneMatch(statement -> SpoonUtil.hasAnyUsesIn( | ||
ctLocalVariable, | ||
statement, | ||
element -> element instanceof CtVariableRead<?> | ||
))) { | ||
addLocalProblem( | ||
ctAssignment, | ||
new LocalizedMessage( | ||
"redundant-assignment", | ||
Map.of("variable", ctLocalVariable.getSimpleName()) | ||
), | ||
ProblemType.REDUNDANT_ASSIGNMENT | ||
); | ||
} | ||
} | ||
}); | ||
} | ||
} |
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
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
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
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
95 changes: 95 additions & 0 deletions
95
...e/src/test/java/de/firemage/autograder/core/check/complexity/TestRedundantAssignment.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,95 @@ | ||
package de.firemage.autograder.core.check.complexity; | ||
|
||
import de.firemage.autograder.core.LinterException; | ||
import de.firemage.autograder.core.LocalizedMessage; | ||
import de.firemage.autograder.core.Problem; | ||
import de.firemage.autograder.core.ProblemType; | ||
import de.firemage.autograder.core.check.AbstractCheckTest; | ||
import de.firemage.autograder.core.compiler.JavaVersion; | ||
import de.firemage.autograder.core.file.StringSourceInfo; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
class TestRedundantAssignment extends AbstractCheckTest { | ||
private static final List<ProblemType> PROBLEM_TYPES = List.of(ProblemType.REDUNDANT_ASSIGNMENT); | ||
|
||
void assertEqualsRedundant(Problem problem, String variable) { | ||
assertEquals( | ||
this.linter.translateMessage(new LocalizedMessage( | ||
"redundant-assignment", | ||
Map.of("variable", variable) | ||
)), | ||
this.linter.translateMessage(problem.getExplanation()) | ||
); | ||
} | ||
|
||
@Test | ||
void testMotivation() throws IOException, LinterException { | ||
ProblemIterator problems = this.checkIterator(StringSourceInfo.fromSourceString( | ||
JavaVersion.JAVA_17, | ||
"Test", | ||
""" | ||
public class Test { | ||
public void test() { | ||
int a = 5; | ||
System.out.println(a); | ||
a = 3; | ||
} | ||
} | ||
""" | ||
), PROBLEM_TYPES); | ||
|
||
|
||
assertEqualsRedundant(problems.next(), "a"); | ||
|
||
problems.assertExhausted(); | ||
} | ||
|
||
@Test | ||
void testInLoop() throws IOException, LinterException { | ||
ProblemIterator problems = this.checkIterator(StringSourceInfo.fromSourceString( | ||
JavaVersion.JAVA_17, | ||
"Test", | ||
""" | ||
public class Test { | ||
public void test() { | ||
int a = 5; | ||
for (int i = 0; i < 5; i++) { | ||
System.out.println(a); | ||
a = i * 2; | ||
} | ||
} | ||
} | ||
""" | ||
), PROBLEM_TYPES); | ||
|
||
problems.assertExhausted(); | ||
} | ||
|
||
@Test | ||
void testAllowedAssignment() throws IOException, LinterException { | ||
ProblemIterator problems = this.checkIterator(StringSourceInfo.fromSourceString( | ||
JavaVersion.JAVA_17, | ||
"Test", | ||
""" | ||
public class Test { | ||
public void test() { | ||
int a = 5; | ||
a = 3; | ||
System.out.println(a); | ||
} | ||
} | ||
""" | ||
), PROBLEM_TYPES); | ||
|
||
problems.assertExhausted(); | ||
} | ||
} |
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 |
---|---|---|
|
@@ -156,3 +156,4 @@ | |
- COLLECTION_ADD_ALL | ||
- USE_MODULO_OPERATOR | ||
- NUMBER_FORMAT_EXCEPTION_IGNORED | ||
- REDUNDANT_ASSIGNMENT |