java redundant cast: allow warnings in poly conditional when target type remains the same

GitOrigin-RevId: f953fc42882a1eaad0b0a8c65d1e94236f0ec99e
This commit is contained in:
Anna Kozlova
2020-05-05 12:31:04 +02:00
committed by intellij-monorepo-bot
parent b4993721fb
commit 349c96eaee
2 changed files with 7 additions and 5 deletions

View File

@@ -13,7 +13,6 @@ import com.intellij.psi.codeStyle.JavaCodeStyleManager;
import com.intellij.psi.impl.source.resolve.graphInference.PsiPolyExpressionUtil;
import com.intellij.psi.infos.MethodCandidateInfo;
import com.intellij.psi.tree.IElementType;
import com.intellij.util.ArrayUtil;
import com.intellij.util.Function;
import com.intellij.util.IncorrectOperationException;
import com.siyeh.ig.psiutils.ExpectedTypeUtils;
@@ -509,10 +508,12 @@ public class RedundantCastUtil {
return;
}
if (!checkResolveAfterRemoveCast(parent)) return;
final PsiExpression thenExpression = ((PsiConditionalExpression)parent).getThenExpression();
final PsiExpression elseExpression = ((PsiConditionalExpression)parent).getElseExpression();
final PsiExpression opposite = thenExpression == typeCast ? elseExpression : thenExpression;
if (opposite == null || !Comparing.equal(conditionalType, opposite.getType())) return;
if (!PsiPolyExpressionUtil.isPolyExpression((PsiExpression)parent)) {
final PsiExpression thenExpression = ((PsiConditionalExpression)parent).getThenExpression();
final PsiExpression elseExpression = ((PsiConditionalExpression)parent).getElseExpression();
final PsiExpression opposite = thenExpression == typeCast ? elseExpression : thenExpression;
if (opposite == null || !Comparing.equal(conditionalType, opposite.getType())) return;
}
}
}
else if (parent instanceof PsiSynchronizedStatement &&

View File

@@ -5,6 +5,7 @@ class Test {
Object o = true ? ((Supplier<String>) () -> "") : null;
Supplier<String> s1 = true ? ((<warning descr="Casting '() -> {...}' to 'Supplier<String>' is redundant">Supplier<String></warning>) () -> "") : null;
Supplier<String> s2 = true ? ((A) () -> "") : null;
A s3 = true ? ((<warning descr="Casting '() -> {...}' to 'A' is redundant">A</warning>) () -> "") : null;
}
interface A extends Supplier<String> {}