lambda: do not mark cast redundant when functional interface won't be found otherwise

This commit is contained in:
Anna Kozlova
2012-09-03 18:27:57 +04:00
parent f324594bc3
commit 70299ca094
5 changed files with 45 additions and 5 deletions
@@ -250,7 +250,7 @@ public class LambdaUtil {
return null;
}
public static int getLambdaIdx(PsiExpressionList expressionList, final PsiLambdaExpression element) {
public static int getLambdaIdx(PsiExpressionList expressionList, final PsiElement element) {
PsiExpression[] expressions = expressionList.getExpressions();
for (int i = 0; i < expressions.length; i++) {
PsiExpression expression = expressions[i];
@@ -329,7 +329,7 @@ public class LambdaUtil {
}
@Nullable
public static PsiType getFunctionalInterfaceType(PsiLambdaExpression expression, final boolean tryToSubstitute) {
public static PsiType getFunctionalInterfaceType(PsiElement expression, final boolean tryToSubstitute) {
PsiElement parent = expression.getParent();
while (parent instanceof PsiParenthesizedExpression) {
parent = parent.getParent();
@@ -0,0 +1,3 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
</problems>
@@ -0,0 +1,31 @@
/*
* Copyright 2000-2012 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.
*/
public class Test {
interface I {
}
interface Bar extends I {
int compare(String o1, String o2);
}
{
I bar2 = (Bar) (o1, o2) -> 0;
g((Bar) (o1, o2) -> 0);
((Bar) (o1, o2) -> 0).getClass();
}
void g(I i){}
}
@@ -33,6 +33,7 @@ public class RedundantCast18Test extends InspectionTestCase {
}
public void testLambdaContext() throws Exception { doTest(); }
public void testExpectedSupertype() throws Exception { doTest(); }
protected Sdk getTestProjectSdk() {
Sdk sdk = JavaSdkImpl.getMockJdk17();
@@ -362,13 +362,13 @@ public class RedundantCastUtil {
PsiElement expr = deparenthesizeExpression(operand);
final PsiType topCastType = typeCast.getType();
if (expr instanceof PsiTypeCastExpression) {
PsiTypeElement typeElement = ((PsiTypeCastExpression)expr).getCastType();
if (typeElement == null) return;
PsiType castType = typeElement.getType();
final PsiExpression innerOperand = ((PsiTypeCastExpression)expr).getOperand();
final PsiType operandType = innerOperand != null ? innerOperand.getType() : null;
final PsiType topCastType = typeCast.getType();
if (!(castType instanceof PsiPrimitiveType)) {
if (operandType != null && topCastType != null && TypeConversionUtil.areTypesConvertible(operandType, topCastType)) {
addToResults((PsiTypeCastExpression)expr);
@@ -389,8 +389,13 @@ public class RedundantCastUtil {
}
} else if (parent instanceof PsiSynchronizedStatement && (expr instanceof PsiExpression && ((PsiExpression)expr).getType() instanceof PsiPrimitiveType)) {
return;
} else if (expr instanceof PsiLambdaExpression && parent instanceof PsiParenthesizedExpression && parent.getParent() instanceof PsiReferenceExpression) {
return;
} else if (expr instanceof PsiLambdaExpression) {
if (parent instanceof PsiParenthesizedExpression && parent.getParent() instanceof PsiReferenceExpression) {
return;
}
final PsiType functionalInterfaceType = LambdaUtil.getFunctionalInterfaceType(typeCast, true);
if (topCastType != null && functionalInterfaceType != null && !TypeConversionUtil.isAssignable(topCastType, functionalInterfaceType, false)) return;
}
processAlreadyHasTypeCast(typeCast);
}