IDEA-116758 ('equals()' between objects of inconvertible types - not working for ...= equals("differentClass"));

This commit is contained in:
Bas Leijdekkers
2013-11-20 16:43:11 +01:00
parent f14b604228
commit faa180a74f
4 changed files with 69 additions and 25 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 2003-2007 Dave Griffith, Bas Leijdekkers
* Copyright 2003-2013 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.
@@ -16,11 +16,13 @@
package com.siyeh.ig.bugs;
import com.intellij.psi.*;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.util.TypeConversionUtil;
import com.siyeh.InspectionGadgetsBundle;
import com.siyeh.ig.BaseInspection;
import com.siyeh.ig.BaseInspectionVisitor;
import com.siyeh.ig.psiutils.MethodCallUtils;
import com.siyeh.ig.psiutils.TypeUtils;
import org.jetbrains.annotations.NotNull;
public class EqualsBetweenInconvertibleTypesInspection
@@ -64,31 +66,34 @@ public class EqualsBetweenInconvertibleTypesInspection
if (!MethodCallUtils.isEqualsCall(expression)) {
return;
}
final PsiReferenceExpression methodExpression =
expression.getMethodExpression();
final PsiReferenceExpression methodExpression = expression.getMethodExpression();
final PsiExpressionList argumentList = expression.getArgumentList();
final PsiExpression[] args = argumentList.getExpressions();
if (args.length != 1) {
final PsiExpression[] arguments = argumentList.getExpressions();
if (arguments.length != 1) {
return;
}
final PsiExpression expression1 = args[0];
final PsiExpression expression2 =
methodExpression.getQualifierExpression();
final PsiExpression expression1 = arguments[0];
final PsiExpression expression2 = methodExpression.getQualifierExpression();
final PsiType comparisonType;
if (expression2 == null) {
final PsiClass aClass = PsiTreeUtil.getParentOfType(expression, PsiClass.class);
if (aClass == null) {
return;
}
comparisonType = TypeUtils.getType(aClass);
} else {
comparisonType = expression2.getType();
}
if (comparisonType == null) {
return;
}
final PsiType comparedType = expression1.getType();
if (comparedType == null) {
return;
}
final PsiType comparisonType = expression2.getType();
if (comparisonType == null) {
return;
}
final PsiType comparedTypeErasure = TypeConversionUtil.erasure(comparedType);
final PsiType comparisonTypeErasure = TypeConversionUtil.erasure(comparisonType);
if (comparedTypeErasure == null ||
comparisonTypeErasure == null ||
if (comparedTypeErasure == null || comparisonTypeErasure == null ||
TypeConversionUtil.areTypesConvertible(comparedTypeErasure, comparisonTypeErasure)) {
return;
}
@@ -38,6 +38,10 @@ public class TypeUtils {
return factory.createTypeByFQClassName(fqName, scope);
}
public static PsiClassType getType(@NotNull PsiClass aClass) {
return JavaPsiFacade.getInstance(aClass.getProject()).getElementFactory().createType(aClass);
}
public static PsiClassType getObjectType(@NotNull PsiElement context) {
return getType(CommonClassNames.JAVA_LANG_OBJECT, context);
}
@@ -1,11 +0,0 @@
package com.siyeh.igtest.bugs;
public class EqualsBetweenInconvertibleTypesInspection {
public void foo()
{
final Integer foo = new Integer(3);
final Double bar = new Double(3);
foo.equals(bar);
}
}
@@ -0,0 +1,46 @@
/*
* Copyright 2000-2013 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.siyeh.ig.bugs;
import com.intellij.codeInspection.InspectionProfileEntry;
import com.siyeh.ig.LightInspectionTestCase;
/**
* @author Bas Leijdekkers
*/
public class EqualsBetweenInconvertibleTypesInspectionTest extends LightInspectionTestCase {
public void testSimple() {
doMemberTest("public void foo() {\n" +
" final Integer foo = new Integer(3);\n" +
" final Double bar = new Double(3);\n" +
" foo./*'equals()' between objects of inconvertible types 'Double' and 'Integer'*/equals/**/(bar);\n" +
"}\n");
}
public void testWithoutQualifier() {
doTest("class Clazz {\n" +
" void foo() {\n" +
" boolean bar = /*'equals()' between objects of inconvertible types 'String' and 'Clazz'*/equals/**/(\"differentClass\");\n" +
" }\n" +
"}");
}
@Override
protected InspectionProfileEntry getInspection() {
return new EqualsBetweenInconvertibleTypesInspection();
}
}