From ad4c63bd1fedc1b65d9bdeafce7fe367f412a1d7 Mon Sep 17 00:00:00 2001 From: Bas Leijdekkers Date: Tue, 18 Apr 2017 23:02:55 +0200 Subject: [PATCH] fix false negative in "Redundant array creation" inspection --- .../RedundantArrayForVarargsCallInspection.java | 11 +++++++---- .../redundantArrayForVarargs/generic/expected.xml | 7 +++++++ .../redundantArrayForVarargs/generic/src/Test.java | 4 ++++ 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/java/java-impl/src/com/intellij/codeInspection/miscGenerics/RedundantArrayForVarargsCallInspection.java b/java/java-impl/src/com/intellij/codeInspection/miscGenerics/RedundantArrayForVarargsCallInspection.java index 07c9b046bada..0764b75a722d 100644 --- a/java/java-impl/src/com/intellij/codeInspection/miscGenerics/RedundantArrayForVarargsCallInspection.java +++ b/java/java-impl/src/com/intellij/codeInspection/miscGenerics/RedundantArrayForVarargsCallInspection.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2014 JetBrains s.r.o. + * 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. @@ -149,6 +149,7 @@ public class RedundantArrayForVarargsCallInspection extends GenericsInspectionTo if (callExpression instanceof PsiEnumConstant) { final PsiEnumConstant enumConstant = (PsiEnumConstant)callExpression; final PsiClass containingClass = enumConstant.getContainingClass(); + if (containingClass == null) return false; final JavaPsiFacade facade = JavaPsiFacade.getInstance(project); final PsiClassType classType = facade.getElementFactory().createType(containingClass); resolveResult = facade.getResolveHelper().resolveConstructor(classType, copyArgumentList, enumConstant); @@ -160,13 +161,15 @@ public class RedundantArrayForVarargsCallInspection extends GenericsInspectionTo return false; } final ExpectedTypeInfo[] expectedTypes = ExpectedTypesProvider.getExpectedTypes((PsiCallExpression)callExpression, false); + if (expectedTypes.length == 0) return true; final PsiType expressionType = ((PsiCallExpression)copy).getType(); + if (expressionType == null) return false; for (ExpectedTypeInfo expectedType : expectedTypes) { - if (!expectedType.getType().isAssignableFrom(expressionType)) { - return false; + if (expectedType.getType().isAssignableFrom(expressionType)) { + return true; } } - return true; + return false; } } catch (IncorrectOperationException e) { diff --git a/java/java-tests/testData/inspection/redundantArrayForVarargs/generic/expected.xml b/java/java-tests/testData/inspection/redundantArrayForVarargs/generic/expected.xml index 403962e9c909..1a4164ee3399 100644 --- a/java/java-tests/testData/inspection/redundantArrayForVarargs/generic/expected.xml +++ b/java/java-tests/testData/inspection/redundantArrayForVarargs/generic/expected.xml @@ -6,4 +6,11 @@ Redundant array creation Redundant array creation for calling varargs method + + + Test.java + 20 + Redundant array creation + Redundant array creation for calling varargs method + \ No newline at end of file diff --git a/java/java-tests/testData/inspection/redundantArrayForVarargs/generic/src/Test.java b/java/java-tests/testData/inspection/redundantArrayForVarargs/generic/src/Test.java index 9f38ddce9cd9..b90af553ffa6 100644 --- a/java/java-tests/testData/inspection/redundantArrayForVarargs/generic/src/Test.java +++ b/java/java-tests/testData/inspection/redundantArrayForVarargs/generic/src/Test.java @@ -15,4 +15,8 @@ class Test { } class C {} + + void m() { + System.out.println(String.format("%s %s", new Object[] {"Z", "X"})); + } }