PY-19399 Prohibit intention "Convert collection to set" for empty collections appcode/146.1665 clion/146.1664
authorMikhail Golubev <mikhail.golubev@jetbrains.com>
Mon, 2 May 2016 19:08:51 +0000 (22:08 +0300)
committerMikhail Golubev <mikhail.golubev@jetbrains.com>
Tue, 3 May 2016 11:54:01 +0000 (14:54 +0300)
since there is no literal for empty set in Python.

python/src/com/jetbrains/python/codeInsight/intentions/PyBaseConvertCollectionLiteralIntention.java
python/src/com/jetbrains/python/codeInsight/intentions/PyConvertLiteralToSetIntention.java
python/testData/intentions/PyConvertCollectionLiteralIntentionTest/cannotConvertEmptyListToSet.py [new file with mode: 0644]
python/testData/intentions/PyConvertCollectionLiteralIntentionTest/cannotConvertEmptyTupleToSet.py [new file with mode: 0644]
python/testSrc/com/jetbrains/python/intentions/PyConvertCollectionLiteralIntentionTest.java

index d6dd06ae2709329b0fc1d99dc8c9fd81684b939a..db243d24b447b3b7fd484b8ab3c5be897c9a932a 100644 (file)
@@ -82,7 +82,7 @@ public abstract class PyBaseConvertCollectionLiteralIntention extends BaseIntent
     return isAvailableForCollection(literal);
   }
 
-  protected boolean isAvailableForCollection(PySequenceExpression literal) {
+  protected boolean isAvailableForCollection(@NotNull PySequenceExpression literal) {
     return true;
   }
 
index 0f21128c657b642d400e8d3c25b685c43f52c17e..5f18663024ad57664cb33e689f48420b35223a87 100644 (file)
  */
 package com.jetbrains.python.codeInsight.intentions;
 
-import com.intellij.openapi.util.Condition;
 import com.intellij.util.containers.ContainerUtil;
-import com.jetbrains.python.psi.*;
+import com.jetbrains.python.psi.LanguageLevel;
+import com.jetbrains.python.psi.PySequenceExpression;
+import com.jetbrains.python.psi.PySetLiteralExpression;
+import com.jetbrains.python.psi.PyTargetExpression;
 import org.jetbrains.annotations.NotNull;
 
 /**
@@ -29,16 +31,11 @@ public class PyConvertLiteralToSetIntention extends PyBaseConvertCollectionLiter
   }
 
   @Override
-  protected boolean isAvailableForCollection(PySequenceExpression literal) {
-    return LanguageLevel.forElement(literal).isAtLeast(LanguageLevel.PYTHON27) && !isInTargetPosition(literal);
+  protected boolean isAvailableForCollection(@NotNull PySequenceExpression literal) {
+    return !literal.isEmpty() && LanguageLevel.forElement(literal).isAtLeast(LanguageLevel.PYTHON27) && !isInTargetPosition(literal);
   }
 
-  private static boolean isInTargetPosition(@NotNull final PySequenceExpression sequenceLiteral) {
-    return ContainerUtil.exists(sequenceLiteral.getElements(), new Condition<PyExpression>() {
-      @Override
-      public boolean value(PyExpression expression) {
-        return expression instanceof PyTargetExpression;
-      }
-    });
+  private static boolean isInTargetPosition(@NotNull PySequenceExpression sequenceLiteral) {
+    return ContainerUtil.exists(sequenceLiteral.getElements(), expression -> expression instanceof PyTargetExpression);
   }
 }
diff --git a/python/testData/intentions/PyConvertCollectionLiteralIntentionTest/cannotConvertEmptyListToSet.py b/python/testData/intentions/PyConvertCollectionLiteralIntentionTest/cannotConvertEmptyListToSet.py
new file mode 100644 (file)
index 0000000..646916b
--- /dev/null
@@ -0,0 +1 @@
+[<caret>]
\ No newline at end of file
diff --git a/python/testData/intentions/PyConvertCollectionLiteralIntentionTest/cannotConvertEmptyTupleToSet.py b/python/testData/intentions/PyConvertCollectionLiteralIntentionTest/cannotConvertEmptyTupleToSet.py
new file mode 100644 (file)
index 0000000..bd27bb5
--- /dev/null
@@ -0,0 +1 @@
+(<caret>)
\ No newline at end of file
index ba2211fe2b5898ebbdce92aa18d7c1ecdd3304f1..5bbc03d56b42e043cc740d8db56247a1d7417eb9 100644 (file)
@@ -143,4 +143,14 @@ public class PyConvertCollectionLiteralIntentionTest extends PyIntentionTestCase
   public void testConvertOneElementTupleWithCommentToList() {
     doIntentionTest(CONVERT_TUPLE_TO_LIST);
   }
+
+  // PY-19399
+  public void testCannotConvertEmptyTupleToSet() {
+    doNegativeTest(CONVERT_TUPLE_TO_SET);
+  }
+  
+  // PY-19399
+  public void testCannotConvertEmptyListToSet() {
+    doNegativeTest(CONVERT_LIST_TO_SET);
+  }
 }