[java-inspections] RawUseOfParameterizedTypeInspection: handle null expected type

Fixes EA-923716 - AE: BasicJavaParserUtil.parseFragment

GitOrigin-RevId: 37fd5e8d2e6504ca924ac09f6316548530a4952c
This commit is contained in:
Tagir Valeev
2023-10-13 16:21:26 +02:00
committed by intellij-monorepo-bot
parent ce933b41d7
commit ea2e768ce2
3 changed files with 14 additions and 1 deletions

View File

@@ -115,7 +115,8 @@ public class RawUseOfParameterizedTypeInspection extends BaseInspection {
if (!PsiUtil.isLanguageLevel7OrHigher(parent)) return null;
if (newExpression.isArrayCreation() || newExpression.getAnonymousClass() != null) return null;
PsiType expectedType = ExpectedTypeUtils.findExpectedType(newExpression, false);
if (expectedType == null || (expectedType instanceof PsiClassType && ((PsiClassType)expectedType).isRaw())) return null;
if (expectedType == null || expectedType.equals(PsiTypes.nullType()) ||
(expectedType instanceof PsiClassType && ((PsiClassType)expectedType).isRaw())) return null;
PsiNewExpression copy = (PsiNewExpression)LambdaUtil.copyWithExpectedType(parent, expectedType);
PsiJavaCodeReferenceElement reference = copy.getClassReference();
if (reference == null) return null;

View File

@@ -0,0 +1,8 @@
import java.util.ArrayList;
class TestNullType {
void test() {
<error descr="Cannot infer type: variable initializer is 'null'">var</error> x = null;
x = new <warning descr="Raw use of parameterized class 'ArrayList'">ArrayList</warning>();
}
}

View File

@@ -18,6 +18,10 @@ public class RawUseOfParameterizedTypeInspectionTest extends LightJavaInspection
public void testRawUseOfParameterizedType() {
doTest();
}
public void testInvalidType() {
doTest();
}
public void testIgnoreWhenQuickFixNotAvailable() {
final RawUseOfParameterizedTypeInspection inspection = (RawUseOfParameterizedTypeInspection)getInspection();