diff --git a/java/java-impl/src/com/intellij/codeInsight/intention/impl/singlereturn/ReturnReplacementContext.java b/java/java-impl/src/com/intellij/codeInsight/intention/impl/singlereturn/ReturnReplacementContext.java index 299ff516a389..f442c317525e 100644 --- a/java/java-impl/src/com/intellij/codeInsight/intention/impl/singlereturn/ReturnReplacementContext.java +++ b/java/java-impl/src/com/intellij/codeInsight/intention/impl/singlereturn/ReturnReplacementContext.java @@ -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); } } } diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/convertToSingleReturn/afterPreserveComments.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/convertToSingleReturn/afterPreserveComments.java new file mode 100644 index 000000000000..44ba3f9a3b45 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/convertToSingleReturn/afterPreserveComments.java @@ -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; + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/convertToSingleReturn/beforePreserveComments.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/convertToSingleReturn/beforePreserveComments.java new file mode 100644 index 000000000000..392aee064931 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/convertToSingleReturn/beforePreserveComments.java @@ -0,0 +1,20 @@ +// "Transform body to single exit-point form" "true-preview" +class Test { + int foo(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 + } +} \ No newline at end of file