Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJeff Johnston2019-01-24 15:44:07 -0500
committerStephan Herrmann2019-02-07 15:13:11 -0500
commit56c7200f17bcd06f03618a81316a7dbfe1d7dcf8 (patch)
tree87cd95708160d891a81b66b36b725bb9c5591a5a
parentbcec7451e941decddf86ddcdbe87a1f9b59eec30 (diff)
downloadeclipse.jdt.core-56c7200f17bcd06f03618a81316a7dbfe1d7dcf8.tar.gz
eclipse.jdt.core-56c7200f17bcd06f03618a81316a7dbfe1d7dcf8.tar.xz
eclipse.jdt.core-56c7200f17bcd06f03618a81316a7dbfe1d7dcf8.zip
Bug 543727 - False positive "Unnecessary cast"I20190207-1800
- change EqualExpression.resolveType() to add additional check before reporting an unnecessary cast if one side is parameterized and the other is a cast of a base type that may be compatible with parameterized type when boxed - add test to CastTest Change-Id: I9e3e1be9a0d42965dba9c8dbf629171248d0b26b Signed-off-by: Jeff Johnston <jjohnstn@redhat.com>
-rw-r--r--org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/CastTest.java50
-rw-r--r--org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/EqualExpression.java27
2 files changed, 71 insertions, 6 deletions
diff --git a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/CastTest.java b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/CastTest.java
index e723cdbad6..e0e23ca878 100644
--- a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/CastTest.java
+++ b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/CastTest.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2003, 2017 IBM Corporation and others.
+ * Copyright (c) 2003, 2019 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
@@ -3297,6 +3297,54 @@ public void testAnonymous_bug520727() {
runConformTest(source,"");
}
}
+//https://bugs.eclipse.org/bugs/show_bug.cgi?id=543727 False positive "Unnecessary cast"
+public void test543727() {
+ if (this.complianceLevel < ClassFileConstants.JDK1_7)
+ return;
+ Map customOptions = getCompilerOptions();
+ customOptions.put(CompilerOptions.OPTION_ReportUnnecessaryTypeCheck, CompilerOptions.ERROR);
+ this.runConformTest(
+ new String[] {
+ "Bug.java",
+ "import java.util.ArrayList;\n" +
+ "import java.util.List;\n" +
+ "public class Bug {\n" +
+ " public static int main(String[] args) {\n" +
+ " List<Comparable<?>> vector = new ArrayList<>();\n" +
+ " vector.add(0);\n" +
+ " if (vector.get(0) == (Integer)0) {\n" +
+ " System.out.print(\"SUCCESS\");\n" +
+ " }\n" +
+ " return 0;\n" +
+ " }" +
+ "}\n",
+ },
+ "SUCCESS");
+}
+public void test543727_notequals() {
+ if (this.complianceLevel < ClassFileConstants.JDK1_7)
+ return;
+ Map customOptions = getCompilerOptions();
+ customOptions.put(CompilerOptions.OPTION_ReportUnnecessaryTypeCheck, CompilerOptions.ERROR);
+ this.runConformTest(
+ new String[] {
+ "Bug.java",
+ "import java.util.ArrayList;\n" +
+ "import java.util.List;\n" +
+ "public class Bug {\n" +
+ " public static int main(String[] args) {\n" +
+ " List<Comparable<?>> vector = new ArrayList<>();\n" +
+ " vector.add(0);\n" +
+ " if (vector.get(0) != (Integer)1) {\n" +
+ " System.out.print(\"SUCCESS\");\n" +
+ " }\n" +
+ " return 0;\n" +
+ " }" +
+ "}\n",
+ },
+ "SUCCESS");
+}
+
public static Class testClass() {
return CastTest.class;
}
diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/EqualExpression.java b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/EqualExpression.java
index c3bbd55075..bdf1ed81fe 100644
--- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/EqualExpression.java
+++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/EqualExpression.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2000, 2018 IBM Corporation and others.
+ * Copyright (c) 2000, 2019 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
@@ -936,10 +936,13 @@ public class EqualExpression extends BinaryExpression {
if (unnecessaryLeftCast || unnecessaryRightCast) {
TypeBinding alternateLeftType = unnecessaryLeftCast ? ((CastExpression)this.left).expression.resolvedType : leftType;
TypeBinding alternateRightType = unnecessaryRightCast ? ((CastExpression)this.right).expression.resolvedType : rightType;
- if (checkCastTypesCompatibility(scope, alternateLeftType, alternateRightType, null)
- || checkCastTypesCompatibility(scope, alternateRightType, alternateLeftType, null)) {
- if (unnecessaryLeftCast) scope.problemReporter().unnecessaryCast((CastExpression)this.left);
- if (unnecessaryRightCast) scope.problemReporter().unnecessaryCast((CastExpression)this.right);
+ // Bug 543727 - check if either cast is really needed
+ if (!isCastNeeded(alternateLeftType, alternateRightType)) {
+ if (checkCastTypesCompatibility(scope, alternateLeftType, alternateRightType, null)
+ || checkCastTypesCompatibility(scope, alternateRightType, alternateLeftType, null)) {
+ if (unnecessaryLeftCast) scope.problemReporter().unnecessaryCast((CastExpression)this.left);
+ if (unnecessaryRightCast) scope.problemReporter().unnecessaryCast((CastExpression)this.right);
+ }
}
}
// check whether comparing identical expressions
@@ -955,6 +958,20 @@ public class EqualExpression extends BinaryExpression {
scope.problemReporter().notCompatibleTypesError(this, leftType, rightType);
return null;
}
+
+ private boolean isCastNeeded(TypeBinding leftType, TypeBinding rightType) {
+ // Bug 543727 - if either type is parameterized and the other is a base type,
+ // a cast is necessary, even if boxing the base type will result in a compatible
+ // type.
+ if (leftType.isParameterizedType()) {
+ return rightType.isBaseType();
+ }
+ if (rightType.isParameterizedType()) {
+ return leftType.isBaseType();
+ }
+ return false;
+ }
+
@Override
public void traverse(ASTVisitor visitor, BlockScope scope) {
if (visitor.visit(this, scope)) {

Back to the top