IfElseIfConstructToSwitch drops leading comments inside single-statement blocks and creates 2-case switches (SonarQube S1301)
メンテナーはふだん 1 日以内に返信
まだ誰も着手していません。
評価
調査の方向性
IfElseIfConstructToSwitch.java、特に getStatement と、IfElseIfConstructToSwitchTest.java にある再現例を読みます。まず、ブロック内の単一の文に先頭のコメントがどのように付与されるか、また変換が報告された2つのパターンチェックとフォールバックをどのように処理するかを確認します。両方の動作について回帰テストを追加します。コメントが保持され、変換後のswitchで報告された2つのcaseしきい値の問題が回避されれば完了です。
索引モデルが issue の本文から書いたものです。
説明
I checked out all relevant OpenRewrite repo's, fed the AI with existing recipe name and a very careful description. Antigravity with Vertex / Gemini was then tasked to check and test. .Also to create a ticket for it, using your default template. The result is shown beneath.
What version of OpenRewrite are you using?
- OpenRewrite v8.30.0
- rewrite-maven-plugin:6.50.0
- Java 25
How are you running OpenRewrite?
I am using the Maven plugin on a multi-module Java 21+ project:
What is the smallest, simplest way to reproduce the problem?
When an if-else-if-else construct contains inline comments preceding single statements inside { ... } blocks:
class Test {
void draw(Object cell) {
if (cell instanceof FirstType first) {
// First type cell description.
render(first);
} else if (cell instanceof SecondType second) {
// Second type cell description.
render(second);
} else {
throw new IllegalStateException("Unknown: " + cell.getClass().getName());
}
}
void render(Object o) {}
}
Or as an OpenRewrite unit test in IfElseIfConstructToSwitchTest.java:
@Test
void preserveSingleStatementBranchLeadingComment() {
rewriteRun(
//language=java
java(
"""
class Test {
void draw(Object cell) {
if (cell instanceof FirstType first) {
// First type cell description.
render(first);
} else if (cell instanceof SecondType second) {
// Second type cell description.
render(second);
} else {
throw new IllegalStateException("Unknown: " + cell.getClass().getName());
}
}
void render(Object o) {}
}
""",
"""
class Test {
void draw(Object cell) {
switch (cell) {
case FirstType first -> {
// First type cell description.
render(first);
}
case SecondType second -> {
// Second type cell description.
render(second);
}
case null, default -> throw new IllegalStateException("Unknown: " + cell.getClass().getName());
}
}
void render(Object o) {}
}
"""
)
);
}
What did you expect to see?
Comments placed above statements inside { ... } should be preserved (by keeping the block { ... } if unwrapping would lose comments):
class Test {
void draw(Object cell) {
switch (cell) {
case FirstType first -> {
// First type cell description.
render(first);
}
case SecondType second -> {
// Second type cell description.
render(second);
}
case null, default -> throw new IllegalStateException("Unknown: " + cell.getClass().getName());
}
}
void render(Object o) {}
}
(Additionally: hasLastElseBlock should not count toward the minimum 3 cases threshold, as converting 2 pattern checks + fallback else results in a 2-case switch triggering SonarQube/Checkstyle java:S1301).
What did you see instead?
The single-statement block { ... } is unwrapped and all inline comments are permanently deleted:
class Test {
void draw(Object cell) {
switch (cell) {
case FirstType first -> render(first);
case SecondType second -> render(second);
case null, default -> throw new IllegalStateException("Unknown: " + cell.getClass().getName());
}
}
void render(Object o) {}
}
Root Cause
In IfElseIfConstructToSwitch.java:
private Statement getStatement(Statement statement) {
if (statement instanceof J.Block && ((J.Block) statement).getStatements().size() == 1) {
Statement firstStatement = ((J.Block) statement).getStatements().get(0);
if ((firstStatement instanceof Expression || firstStatement instanceof J.Throw) &&
firstStatement.getComments().isEmpty() &&
statement.getComments().isEmpty() &&
((J.Block) statement).getEnd().getComments().isEmpty()) {
return firstStatement;
}
}
return statement;
}
Leading comments inside the block are attached to firstStatement.getPrefix().getComments(). Because firstStatement.getPrefix().getComments().isEmpty() is not checked, getStatement() unwraps the block and discards the comments.
What is the full stack trace of any errors you encountered?
N/A (No runtime exception thrown — this is silent comment loss during AST transformation).
Are you interested in contributing a fix to OpenRewrite?
Yes, I have already reproduced the issue, added a unit test, and verified the fix locally:
diff --git a/src/main/java/org/openrewrite/java/migrate/lang/IfElseIfConstructToSwitch.java b/src/main/java/org/openrewrite/java/migrate/lang/IfElseIfConstructToSwitch.java
--- a/src/main/java/org/openrewrite/java/migrate/lang/IfElseIfConstructToSwitch.java
+++ b/src/main/java/org/openrewrite/java/migrate/lang/IfElseIfConstructToSwitch.java
@@ -347,6 +347,7 @@ public class IfElseIfConstructToSwitch extends Recipe {
if (statement instanceof J.Block && ((J.Block) statement).getStatements().size() == 1) {
Statement firstStatement = ((J.Block) statement).getStatements().get(0);
if ((firstStatement instanceof Expression || firstStatement instanceof J.Throw) &&
+ firstStatement.getPrefix().getComments().isEmpty() &&
firstStatement.getComments().isEmpty() &&
statement.getComments().isEmpty() &&
((J.Block) statement).getEnd().getComments().isEmpty()) {
- 主要言語
- Java
- スター
- 159
- フォーク
- 130
- 平均マージ
- 7時間 49分
- マージ済み PR(30日)
- 60
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートなし
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
openrewrite/rewrite-migrate-java のほかの issue
-
bug
難易度 3/5 1〜2日 初心者へのやさしさ 72/100
openrewrite/rewrite-migrate-java#1319 ·
メンテナーはふだん 1 日以内に返信
-
bug
難易度 3/5 1〜2日 初心者へのやさしさ 55/100
openrewrite/rewrite-migrate-java#1314 ·
メンテナーはふだん 1 日以内に返信
-
bug
難易度 3/5 1〜2日 初心者へのやさしさ 55/100
openrewrite/rewrite-migrate-java#1310 ·
メンテナーはふだん 1 日以内に返信
-
bug
難易度 4/5 3〜5日 初心者へのやさしさ 55/100
openrewrite/rewrite-migrate-java#1307 ·
メンテナーはふだん 1 日以内に返信
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 55/100
openrewrite/rewrite-migrate-java#1304 ·
メンテナーはふだん 1 日以内に返信
openrewrite/rewrite-migrate-java の issue をすべて見る
似ている issue
-
human-required sync-failed
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
Azure/osdu-spi-file#63 ·
メンテナーはふだん 1 日以内に返信
-
frontend quick win
難易度 2/5 1〜3時間 初心者へのやさしさ 65/100
CodeForPhilly/benefit-decision-toolkit#571 ·
メンテナーはふだん 1 日以内に返信
-
approved
難易度 2/5 1〜3時間 初心者へのやさしさ 67/100
checkstyle/checkstyle#22129 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
メンテナーはふだん 1 日以内に返信
-
Add Hardcharger JDBC driver対応中かも @prmoore77 が今日担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
メンテナーはふだん 1 日以内に返信