openrewrite / openrewrite/rewrite-static-analysis

Enhance `InstanceOfPatternMatch` recipe to inline expression when possible

Open
#85 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement recipe
Dominant language
Java
Stars
62
Forks
112
Avg merge
1d 19h
Merged PRs (30d)
40

Description

For cases when the expressions targeted by the instanceof and type cast expressions both refer to a local variable, which isn't referenced anywhere else outside the if statement and the expression this variable was evaluated from doesn't have any side effects or the declaration immediately precedes the if statement, it would be possible to inline the variable into the instanceof pattern match variable. E.g.

Object o = foo.bar();
if (o instanceof String && ((String) o).isEmpty()) {
    // ...
}

would become:

if (foo.bar() instanceof String o && o.isEmpty()) {
    // ...
}

The required inlining visitor would potentially also be reusable in other recipes.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating the InstanceOfPatternMatch recipe and reading how it currently handles instanceof and cast expressions. Implement the described inlining only when the variable is local, otherwise unused outside the if statement, and the side-effect or declaration conditions are satisfied. Done means the Java example is transformed safely and the inlining visitor remains reusable for other recipes.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
devtools
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.