diff options
author | Jeff Johnston | 2020-11-09 20:58:03 +0000 |
---|---|---|
committer | Jeff Johnston | 2020-11-13 19:01:24 +0000 |
commit | 049f834d0ef4005792049b71c02e98e8112e9b62 (patch) | |
tree | fa471c5e5c20ab91208120bf01f044d98946c725 | |
parent | e03c83842794728f70fc805c9d4f2bde43cd4e48 (diff) | |
download | eclipse.jdt.core-049f834d0ef4005792049b71c02e98e8112e9b62.tar.gz eclipse.jdt.core-049f834d0ef4005792049b71c02e98e8112e9b62.tar.xz eclipse.jdt.core-049f834d0ef4005792049b71c02e98e8112e9b62.zip |
Bug 561167 - "Source > Cleanup" removes necessary cast of nullI20201115-1800I20201115-0600I20201114-1800I20201114-0740I20201114-0600I20201113-1800
- add setVarTypeDeclaration() method to CastExpression to
indicate when CastExpression is the initialization of a var type
- add logic in CastExpression to disable unnecessary cast checks
if we are initializing a var type variable and the type of the cast
and expression are not equal
- add logic in LocalDeclaration to call
CastExpression.setVarTypeDeclaration() when appropriate
- add new test to CastTest
Change-Id: I43ac031a69796a14be3a00fc2656becec5b38b81
Signed-off-by: Jeff Johnston <jjohnstn@redhat.com>
3 files changed, 49 insertions, 0 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 45f4e9e41e..c979cdf012 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 @@ -3532,6 +3532,44 @@ public void test472466() { runner.runWarningTest(); } +public void testBug561167() { + if (this.complianceLevel < ClassFileConstants.JDK10) + return; + Map customOptions = getCompilerOptions(); + customOptions.put(CompilerOptions.OPTION_ReportUnnecessaryTypeCheck, CompilerOptions.ERROR); + runNegativeTest( + // test directory preparation + true /* flush output directory */, + new String[] { /* test files */ + "X.java", + "public class X {\n" + + " public static void main(String[] args) {\n" + + " var s = (String) null; // Necessary\n" + + " var t = (String) \"hello\"; // UNnecessary\n" + + " var f = (float) 12; // Necessary\n" + + " var g = (float)f; // UNnecessary\n" + + " }\n" + + "}\n" + }, + // compiler options + null /* no class libraries */, + customOptions /* custom options */, + // compiler results + "----------\n" + + "1. ERROR in X.java (at line 4)\n" + + " var t = (String) \"hello\"; // UNnecessary\n" + + " ^^^^^^^^^^^^^^^^\n" + + "Unnecessary cast from String to String\n" + + "----------\n" + + "2. ERROR in X.java (at line 6)\n" + + " var g = (float)f; // UNnecessary\n" + + " ^^^^^^^^\n" + + "Unnecessary cast from float to float\n" + + "----------\n", + // javac options + JavacTestOptions.Excuse.EclipseWarningConfiguredAsError /* javac test options */); +} + public static Class testClass() { return CastTest.class; } diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/CastExpression.java b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/CastExpression.java index 38fa4fccbe..034482324c 100644 --- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/CastExpression.java +++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/CastExpression.java @@ -65,6 +65,7 @@ public class CastExpression extends Expression { public TypeReference type; public TypeBinding expectedType; // when assignment conversion to a given expected type: String s = (String) t; public TypeBinding instanceofType; // set by InstanceofExpression to ensure we don't flag a necessary cast unnecessary + public boolean isVarTypeDeclaration; // set by LocalDeclaration to indicate we are initializing a var type declaration //expression.implicitConversion holds the cast for baseType casting public CastExpression(Expression expression, TypeReference type) { @@ -646,6 +647,9 @@ public TypeBinding resolveType(BlockScope scope) { && expressionType.isProvablyDistinct(this.instanceofType)) { this.bits |= ASTNode.DisableUnnecessaryCastCheck; } + if (this.isVarTypeDeclaration && TypeBinding.notEquals(expressionType, castType)) { + this.bits |= ASTNode.DisableUnnecessaryCastCheck; + } boolean isLegal = checkCastTypesCompatibility(scope, castType, expressionType, this.expression, true); if (isLegal) { this.expression.computeConversion(scope, castType, expressionType); @@ -731,6 +735,10 @@ public void setInstanceofType(TypeBinding instanceofTypeBinding) { this.instanceofType = instanceofTypeBinding; } +public void setVarTypeDeclaration(boolean value) { + this.isVarTypeDeclaration = value; +} + @Override public void traverse(ASTVisitor visitor, BlockScope blockScope) { if (visitor.visit(this, blockScope)) { diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/LocalDeclaration.java b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/LocalDeclaration.java index 0b6454b354..64c1c9a7b3 100644 --- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/LocalDeclaration.java +++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/LocalDeclaration.java @@ -340,6 +340,9 @@ public FlowInfo analyseCode(BlockScope currentScope, FlowContext flowContext, Fl if (variableType == null) { if (this.initialization != null) { + if (this.initialization instanceof CastExpression) { + ((CastExpression)this.initialization).setVarTypeDeclaration(true); + } this.initialization.resolveType(scope); // want to report all possible errors if (isTypeNameVar && this.initialization.resolvedType != null) { if (TypeBinding.equalsEquals(TypeBinding.NULL, this.initialization.resolvedType)) { |