From f127b4491c1129cda4bd464f14fdc72dea8735e0 Mon Sep 17 00:00:00 2001 From: Tagir Valeev Date: Tue, 24 Sep 2019 12:19:18 +0700 Subject: [PATCH] DeleteSideEffectsAwareFix: fix removal of for-update statement GitOrigin-RevId: 15a64c215cc98f4f94b76edc3486bcf762a8be28 --- .../quickfix/DeleteSideEffectsAwareFix.java | 4 +- .../deleteRedundantUpdate/afterFor.java | 8 ++++ .../deleteRedundantUpdate/beforeFor.java | 8 ++++ .../DeleteRedundantUpdateFixTest.java | 43 +++++++++++++++++++ .../DataFlowInspectionTestSuite.java | 1 + 5 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/deleteRedundantUpdate/afterFor.java create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/deleteRedundantUpdate/beforeFor.java create mode 100644 java/java-tests/testSrc/com/intellij/java/codeInsight/daemon/quickFix/DeleteRedundantUpdateFixTest.java diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/DeleteSideEffectsAwareFix.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/DeleteSideEffectsAwareFix.java index 022331f30eb6..40d920073ac7 100644 --- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/DeleteSideEffectsAwareFix.java +++ b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/DeleteSideEffectsAwareFix.java @@ -109,7 +109,9 @@ public class DeleteSideEffectsAwareFix extends LocalQuickFixAndIntentionActionOn statement = Objects.requireNonNull(PsiTreeUtil.getNextSiblingOfType(lastAdded, PsiStatement.class)); } PsiElement parent = statement.getParent(); - if (parent instanceof PsiStatement && !(parent instanceof PsiIfStatement && ((PsiIfStatement)parent).getElseBranch() == statement)) { + if (parent instanceof PsiStatement && + !(parent instanceof PsiIfStatement && ((PsiIfStatement)parent).getElseBranch() == statement) && + !(parent instanceof PsiForStatement && ((PsiForStatement)parent).getUpdate() == statement)) { ct.replaceAndRestoreComments(statement, "{}"); } else { ct.deleteAndRestoreComments(statement); diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/deleteRedundantUpdate/afterFor.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/deleteRedundantUpdate/afterFor.java new file mode 100644 index 000000000000..0adf1b95d968 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/deleteRedundantUpdate/afterFor.java @@ -0,0 +1,8 @@ +// "Delete element" "true" +class X { + void test() { + for (int i = 0; i < Integer.MAX_VALUE; ) { + + } + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/deleteRedundantUpdate/beforeFor.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/deleteRedundantUpdate/beforeFor.java new file mode 100644 index 000000000000..1ba53f09e99c --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/deleteRedundantUpdate/beforeFor.java @@ -0,0 +1,8 @@ +// "Delete element" "true" +class X { + void test() { + for (int i = 0; i < Integer.MAX_VALUE; i *= 2) { + + } + } +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/java/codeInsight/daemon/quickFix/DeleteRedundantUpdateFixTest.java b/java/java-tests/testSrc/com/intellij/java/codeInsight/daemon/quickFix/DeleteRedundantUpdateFixTest.java new file mode 100644 index 000000000000..42727c21ed0d --- /dev/null +++ b/java/java-tests/testSrc/com/intellij/java/codeInsight/daemon/quickFix/DeleteRedundantUpdateFixTest.java @@ -0,0 +1,43 @@ +/* + * 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. + */ + +package com.intellij.java.codeInsight.daemon.quickFix; + +import com.intellij.codeInsight.daemon.quickFix.LightQuickFixParameterizedTestCase; +import com.intellij.codeInspection.LocalInspectionTool; +import com.intellij.codeInspection.dataFlow.DataFlowInspection; +import com.intellij.testFramework.LightProjectDescriptor; +import com.intellij.testFramework.fixtures.LightJavaCodeInsightFixtureTestCase; +import org.jetbrains.annotations.NotNull; + +public class DeleteRedundantUpdateFixTest extends LightQuickFixParameterizedTestCase { + @NotNull + @Override + protected LocalInspectionTool[] configureLocalInspectionTools() { + return new LocalInspectionTool[]{new DataFlowInspection()}; + } + + @NotNull + @Override + protected LightProjectDescriptor getProjectDescriptor() { + return LightJavaCodeInsightFixtureTestCase.JAVA_10_ANNOTATED; + } + + @Override + protected String getBasePath() { + return "/codeInsight/daemonCodeAnalyzer/quickFix/deleteRedundantUpdate"; + } +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/java/codeInspection/DataFlowInspectionTestSuite.java b/java/java-tests/testSrc/com/intellij/java/codeInspection/DataFlowInspectionTestSuite.java index f506e563f5df..e921ae011dcf 100644 --- a/java/java-tests/testSrc/com/intellij/java/codeInspection/DataFlowInspectionTestSuite.java +++ b/java/java-tests/testSrc/com/intellij/java/codeInspection/DataFlowInspectionTestSuite.java @@ -69,6 +69,7 @@ import org.junit.runners.Suite; RedundantInstanceofFixTest.class, ReplaceComputeWithComputeIfPresentFixTest.class, DeleteSwitchLabelFixTest.class, + DeleteRedundantUpdateFixTest.class }) public class DataFlowInspectionTestSuite { }