cast conflict should not be reported when additional check is performed in the same binary expr

This commit is contained in:
Anna Kozlova
2012-06-06 18:35:38 +04:00
parent 4593b16369
commit 683ad6299f
4 changed files with 23 additions and 4 deletions
@@ -113,6 +113,9 @@ public class InstanceOfUtils {
}
else if (tokenType == JavaTokenType.OROR) {
for (PsiExpression operand : expression.getOperands()) {
if (operand instanceof PsiPrefixExpression && ((PsiPrefixExpression)operand).getOperationTokenType() == JavaTokenType.EXCL) {
inElse = true;
}
checkExpression(operand);
}
if (inElse && conflictingInstanceof != null) {
@@ -125,8 +128,7 @@ public class InstanceOfUtils {
public void visitIfStatement(PsiIfStatement ifStatement) {
final PsiStatement branch = ifStatement.getElseBranch();
inElse = branch != null &&
PsiTreeUtil.isAncestor(branch, referenceExpression,
true);
PsiTreeUtil.isAncestor(branch, referenceExpression, true);
if (inElse) {
if (branch instanceof PsiBlockStatement) {
final PsiBlockStatement blockStatement =
@@ -161,8 +163,7 @@ public class InstanceOfUtils {
}
@Override
public void visitConditionalExpression(
PsiConditionalExpression expression) {
public void visitConditionalExpression(PsiConditionalExpression expression) {
final PsiExpression elseExpression =
expression.getElseExpression();
inElse = elseExpression != null &&
@@ -0,0 +1,11 @@
interface I1 {}
interface I2 extends I1{}
interface I3 extends I1 {}
class PP {
void f(Object o) {
if (o instanceof I1) {
if (!(o instanceof I2) || ((I2)o).getClass() != null){}
}
}
}
@@ -0,0 +1,3 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
</problems>
@@ -38,6 +38,10 @@ public class CastConflictsWithInstanceofInspectionTest extends IGInspectionTestC
public void testPolyadic() throws Exception {
doTest();
}
public void testNotOr() throws Exception {
doTest();
}
private void doTest() throws Exception {
doTest("com/siyeh/igtest/bugs/castConflictingInstanceof/" + getTestName(true), new CastConflictsWithInstanceofInspection());