Skip to content

Use var for variables initialized with type casts#965

Merged
timtebeek merged 2 commits intomainfrom
use-var-for-type-casts-only
Jan 13, 2026
Merged

Use var for variables initialized with type casts#965
timtebeek merged 2 commits intomainfrom
use-var-for-type-casts-only

Conversation

@timtebeek
Copy link
Copy Markdown
Member

What's changed?

Added a new recipe with very limited scope in applying var only where initialized by a type cast within methods.

What's your motivation?

This seemed like a simple and safe, non-controversial usage of var that we can then adopt in our tests, which frequently unpack and cast.

Anything in particular you'd like reviewers to focus on?

Any edge cases where this might be unsafe?

Have you considered any alternatives or workarounds?

Could have also tackled static initializer blocks, but that seemed rare to me to have casts there.

@timtebeek timtebeek requested a review from MBoegers January 11, 2026 12:20
@timtebeek timtebeek self-assigned this Jan 11, 2026
@timtebeek timtebeek added the recipe Recipe requested label Jan 11, 2026
@github-project-automation github-project-automation bot moved this to In Progress in OpenRewrite Jan 11, 2026
@timtebeek timtebeek moved this from In Progress to Ready to Review in OpenRewrite Jan 12, 2026
Copy link
Copy Markdown
Contributor

@MBoegers MBoegers left a comment

Choose a reason for hiding this comment

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

LGTM, great to see another edge case closed.

@timtebeek timtebeek merged commit 170b248 into main Jan 13, 2026
2 checks passed
@timtebeek timtebeek deleted the use-var-for-type-casts-only branch January 13, 2026 11:01
@github-project-automation github-project-automation bot moved this from Ready to Review to Done in OpenRewrite Jan 13, 2026
@timtebeek
Copy link
Copy Markdown
Member Author

mergify bot added a commit to robfrank/linklift that referenced this pull request Jan 25, 2026
…25.0 to 3.26.0 [skip ci]

Bumps [org.openrewrite.recipe:rewrite-migrate-java](https://github.com/openrewrite/rewrite-migrate-java) from 3.25.0 to 3.26.0.
Release notes

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

> 3.26.0
> ------
>
> What's Changed
> --------------
>
> * Also change managed dependency classifier to ehcache to avoid breaking multi module projects by [`@​dsgrieve`](https://github.com/dsgrieve) in [openrewrite/rewrite-migrate-java#963](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/963)
> * fix `IndexOutOfBoundsException` that occurs in `UseNoArgsConstructor` when visiting record compact constructor method by [`@​zakaria-shahen`](https://github.com/zakaria-shahen) in [openrewrite/rewrite-migrate-java#964](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/964)
> * Replace `getDisplayName()` and `getDescription()` methods with fields by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#966](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/966)
> * Use `var` for variables initialized with type casts by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#965](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/965)
> * Add recipe to find locale-sensitive date/time formatting affected by JDK 20+ CLDR changes by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#967](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/967)
> * Normalize version numbers in `FindJavaVersion` by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#969](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/969)
> * Fix JavaxWebXmlToJakartaWebXml regex to match multiline schemaLocation by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#971](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/971)
>
> New Contributors
> ----------------
>
> * [`@​dsgrieve`](https://github.com/dsgrieve) made their first contribution in [openrewrite/rewrite-migrate-java#963](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/963)
>
> **Full Changelog**: <openrewrite/rewrite-migrate-java@v3.25.0...v3.26.0>


Commits

* [`424b525`](openrewrite/rewrite-migrate-java@424b525) Fix JavaxWebXmlToJakartaWebXml regex to match multiline schemaLocation ([#971](https://redirect.github.com/openrewrite/rewrite-migrate-java/issues/971))
* [`4beeb8a`](openrewrite/rewrite-migrate-java@4beeb8a) Normalize version numbers in `FindJavaVersion` ([#969](https://redirect.github.com/openrewrite/rewrite-migrate-java/issues/969))
* [`1180190`](openrewrite/rewrite-migrate-java@1180190) [Auto] SDKMAN! Java candidates as of 2026-01-19T1023
* [`288ea96`](openrewrite/rewrite-migrate-java@288ea96) OpenRewrite recipe best practices
* [`c7f8d21`](openrewrite/rewrite-migrate-java@c7f8d21) Replace `getEstimatedEffortPerOccurrence()` method with field
* [`b22610d`](openrewrite/rewrite-migrate-java@b22610d) Replace `getTags()` method with field
* [`90b4e63`](openrewrite/rewrite-migrate-java@90b4e63) Add recipe to find locale-sensitive date/time formatting affected by JDK 20+ ...
* [`bb9adab`](openrewrite/rewrite-migrate-java@bb9adab) Update Gradle wrapper to 9.3.0
* [`da7c2e1`](openrewrite/rewrite-migrate-java@da7c2e1) Replace `Arrays.asList` with `List.of` in tests
* [`7a52f88`](openrewrite/rewrite-migrate-java@7a52f88) OpenRewrite recipe best practices
* Additional commits viewable in [compare view](openrewrite/rewrite-migrate-java@v3.25.0...v3.26.0)
  
[![Dependabot compatibility score](https://dependabot-badges.githubapp.com/badges/compatibility\_score?dependency-name=org.openrewrite.recipe:rewrite-migrate-java&package-manager=maven&previous-version=3.25.0&new-version=3.26.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

recipe Recipe requested

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

2 participants