From cee5232a5ef6586efe62d3e22ee7add60b6f4194 Mon Sep 17 00:00:00 2001 From: "alexey.afanasiev" Date: Tue, 10 Oct 2017 17:23:27 +0300 Subject: [PATCH] IDEA-179713 Add conversion empty list literal to Set --- .../typing/DefaultListOrMapTypeCalculator.kt | 45 +++++++++---------- .../GrAssignAutoTestFalsePositiveTest.groovy | 10 ++++- 2 files changed, 30 insertions(+), 25 deletions(-) diff --git a/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/typing/DefaultListOrMapTypeCalculator.kt b/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/typing/DefaultListOrMapTypeCalculator.kt index 8d51227e3021..cde837e0ab8b 100644 --- a/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/typing/DefaultListOrMapTypeCalculator.kt +++ b/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/typing/DefaultListOrMapTypeCalculator.kt @@ -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-2017 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. package org.jetbrains.plugins.groovy.lang.typing import com.intellij.openapi.util.RecursionManager.doPreventingRecursion @@ -65,17 +51,30 @@ class DefaultListOrMapTypeCalculator : GrTypeCalculator { if (initializers.isNotEmpty()) return null val lType = PsiImplUtil.inferExpectedTypeForDiamond(expression) - if (lType !is PsiClassType || !isInheritor(lType, CommonClassNames.JAVA_UTIL_LIST)) return null - + if (lType !is PsiClassType) return null val scope = expression.resolveScope val facade = JavaPsiFacade.getInstance(expression.project) - val arrayList = facade.findClass(CommonClassNames.JAVA_UTIL_ARRAY_LIST, scope) ?: - facade.findClass(CommonClassNames.JAVA_UTIL_LIST, scope) ?: return null - return facade.elementFactory.createType( - arrayList, - substituteTypeParameter(lType, CommonClassNames.JAVA_UTIL_LIST, 0, false) - ) + if (isInheritor(lType, CommonClassNames.JAVA_UTIL_LIST)) { + val arrayList = facade.findClass(CommonClassNames.JAVA_UTIL_ARRAY_LIST, scope) ?: + facade.findClass(CommonClassNames.JAVA_UTIL_LIST, scope) ?: return null + + return facade.elementFactory.createType( + arrayList, + substituteTypeParameter(lType, CommonClassNames.JAVA_UTIL_LIST, 0, false) + ) + } + if (isInheritor(lType, CommonClassNames.JAVA_UTIL_SET)) { + val set = facade.findClass("java.util.LinkedHashSet", scope) ?: + facade.findClass(CommonClassNames.JAVA_UTIL_SET, scope) ?: return null + + return facade.elementFactory.createType( + set, + substituteTypeParameter(lType, CommonClassNames.JAVA_UTIL_SET, 0, false) + ) + + } + return null } private fun getTupleType(expression: GrListOrMap): PsiType? { diff --git a/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/highlighting/GrAssignAutoTestFalsePositiveTest.groovy b/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/highlighting/GrAssignAutoTestFalsePositiveTest.groovy index 80f74a363aef..b19259410af0 100644 --- a/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/highlighting/GrAssignAutoTestFalsePositiveTest.groovy +++ b/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/highlighting/GrAssignAutoTestFalsePositiveTest.groovy @@ -19,7 +19,8 @@ class GrAssignAutoTestFalsePositiveTest extends GrHighlightingTestBase { List types = ['boolean', 'int', 'double', 'String', 'Integer', 'BigDecimal', 'BigInteger', 'List', 'Object', 'Thread', 'List', 'List', 'List', 'List', 'List', 'List', 'boolean[]', - 'int[]', 'double[]', 'String[]', 'Integer[]', 'List[]', 'Object[]', 'Thread[]', 'short', 'byte'] + 'int[]', 'double[]', 'String[]', 'Integer[]', 'List[]', 'Object[]', 'Thread[]', 'short', 'byte', 'Set', 'Set', + 'Set', 'Set', 'Set'] List values = ['true', '0', '1', '(int)1', '(short)1', '(byte)1', '1.1', '1.1d', '1.1f', '"1"', '["1"]', '1f', '"str"', 'null', 'new Object()', 'new Thread()', '[]' /*, '[1]', '[(int)1]', '[(byte)1]'*/, '[1.1]', '[1.1d]', '[1.1f]', '["str"]', 'new ArrayList<>()', @@ -59,7 +60,12 @@ class GrAssignAutoTestFalsePositiveTest extends GrHighlightingTestBase { 'List -> String[]', 'List -> Integer[]', 'List -> List[]', 'boolean[] -> int[]', 'boolean[] -> double[]', 'boolean[] -> String[]', 'int[] -> boolean[]', 'int[] -> String[]', 'double[] -> boolean[]', 'double[] -> String[]', 'String[] -> boolean[]', 'Integer[] -> boolean[]', 'Integer[] -> String[]', 'List[] -> boolean[]', 'List[] -> String[]', - 'Object[] -> boolean[]', 'Object[] -> String[]', 'Thread[] -> boolean[]', 'Thread[] -> String[]'] + 'Object[] -> boolean[]', 'Object[] -> String[]', 'Thread[] -> boolean[]', 'Thread[] -> String[]', 'Set -> boolean[]', + 'Set -> int[]', 'Set -> double[]', 'Set -> Integer[]', 'Set -> List[]', 'Set -> Thread[]', + 'Set -> boolean[]', 'Set -> String[]', 'Set -> List[]', 'Set -> Thread[]', + 'Set -> boolean[]', 'Set -> int[]', 'Set -> double[]', 'Set -> String[]', + 'Set -> Integer[]', 'Set -> List[]', 'Set -> Thread[]', 'Set -> boolean[]', + 'Set -> int[]', 'Set -> double[]', 'Set -> String[]', 'Set -> Integer[]', 'Set -> List[]'] }