diff --git a/java/java-psi-api/src/com/intellij/psi/GenericsUtil.java b/java/java-psi-api/src/com/intellij/psi/GenericsUtil.java index db383980fef4..fe99a81e58a7 100644 --- a/java/java-psi-api/src/com/intellij/psi/GenericsUtil.java +++ b/java/java-psi-api/src/com/intellij/psi/GenericsUtil.java @@ -468,8 +468,17 @@ public final class GenericsUtil { //Given a generic type declaration C (n > 0), the direct supertypes of the parameterized type C where at least one of the Ri is a wildcard //type argument, are the direct supertypes of the parameterized type C which is the result of applying capture conversion to C. PsiType capturedType = PsiUtil.captureToplevelWildcards(type, referenceParameterList); - //allow unchecked conversions in method calls but not in type declaration - return checkNotInBounds(capturedType, bound, PsiTreeUtil.getParentOfType(referenceParameterList, PsiCallExpression.class) != null); + //allow unchecked conversions in method calls, new expression args, or in diamond types, but not in other places + boolean uncheckedConversionByDefault; + PsiElement parent = referenceParameterList.getParent(); + if (parent instanceof PsiReferenceExpression || parent instanceof PsiNewExpression) { + uncheckedConversionByDefault = true; + } + else { + PsiTypeElement[] elements = referenceParameterList.getTypeParameterElements(); + uncheckedConversionByDefault = elements.length == 1 && elements[0].getType() instanceof PsiDiamondType; + } + return checkNotInBounds(capturedType, bound, uncheckedConversionByDefault); } public static boolean checkNotInBounds(PsiType type, PsiType bound, boolean uncheckedConversionByDefault) { diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/genericsHighlighting/CaptureForBoundCheck.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/genericsHighlighting/CaptureForBoundCheck.java index 323840286822..ea89adaa4ef8 100644 --- a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/genericsHighlighting/CaptureForBoundCheck.java +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/genericsHighlighting/CaptureForBoundCheck.java @@ -17,4 +17,12 @@ class EnumBug { EnumSet<Option> enumSetRaw = EnumSet.