Skip to content

RemoveRedundantTypeCast removes necessary Cast in Case when Generics are used #901

Description

@meyerimx

What version of OpenRewrite are you using?

I am using

  • OpenRewrite v8.82.0
  • Maven/Gradle plugin v6.40.0
  • rewrite-static-analysis v2.35.0

How are you running OpenRewrite?

I am using the Maven plugin, and my project is a single module project.

<plugin>
  <groupId>org.openrewrite.maven</groupId>
  <artifactId>rewrite-maven-plugin</artifactId>
  <version>6.40.0</version>
  <configuration>
    <activeRecipes>
      <recipe>org.openrewrite.staticanalysis.RemoveRedundantTypeCast</recipe>
    </activeRecipes>
  </configuration>
  <dependencies>
    <dependency>
      <groupId>org.openrewrite.recipe</groupId>
      <artifactId>rewrite-static-analysis</artifactId>
      <version>2.35.0</version>
    </dependency>
  </dependencies>
</plugin>

What is the smallest, simplest way to reproduce the problem?

Please note, of course this cast in the A class is not the best solution, in the real use case sadly it's a bit more complex to remove.

public interface I<T> {
  void method(B<T> param);
}

public class B<T> {}

public class A<T> {
  void test(I<? extends T> i, B<T> b) {
    i.method((B) b);
  }
}

What did you expect to see?

Nothing should change, as the cast i required.

public interface I<T> {
  void method(B<T> param);
}

public class B<T> {}

public class A<T> {
  void test(I<? extends T> i, B<T> b) {
    i.method((B) b);
  }
}

What did you see instead?

The cast in the class A is removed. This now cannot compile anymore, so the removed cast is not correct. I guess there is an issue with the generics not being known at that point, but in my opinion it would be better to not remove if something is unknown, right now i just cannot use it for this file at all, because i have to manually change it everytime.

public interface I<T> {
  void method(B<T> param);
}

public class B<T> {}

public class A<T> {
  void test(I<? extends T> i, B<T> b) {
    i.method(b);
  }
}

What is the full stack trace of any errors you encountered?

There are no errors associated with this behavior.

Are you interested in contributing a fix to OpenRewrite?

No

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    Status
    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions