Problem
The variable currentFacts is first assigned from a generic constructor, and then later overwritten by a method invocation in a loop. That second assignment doesn't work anymore when the type is switched from an explicit Set to an implied HashSet through the use of var.
Expected behavior
We should avoid making changes to variables that are later reassigned, if the original type differs from the initializer.
Example diff
From: src/test/java/org/openrewrite/analysis/dataflow/ForwardDataFlowAnalysisTest.java
@Override
protected Set<ConstantFact> transfer(BasicBlock block, Set<ConstantFact> inFacts) {
- Set<ConstantFact> currentFacts = new HashSet<>(inFacts);
+ var currentFacts = new HashSet<ConstantFact>(inFacts);
// Process each statement in the block
for (Tree stmt : block.getStatements()) {
Recipes in example diff:
org.openrewrite.java.migrate.lang.var.UseVarForGenericsConstructors
org.openrewrite.java.migrate.lang.var.UseVarForGenericMethodInvocations
Problem
The variable
currentFactsis first assigned from a generic constructor, and then later overwritten by a method invocation in a loop. That second assignment doesn't work anymore when the type is switched from an explicitSetto an impliedHashSetthrough the use ofvar.Expected behavior
We should avoid making changes to variables that are later reassigned, if the original type differs from the initializer.
Example diff
From:
src/test/java/org/openrewrite/analysis/dataflow/ForwardDataFlowAnalysisTest.java@Override protected Set<ConstantFact> transfer(BasicBlock block, Set<ConstantFact> inFacts) { - Set<ConstantFact> currentFacts = new HashSet<>(inFacts); + var currentFacts = new HashSet<ConstantFact>(inFacts); // Process each statement in the block for (Tree stmt : block.getStatements()) {Recipes in example diff:
org.openrewrite.java.migrate.lang.var.UseVarForGenericsConstructorsorg.openrewrite.java.migrate.lang.var.UseVarForGenericMethodInvocations