[java] Preserve comments in ReturnReplacementContext#stripUnnecessaryBlocks

#IDEA-369976 Fixed

GitOrigin-RevId: d20a4a7d92ffb5fc7bda36c879868a2d3e2e8413
This commit is contained in:
Louis Vignier
2025-04-02 22:05:33 +00:00
committed by intellij-monorepo-bot
parent 3ad99b5573
commit d82a6a1d1a
3 changed files with 43 additions and 3 deletions
@@ -1,4 +1,4 @@
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
// Copyright 2000-2025 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.codeInsight.intention.impl.singlereturn;
import com.intellij.codeInsight.BlockUtils;
@@ -21,7 +21,7 @@ import static com.intellij.util.ObjectUtils.tryCast;
import static java.util.Objects.requireNonNull;
/**
* Performs replacement of single return statement as the part of {@link ConvertToSingleReturnAction}.
* Performs replacement of a single return statement as the part of {@link ConvertToSingleReturnAction}.
*/
final class ReturnReplacementContext {
private final Project myProject;
@@ -296,7 +296,7 @@ final class ReturnReplacementContext {
if (parentIf != null && parentIf.getElseBranch() == place) {
PsiIfStatement childIf = tryCast(ControlFlowUtils.stripBraces((PsiStatement)place), PsiIfStatement.class);
if (childIf != null) {
place = place.replace(childIf);
place = new CommentTracker().replaceAndRestoreComments(place, childIf);
}
}
}
@@ -0,0 +1,20 @@
// "Transform body to single exit-point form" "true-preview"
class Test {
int foo(int i) {
int result = -1;
// comment 1
// comment 5
// comment 6
if (i == 0) { // comment 2
// comment 3
result = 1;// comment 4
} else if (i == 1) { // comment 7
// comment 8
result = 0;// comment 9
} // comment 10
// comment 11
return result;
}
}
@@ -0,0 +1,20 @@
// "Transform body to single exit-point form" "true-preview"
class Test {
int fo<caret>o(int i) {
// comment 1
if (i == 0) { // comment 2
// comment 3
return 1; // comment 4
}
// comment 5
// comment 6
if (i == 1) { // comment 7
// comment 8
return 0; // comment 9
}
// comment 10
return -1; // comment 11
}
}