IG: ignore parentheses

This commit is contained in:
Bas Leijdekkers
2016-02-10 21:06:03 +01:00
parent 181e8a993b
commit 3f14ebaa69
5 changed files with 29 additions and 131 deletions
@@ -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() {
}
@@ -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
// ...
}
}
}
@@ -0,0 +1,9 @@
class CastMethod {
public void foo(Number x) {
System.out.println((Double) x);
if (x instanceof Float) {
System.out.println(<warning descr="Cast '(Double.class).cast((x))' conflicts with surrounding 'instanceof' check">(Double.class).cast((x))</warning>);
}
}
}
@@ -23,7 +23,7 @@ class U {
} else {
if (o instanceof A) {
B bb = <warning descr="Cast '(B)o' conflicts with surrounding 'instanceof' check">(B)o</warning>;
B bb = <warning descr="Cast '(B)(o)' conflicts with surrounding 'instanceof' check">(B)(o)</warning>;
} else {
}
}
@@ -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