diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/bugs/CastConflictsWithInstanceofInspection.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/bugs/CastConflictsWithInstanceofInspection.java index 832cb68f5c2e..9fefcc0d35cb 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/bugs/CastConflictsWithInstanceofInspection.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/bugs/CastConflictsWithInstanceofInspection.java @@ -1,5 +1,5 @@ /* - * Copyright 2003-2012 Dave Griffith, Bas Leijdekkers + * Copyright 2003-2016 Dave Griffith, Bas Leijdekkers * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -25,6 +25,7 @@ import com.siyeh.ig.BaseInspection; import com.siyeh.ig.BaseInspectionVisitor; import com.siyeh.ig.InspectionGadgetsFix; import com.siyeh.ig.psiutils.InstanceOfUtils; +import com.siyeh.ig.psiutils.ParenthesesUtils; import org.jetbrains.annotations.NotNull; public class CastConflictsWithInstanceofInspection extends BaseInspection { @@ -60,8 +61,7 @@ public class CastConflictsWithInstanceofInspection extends BaseInspection { return new CastConflictsWithInstanceofVisitor(); } - private static class CastConflictsWithInstanceofVisitor - extends BaseInspectionVisitor { + private static class CastConflictsWithInstanceofVisitor extends BaseInspectionVisitor { @Override public void visitTypeCastExpression(@NotNull PsiTypeCastExpression expression) { @@ -71,7 +71,7 @@ public class CastConflictsWithInstanceofInspection extends BaseInspection { return; } final PsiType type = castType.getType(); - final PsiExpression operand = expression.getOperand(); + final PsiExpression operand = ParenthesesUtils.stripParentheses(expression.getOperand()); if (!(operand instanceof PsiReferenceExpression)) { return; } @@ -108,7 +108,7 @@ public class CastConflictsWithInstanceofInspection extends BaseInspection { if (!"java.lang.Class".equals(qualifiedName)) { return; } - final PsiExpression qualifier = methodExpression.getQualifierExpression(); + final PsiExpression qualifier = ParenthesesUtils.stripParentheses(methodExpression.getQualifierExpression()); if (!(qualifier instanceof PsiClassObjectAccessExpression)) { return; } @@ -123,7 +123,7 @@ public class CastConflictsWithInstanceofInspection extends BaseInspection { if (arguments.length != 1) { return; } - final PsiExpression argument = arguments[0]; + final PsiExpression argument = ParenthesesUtils.stripParentheses(arguments[0]); if (!(argument instanceof PsiReferenceExpression)) { return; } @@ -138,7 +138,7 @@ public class CastConflictsWithInstanceofInspection extends BaseInspection { } } - private static abstract class ReplaceFix extends InspectionGadgetsFix { + private abstract static class ReplaceFix extends InspectionGadgetsFix { protected ReplaceFix() { } diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/bugs/CastConflictsWithInstanceofInspection.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/bugs/CastConflictsWithInstanceofInspection.java deleted file mode 100644 index a86a468f863a..000000000000 --- a/plugins/InspectionGadgets/test/com/siyeh/igtest/bugs/CastConflictsWithInstanceofInspection.java +++ /dev/null @@ -1,83 +0,0 @@ -package com.siyeh.igtest.bugs; - -import com.intellij.psi.PsiElement; -import com.intellij.psi.PsiReferenceExpression; -import com.intellij.psi.PsiStatement; - -import javax.swing.JButton; -import javax.swing.JComponent; -import java.awt.Component; -import java.awt.Frame; -import java.lang.Object; -import java.util.ArrayList; -import java.util.List; - -public class CastConflictsWithInstanceofInspection { - - public void foo() { - Number x = bar(); - - System.out.println((Double) x); - - if (x instanceof Float) { - System.out.println((Double) x); //warn here - } - } - - private Number bar() { - return null; - } - - void method(PsiElement p) { - if (p instanceof PsiReferenceExpression) { - PsiStatement stmt = (PsiStatement) p; // warn here - PsiReferenceExpression ref = (PsiReferenceExpression) p; // no warn - } else { - PsiStatement stmt = (PsiStatement) p; // no warn - } - } - - - boolean foo(Object o) { - if (o instanceof List) { - return !(o instanceof ArrayList) || ((ArrayList)o).get(0) == "asdf"; // no warn - } else if (o instanceof JButton) { - if (o instanceof Component) { - return ((JComponent)o).isBackgroundSet(); // no warn - } - } else if (o instanceof Component) { - return o instanceof Frame ? ((Frame)o).isFocusableWindow() : false; // no warn - } - return false; - } - - Object boom(Object feed, boolean includeDisabled) { - if (feed instanceof JComponent) - { - if (!(feed instanceof JButton) || - ((JButton)feed).isDefaultButton()) // no warn - { - return feed; - } - - } else { - String string = (String)feed; // no warn - } - return null; - } - - String baroom(Object o) { - if (o != null && !(o instanceof Integer)) { - return (String)o; // no warn - } - return null; - } - - public void test(Object arg) { - if (arg instanceof Byte || - arg instanceof Short && ((Short)arg).shortValue() == 0) { - // no warn - // ... - } - } -} \ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/bugs/cast_conflicts_with_instanceof/CastMethod.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/bugs/cast_conflicts_with_instanceof/CastMethod.java new file mode 100644 index 000000000000..7c9b0986a18f --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/bugs/cast_conflicts_with_instanceof/CastMethod.java @@ -0,0 +1,9 @@ +class CastMethod { + public void foo(Number x) { + System.out.println((Double) x); + + if (x instanceof Float) { + System.out.println((Double.class).cast((x))); + } + } +} \ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/bugs/cast_conflicts_with_instanceof/Simple.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/bugs/cast_conflicts_with_instanceof/Simple.java index 0334745f49a7..cc792d572de0 100644 --- a/plugins/InspectionGadgets/test/com/siyeh/igtest/bugs/cast_conflicts_with_instanceof/Simple.java +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/bugs/cast_conflicts_with_instanceof/Simple.java @@ -23,7 +23,7 @@ class U { } else { if (o instanceof A) { - B bb = (B)o; + B bb = (B)(o); } else { } } diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/bugs/CastConflictsWithInstanceofInspectionTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/bugs/CastConflictsWithInstanceofInspectionTest.java index 2876ea89c956..ad5be1cab90c 100644 --- a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/bugs/CastConflictsWithInstanceofInspectionTest.java +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/bugs/CastConflictsWithInstanceofInspectionTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2015 JetBrains s.r.o. + * Copyright 2000-2016 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. @@ -21,45 +21,17 @@ import org.jetbrains.annotations.Nullable; public class CastConflictsWithInstanceofInspectionTest extends LightInspectionTestCase { - public void testElseElse() { - doTest(); - } - - public void testSimple() { - doTest(); - } - - public void testElseElseOrOr() { - doTest(); - } - - public void testAndAnd() { - doTest(); - } - - public void testPolyadic() { - doTest(); - } - - public void testNotOr() { - doTest(); - } - - public void testOrInstanceofOrInstanceof() { - doTest(); - } - - public void testIfCheckBefore() { - doTest(); - } - - public void testIfElseCheckBefore() { - doTest(); - } - - public void testAssertCheckBefore() { - doTest(); - } + public void testElseElse() { doTest(); } + public void testSimple() { doTest(); } + public void testElseElseOrOr() { doTest(); } + public void testAndAnd() { doTest(); } + public void testPolyadic() { doTest(); } + public void testNotOr() { doTest(); } + public void testOrInstanceofOrInstanceof() { doTest(); } + public void testIfCheckBefore() { doTest(); } + public void testIfElseCheckBefore() { doTest(); } + public void testAssertCheckBefore() { doTest(); } + public void testCastMethod() { doTest(); } @Nullable @Override