From a9069f60e85ca231d276195b9aeb841fc5a75408 Mon Sep 17 00:00:00 2001 From: Tagir Valeev Date: Tue, 11 May 2021 16:10:18 +0700 Subject: [PATCH] [java-intentions] IterateOverIterable: disable for empty lists and newly-created arrays (IJ-CR-8575) GitOrigin-RevId: 78ef76ab85b1e051d0bb887801379136be041c5b --- .../impl/quickfix/IterateOverIterableIntention.java | 9 +++++++++ .../iterateOver/afterNewArrayWithInitializer.java | 9 +++++++++ .../quickFix/iterateOver/beforeEmptyList.java | 8 ++++++++ .../quickFix/iterateOver/beforeNewArray.java | 6 ++++++ .../iterateOver/beforeNewArrayWithInitializer.java | 6 ++++++ 5 files changed, 38 insertions(+) create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/iterateOver/afterNewArrayWithInitializer.java create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/iterateOver/beforeEmptyList.java create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/iterateOver/beforeNewArray.java create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/iterateOver/beforeNewArrayWithInitializer.java diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/IterateOverIterableIntention.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/IterateOverIterableIntention.java index c6af9addecd4..9833173c7753 100644 --- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/IterateOverIterableIntention.java +++ b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/IterateOverIterableIntention.java @@ -35,6 +35,7 @@ import com.intellij.psi.util.PsiTreeUtil; import com.intellij.psi.util.PsiUtil; import com.intellij.refactoring.util.CommonRefactoringUtil; import com.intellij.util.IncorrectOperationException; +import com.siyeh.ig.psiutils.ConstructionUtils; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -86,6 +87,14 @@ public class IterateOverIterableIntention implements IntentionAction { } PsiExpression expression = getIterableExpression(editor, file); if (expression == null) return false; + if (ConstructionUtils.isEmptyCollectionInitializer(expression)) { + // Empty collection: iterating doesn't make much sense + return false; + } + if (expression instanceof PsiNewExpression && ((PsiNewExpression)expression).getArrayDimensions().length > 0) { + // new array without initializers: all elements are 0/null/false, so iterating doesn't make much sense + return false; + } myText = JavaBundle.message("intention.name.iterate.over", Objects.requireNonNull(expression.getType()).getPresentableText()); return true; } diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/iterateOver/afterNewArrayWithInitializer.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/iterateOver/afterNewArrayWithInitializer.java new file mode 100644 index 000000000000..16b108eb396e --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/iterateOver/afterNewArrayWithInitializer.java @@ -0,0 +1,9 @@ +// "Iterate over String[]" "true" +class Test { + void foo() { + for (String s : new String[]{"a", "b", "c"}) { + + } + + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/iterateOver/beforeEmptyList.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/iterateOver/beforeEmptyList.java new file mode 100644 index 000000000000..9870a78e434f --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/iterateOver/beforeEmptyList.java @@ -0,0 +1,8 @@ +// "Disable 'Iterate'" "false" +import java.util.*; + +class Test { + void foo() { + new ArrayList<>(); + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/iterateOver/beforeNewArray.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/iterateOver/beforeNewArray.java new file mode 100644 index 000000000000..3b2e2a201dfb --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/iterateOver/beforeNewArray.java @@ -0,0 +1,6 @@ +// "Disable 'Iterate'" "false" +class Test { + void foo() { + new String[10]; + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/iterateOver/beforeNewArrayWithInitializer.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/iterateOver/beforeNewArrayWithInitializer.java new file mode 100644 index 000000000000..a2b8c13169e1 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/iterateOver/beforeNewArrayWithInitializer.java @@ -0,0 +1,6 @@ +// "Iterate over String[]" "true" +class Test { + void foo() { + new String[] {"a", "b", "c"}; + } +} \ No newline at end of file