add javac quirks case for object-primitive comparison (IDEA-168944)

This commit is contained in:
Anna.Kozlova
2017-03-17 16:22:00 +01:00
parent aaad1a84e9
commit 2159d803f8
2 changed files with 21 additions and 2 deletions

View File

@@ -145,6 +145,25 @@ public class JavacQuirksInspectionVisitor extends JavaElementVisitor {
}
}
@Override
public void visitBinaryExpression(PsiBinaryExpression expression) {
super.visitBinaryExpression(expression);
if (myLanguageLevel.isAtLeast(LanguageLevel.JDK_1_7) && !myLanguageLevel.isAtLeast(LanguageLevel.JDK_1_8)) {
PsiType ltype = expression.getLOperand().getType();
PsiExpression rOperand = expression.getROperand();
if (rOperand != null) {
PsiType rtype = rOperand.getType();
if (ltype != null && rtype != null &&
(ltype.equalsToText(CommonClassNames.JAVA_LANG_OBJECT) ^ rtype.equalsToText(CommonClassNames.JAVA_LANG_OBJECT)) &&
(TypeConversionUtil.isPrimitiveAndNotNull(ltype) ^ TypeConversionUtil.isPrimitiveAndNotNull(rtype)) &&
TypeConversionUtil.isBinaryOperatorApplicable(expression.getOperationTokenType(), ltype, rtype, false) &&
TypeConversionUtil.areTypesConvertible(rtype, ltype)) {
myHolder.registerProblem(expression.getOperationSign(), "Comparision between Object and primitive is illegal and is accepted in java 7 only", ProblemHighlightType.GENERIC_ERROR_OR_WARNING);
}
}
}
}
private static class ReplaceAssignmentOperatorWithAssignmentFix implements LocalQuickFix {
private final String myOperationSign;

View File

@@ -32,7 +32,7 @@ class Test {
System.out.println((<warning descr="Casting 'c1' to 'char' is redundant">char</warning>) c1 == (char) o);
// Although a reference comparison, the cast on the wrapper has a side effect; not redundant.
System.out.println(o == (char) c1);
System.out.println((char) c1 == o);
System.out.println(o <warning descr="Comparision between Object and primitive is illegal and is accepted in java 7 only">==</warning> (char) c1);
System.out.println((char) c1 <warning descr="Comparision between Object and primitive is illegal and is accepted in java 7 only">==</warning> o);
}
}