Skip to content

Improve support method invocations for RemoveRedundantTypeCast#711

Merged
timtebeek merged 10 commits intomainfrom
support-method-invocations-for-remove-redundanttypecast
Aug 15, 2025
Merged

Improve support method invocations for RemoveRedundantTypeCast#711
timtebeek merged 10 commits intomainfrom
support-method-invocations-for-remove-redundanttypecast

Conversation

@jevanlingen
Copy link
Copy Markdown
Contributor

@jevanlingen jevanlingen commented Aug 15, 2025

What's changed?

  • If a cast is performed with the exact same type as the expression, remove the cast.
  • if a cast is performed on a methodCall and the type is a subtype of the expression, remove the cast.

What's your motivation?

Checklist

  • I've added unit tests to cover both positive and negative cases
  • I've read and applied the recipe conventions and best practices
  • I've used the IntelliJ IDEA auto-formatter on affected files

@jevanlingen jevanlingen self-assigned this Aug 15, 2025
@jevanlingen jevanlingen added enhancement New feature or request recipe labels Aug 15, 2025
@github-project-automation github-project-automation bot moved this to In Progress in OpenRewrite Aug 15, 2025
jevanlingen and others added 2 commits August 15, 2025 10:54
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
@jevanlingen jevanlingen moved this from In Progress to Ready to Review in OpenRewrite Aug 15, 2025
@timtebeek timtebeek changed the title Improve support method invocations for RemoveRedundantTypeCast Improve support method invocations for RemoveRedundantTypeCast Aug 15, 2025
Copy link
Copy Markdown
Member

@timtebeek timtebeek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, thanks! Have you tried this out at scale against say Apache to ensure we've not missed necessary types past what the tests cover? If so then good to merge.

…peCastTest.java

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
@jevanlingen
Copy link
Copy Markdown
Contributor Author

Have you tried this out at scale against say Apache to ensure we've not missed necessary types past what the tests cover?

I did not. So I run it against Apache repositories!

Turned out a @NonNullfield could still be null for Kotlin Gradle files. I fixed it + wrote a small test to show the recipe does do something for Kotlin as well: 4af1a1c. I am wondering though, do we really want to support Kotlin for this recipe or should we add an exclusion?

@timtebeek
Copy link
Copy Markdown
Member

Fine to exclude for now,. especially since I don't think we handle the inverse there well.

github-actions[bot]

This comment was marked as spam.

@timtebeek timtebeek merged commit f6a2d03 into main Aug 15, 2025
2 checks passed
@timtebeek timtebeek deleted the support-method-invocations-for-remove-redundanttypecast branch August 15, 2025 16:22
@github-project-automation github-project-automation bot moved this from Ready to Review to Done in OpenRewrite Aug 15, 2025
mergify bot added a commit to robfrank/linklift that referenced this pull request Sep 2, 2025
… 2.15.0 to 2.16.0 [skip ci]

