openrewrite / openrewrite/rewrite-static-analysis

`SimplifyElseBranch` puts comment in not the best location

Open
#878 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

What version of OpenRewrite are you using?

I am using

  • Maven/Gradle plugin v7.32.1
  • rewrite-recipe-bom:3.30.0

How are you running OpenRewrite?

I am using the Gradle plugin.

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

Sorry for not providing an MWE, because of the lack of time.

I guess a simpler version is:

Before:

if (<condition>) {
    do1();
} else {
    // Comment.
    if (<condition 2>) {
        do2();
    }
}

What did you expect to see?

Not sure if this is a better version.

if (<condition>) {
    do1();
} else if (<condition 2>) {
    // Comment.
    do2();
}

What did you see instead?

if (<condition>) {
    do1();
} else // Comment.
    if (<condition 2>) {
        do2();
}

Which is technically right, but a bit crazy. I have never seen such a placement of a comment.

It's not really a bug, but a discussion on the style and approach.

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

No errors.

Are you interested in contributing a fix to OpenRewrite?

Yes, but I don't have time right now. Maybe later

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 at the SimplifyElseBranch recipe entry point and reproduce the issue with the Java examples in the report. Compare the current output, which places the comment after else, with the proposed output; done means the comment is retained in the more conventional location without changing the conditional structure.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.