IfElseIfConstructToSwitch drops leading comments inside single-statement blocks and creates 2-case switches (SonarQube S1301)
Maintainer thường phản hồi trong vòng 1 ngày
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 2/5
- Thời gian dự kiến
- 1-3 giờ
- Mức phù hợp với người mới
- 68/100
Hướng nghiên cứu
Đọc IfElseIfConstructToSwitch.java, đặc biệt là getStatement, và trường hợp tái hiện trong IfElseIfConstructToSwitchTest.java. Trước tiên, hãy kiểm tra cách các chú thích đứng đầu được gắn với câu lệnh duy nhất trong một khối và cách quá trình chuyển đổi xử lý hai lần kiểm tra pattern được báo cáo cùng với fallback. Bổ sung kiểm thử hồi quy cho cả hai hành vi; công việc hoàn tất khi các chú thích được giữ nguyên và switch tạo ra tránh được vấn đề ngưỡng hai case đã nêu.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
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()) {
- Ngôn ngữ chính
- Java
- Star
- 159
- Fork
- 130
- Merge trung bình
- 7 giờ 49 phút
- Pull request đã merge (30 ngày)
- 60
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Không có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của openrewrite/rewrite-migrate-java
-
bug
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 72/100
openrewrite/rewrite-migrate-java#1319 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
bug
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 55/100
openrewrite/rewrite-migrate-java#1314 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
bug
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 55/100
openrewrite/rewrite-migrate-java#1310 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
bug
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 55/100
openrewrite/rewrite-migrate-java#1307 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 55/100
openrewrite/rewrite-migrate-java#1304 ·
Maintainer thường phản hồi trong vòng 1 ngày
Tất cả issue của openrewrite/rewrite-migrate-java
Issue tương tự
-
human-required sync-failed
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
Azure/osdu-spi-file#63 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
frontend quick win
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 65/100
CodeForPhilly/benefit-decision-toolkit#571 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
approved
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 67/100
checkstyle/checkstyle#22129 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Add Hardcharger JDBC driverCó thể đã có người làm @prmoore77 đã nhận hôm nay. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
Maintainer thường phản hồi trong vòng 1 ngày