Bumps [org.openrewrite.recipe:rewrite-static-analysis](https://github.com/openrewrite/rewrite-static-analysis) from 2.15.0 to 2.16.0.
Release notes

*Sourced from [org.openrewrite.recipe:rewrite-static-analysis's releases](https://github.com/openrewrite/rewrite-static-analysis/releases).*

> 2.16.0
> ------
>
> What's Changed
> --------------
>
> * fix: Enhanced UseCollectionInterfaces to prevent conversions when non interface methods are used by [`@​e5LA`](https://github.com/e5LA) in [openrewrite/rewrite-static-analysis#709](https://github.com/openrewrite/rewrite-static-analysis/pull/709)
> * Improve support method invocations for `RemoveRedundantTypeCast` by [`@​jevanlingen`](https://github.com/jevanlingen) in [openrewrite/rewrite-static-analysis#711](https://github.com/openrewrite/rewrite-static-analysis/pull/711)
> * Do not move field annotations around arrays by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-static-analysis#712](https://github.com/openrewrite/rewrite-static-analysis/pull/712)
> * Use classpath from resource for Refaster by [`@​jevanlingen`](https://github.com/jevanlingen) in [openrewrite/rewrite-static-analysis#714](https://github.com/openrewrite/rewrite-static-analysis/pull/714)
> * Fix NPE in UseCollectionInterfaces with annotated raw collection types by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-static-analysis#717](https://github.com/openrewrite/rewrite-static-analysis/pull/717)
> * Prefer `System getProperty()` over `getenv()` by [`@​Akanksha928`](https://github.com/Akanksha928) in [openrewrite/rewrite-static-analysis#679](https://github.com/openrewrite/rewrite-static-analysis/pull/679)
> * Expand coverage of UseCollectionInterfaces to LinkedList, Stack, TreeMap, TreeSet and PriorityQueue by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-static-analysis#718](https://github.com/openrewrite/rewrite-static-analysis/pull/718)
> * Fix ReplaceLambdaWithMethodReference to not replace lambdas inside anonymous inner classes by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-static-analysis#723](https://github.com/openrewrite/rewrite-static-analysis/pull/723)
>
> New Contributors
> ----------------
>
> * [`@​Akanksha928`](https://github.com/Akanksha928) made their first contribution in [openrewrite/rewrite-static-analysis#679](https://github.com/openrewrite/rewrite-static-analysis/pull/679)
>
> **Full Changelog**: <openrewrite/rewrite-static-analysis@v2.15.0...v2.16.0>


Commits

* [`53c6b3e`](openrewrite/rewrite-static-analysis@53c6b3e) Update documentation examples
* [`0437c5c`](openrewrite/rewrite-static-analysis@0437c5c) Fix ReplaceLambdaWithMethodReference to not replace lambdas inside anonymous ...
* [`da75646`](openrewrite/rewrite-static-analysis@da75646) Expand coverage of UseCollectionInterfaces to LinkedList, Stack, TreeMap, Tre...
* [`d01363c`](openrewrite/rewrite-static-analysis@d01363c) Prefer `System getProperty()` over `getenv()` ([#679](https://github.com/openrewrite/rewrite-static-analysis/issues/679))
* [`6f8b0df`](openrewrite/rewrite-static-analysis@6f8b0df) AssertJ best practices
* [`5f64939`](openrewrite/rewrite-static-analysis@5f64939) Fix NPE in UseCollectionInterfaces with annotated raw collection types ([#717](https://github.com/openrewrite/rewrite-static-analysis/issues/717))
* [`feee568`](openrewrite/rewrite-static-analysis@feee568) Fix annotation order and import
* [`a51a3ba`](openrewrite/rewrite-static-analysis@a51a3ba) Use classpath from resource for Refaster ([#714](https://github.com/openrewrite/rewrite-static-analysis/issues/714))
* [`fa1d3c3`](openrewrite/rewrite-static-analysis@fa1d3c3) Fix OnlyCatchDeclaredExceptions to skip generic type variables
* [`21ad269`](openrewrite/rewrite-static-analysis@21ad269) Do not move field annotations around arrays ([#712](https://github.com/openrewrite/rewrite-static-analysis/issues/712))
* Additional commits viewable in [compare view](openrewrite/rewrite-static-analysis@v2.15.0...v2.16.0)
  
[![Dependabot compatibility score](https://dependabot-badges.githubapp.com/badges/compatibility\_score?dependency-name=org.openrewrite.recipe:rewrite-static-analysis&package-manager=maven&previous-version=2.15.0&new-version=2.16.0)](https://docs.github.com/en/github/managing-security-vulnerabilities/about-dependabot-security-updates#about-compatibility-scores)
Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting `@dependabot rebase`.
[//]: # (dependabot-automerge-start)
[//]: # (dependabot-automerge-end)
---
Dependabot commands and options
  
You can trigger Dependabot actions by commenting on this PR:
- `@dependabot rebase` will rebase this PR
- `@dependabot recreate` will recreate this PR, overwriting any edits that have been made to it
- `@dependabot merge` will merge this PR after your CI passes on it
- `@dependabot squash and merge` will squash and merge this PR after your CI passes on it
- `@dependabot cancel merge` will cancel a previously requested merge and block automerging
- `@dependabot reopen` will reopen this PR if it is closed
- `@dependabot close` will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually
- `@dependabot show  ignore conditions` will show all of the ignore conditions of the specified dependency
- `@dependabot ignore this major version` will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself)
- `@dependabot ignore this minor version` will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself)
- `@dependabot ignore this dependency` will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request recipe

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

2 participants