redundant cast: ignore for lambda casts with serializable/intersections (IDEA-105839)

This commit is contained in:
anna
2013-04-22 18:00:30 +02:00
parent 5616d066c5
commit d67a21f788
4 changed files with 92 additions and 0 deletions
@@ -0,0 +1,14 @@
import java.io.*;
interface Predicate<T> {
boolean test(String s);
}
class Test {
private static boolean test(String s) {
return false;
}
{
Predicate<String> mh1 = (Predicate<String> & Serializable)Test::test;
Predicate<String> mh0 = (<warning descr="Casting 'Test::test' to 'Predicate<String> & Predicate<String>' is redundant">Predicate<String> & Predicate<String></warning>)Test::test;
}
}
@@ -0,0 +1,20 @@
import java.io.*;
interface Predicate<T> {
boolean test(String s);
}
interface SerPredicate<T> extends Predicate<T>, Serializable {
}
interface NonSerPredicate<T> extends Predicate<T> {
}
class Test {
private static boolean test(String s) {
return false;
}
{
Predicate<String> mh2 = (SerPredicate<String>)Test::test;
Predicate<String> mh02 = (<warning descr="Casting 'Test::test' to 'NonSerPredicate<String>' is redundant">NonSerPredicate<String></warning>)Test::test;
}
}
@@ -0,0 +1,40 @@
/*
* Copyright 2000-2013 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.
*/
package com.intellij.codeInsight.daemon.lambda;
import com.intellij.codeInsight.daemon.LightDaemonAnalyzerTestCase;
import com.intellij.codeInspection.LocalInspectionTool;
import com.intellij.codeInspection.redundantCast.RedundantCastInspection;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
public class LambdaRedundantCastTest extends LightDaemonAnalyzerTestCase {
@NonNls static final String BASE_PATH = "/codeInsight/daemonCodeAnalyzer/lambda/redundantCast";
@NotNull
@Override
protected LocalInspectionTool[] configureLocalInspectionTools() {
return new LocalInspectionTool[]{
new RedundantCastInspection()
};
}
public void testIntersection() { doTest(); }
public void testSer() { doTest(); }
private void doTest() {
doTest(BASE_PATH + "/" + getTestName(false) + ".java", true, false);
}
}
@@ -545,6 +545,24 @@ public class RedundantCastUtil {
if (opType instanceof PsiClassType && ((PsiClassType)opType).hasParameters()) return true;
}
if (operand instanceof PsiLambdaExpression || operand instanceof PsiMethodReferenceExpression) {
if (castType instanceof PsiClassType && InheritanceUtil.isInheritor(PsiUtil.resolveClassInType(castType), CommonClassNames.JAVA_IO_SERIALIZABLE)) return true;
if (castType instanceof PsiIntersectionType) {
boolean redundant = false;
final PsiType[] conjuncts = ((PsiIntersectionType)castType).getConjuncts();
for (int i = 1; i < conjuncts.length; i++) {
PsiType conjunct = conjuncts[i];
if (TypeConversionUtil.isAssignable(conjuncts[0], conjunct)) {
redundant = true;
break;
}
}
if (!redundant) {
return true;
}
}
}
PsiElement parent = typeCast.getParent();
while(parent instanceof PsiParenthesizedExpression) parent = parent.getParent();