diff --git a/java/java-analysis-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/SimplifyBooleanExpressionFix.java b/java/java-analysis-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/SimplifyBooleanExpressionFix.java index fedb1ea4f232..8d8971214f75 100644 --- a/java/java-analysis-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/SimplifyBooleanExpressionFix.java +++ b/java/java-analysis-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/SimplifyBooleanExpressionFix.java @@ -35,6 +35,7 @@ import com.intellij.psi.util.PsiUtil; import com.intellij.util.Function; import com.intellij.util.IncorrectOperationException; import com.siyeh.ig.psiutils.ParenthesesUtils; +import com.siyeh.ig.psiutils.VariableSearchUtils; import org.jetbrains.annotations.NonNls; import org.jetbrains.annotations.NotNull; @@ -130,14 +131,15 @@ public class SimplifyBooleanExpressionFix extends LocalQuickFixOnPsiElement { return true; } - private static void replaceWithStatements(final PsiStatement orig, final PsiStatement statement) throws IncorrectOperationException { + private static void replaceWithStatements(final PsiIfStatement orig, final PsiStatement statement) throws IncorrectOperationException { if (statement == null) { orig.delete(); return; } PsiElement parent = orig.getParent(); if (parent == null) return; - if (statement instanceof PsiBlockStatement && parent instanceof PsiCodeBlock) { + if (statement instanceof PsiBlockStatement && parent instanceof PsiCodeBlock && + !VariableSearchUtils.containsConflictingDeclarations((PsiCodeBlock)parent, (PsiCodeBlock)parent)) { // See IDEADEV-24277 // Code block can only be inlined into another (parent) code block. // Code blocks, which are if or loop statement branches should not be inlined. diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/unwrapIfStatement/afterConflictingVariables.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/unwrapIfStatement/afterConflictingVariables.java new file mode 100644 index 000000000000..c19a040f7a69 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/unwrapIfStatement/afterConflictingVariables.java @@ -0,0 +1,9 @@ +// "Unwrap 'if' statement" "true" +class X { + void f() { + { + int i = 0; + } + int i = 0; + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/unwrapIfStatement/beforeConflictingVariables.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/unwrapIfStatement/beforeConflictingVariables.java new file mode 100644 index 000000000000..9246e00966b3 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/unwrapIfStatement/beforeConflictingVariables.java @@ -0,0 +1,9 @@ +// "Unwrap 'if' statement" "true" +class X { + void f() { + if (true) { + int i = 0; + } + int i = 0; + } +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/codeInsight/daemon/quickFix/UnwrapIfStatementFixTest.java b/java/java-tests/testSrc/com/intellij/codeInsight/daemon/quickFix/UnwrapIfStatementFixTest.java new file mode 100644 index 000000000000..f82216f17de2 --- /dev/null +++ b/java/java-tests/testSrc/com/intellij/codeInsight/daemon/quickFix/UnwrapIfStatementFixTest.java @@ -0,0 +1,42 @@ +/* + * Copyright 2000-2016 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. + */ + +/* + * User: anna + * Date: 21-Mar-2008 + */ +package com.intellij.codeInsight.daemon.quickFix; + +import com.intellij.codeInspection.LocalInspectionTool; +import com.intellij.codeInspection.dataFlow.DataFlowInspection; +import org.jetbrains.annotations.NotNull; + +public class UnwrapIfStatementFixTest extends LightQuickFixParameterizedTestCase { + @NotNull + @Override + protected LocalInspectionTool[] configureLocalInspectionTools() { + return new LocalInspectionTool[]{new DataFlowInspection()}; + } + + public void test() throws Exception { + doAllTests(); + } + + @Override + protected String getBasePath() { + return "/codeInsight/daemonCodeAnalyzer/quickFix/unwrapIfStatement"; + } +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/codeInspection/DataFlowInspectionTestSuite.java b/java/java-tests/testSrc/com/intellij/codeInspection/DataFlowInspectionTestSuite.java index 89cbbc45395a..ad81053648a5 100644 --- a/java/java-tests/testSrc/com/intellij/codeInspection/DataFlowInspectionTestSuite.java +++ b/java/java-tests/testSrc/com/intellij/codeInspection/DataFlowInspectionTestSuite.java @@ -51,6 +51,7 @@ public class DataFlowInspectionTestSuite { suite.addTestSuite(ReplaceWithTernaryOperatorTest.class); suite.addTestSuite(ReplaceWithOfNullableFixTest.class); suite.addTestSuite(ReplaceFromOfNullableFixTest.class); + suite.addTestSuite(UnwrapIfStatementFixTest.class); return suite; } }