mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
[java] inline method: keep side effects free single returns (IDEA-280791)
avoid generating red code though when return value won't be a valid statement on its own GitOrigin-RevId: ad768417d454192680b8d6692d8e1037b3ee9308
This commit is contained in:
committed by
intellij-monorepo-bot
parent
da021d47d0
commit
2aa6ed2a41
@@ -1,4 +1,4 @@
|
||||
// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
|
||||
// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
|
||||
package com.intellij.refactoring.util;
|
||||
|
||||
import com.intellij.codeInsight.BlockUtils;
|
||||
@@ -508,7 +508,9 @@ public final class InlineUtil {
|
||||
for (PsiReturnStatement returnStatement : returnStatements) {
|
||||
final PsiExpression returnValue = returnStatement.getReturnValue();
|
||||
if (returnValue != null) {
|
||||
List<PsiExpression> sideEffects = SideEffectChecker.extractSideEffectExpressions(returnValue);
|
||||
List<PsiExpression> sideEffects = !singleReturnMethod(method) || !PsiUtil.isStatement(returnValue)
|
||||
? SideEffectChecker.extractSideEffectExpressions(returnValue)
|
||||
: Collections.singletonList(returnValue);
|
||||
CommentTracker ct = new CommentTracker();
|
||||
sideEffects.forEach(ct::markUnchanged);
|
||||
PsiStatement[] statements = StatementExtractor.generateStatements(sideEffects, returnValue);
|
||||
@@ -529,6 +531,14 @@ public final class InlineUtil {
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean singleReturnMethod(PsiMethod method) {
|
||||
int statementCount = Objects.requireNonNull(method.getBody()).getStatementCount();
|
||||
if (!method.hasModifierProperty(PsiModifier.STATIC) && PsiTreeUtil.getContextOfType(method, PsiClass.class) != null) { //this declaration
|
||||
statementCount--;
|
||||
}
|
||||
return statementCount <= 1;
|
||||
}
|
||||
|
||||
public static PsiExpression inlineInitializer(PsiVariable variable, PsiExpression initializer, PsiJavaCodeReferenceElement ref) {
|
||||
Project project = variable.getProject();
|
||||
PsiElementFactory factory = JavaPsiFacade.getElementFactory(project);
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
class A {
|
||||
{
|
||||
g();
|
||||
}
|
||||
int <caret>g() {
|
||||
return new Integer(0);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
class A {
|
||||
{
|
||||
new Integer(0);
|
||||
}
|
||||
}
|
||||
@@ -1,18 +1,4 @@
|
||||
/*
|
||||
* Copyright 2000-2017 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
|
||||
package com.intellij.java.refactoring.inline;
|
||||
|
||||
import com.intellij.JavaTestUtil;
|
||||
@@ -65,6 +51,7 @@ public class InlineMethodTest extends LightRefactoringTestCase {
|
||||
public void testSideEffect() { doTest(); }
|
||||
|
||||
public void testInlineWithTry() { doTest(); }
|
||||
public void testEmptyMethod() { doTest(); }
|
||||
|
||||
public void testVoidWithReturn() { doTest(); }
|
||||
public void testVoidWithReturn1() { doTest(); }
|
||||
|
||||
Reference in New Issue
Block a user