Skip to content

Issue discovered on src/test/java/org/openrewrite/analysis/dataflow/ForwardDataFlowAnalysisTest.java with UseVarForGenericsConstructors #1077

@timtebeek

Description

@timtebeek

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions