Skip to content

Commit

Permalink
* Instanceof with record deconstruction patterns should never be flagged
Browse files Browse the repository at this point in the history
as unnecessary
* Fixes #1999
  • Loading branch information
srikanth-sankaran committed Mar 5, 2024
1 parent 279c915 commit 00748e7
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,9 @@ public boolean checkUnsafeCast(Scope scope, TypeBinding castType, TypeBinding ex

@Override
public void tagAsUnnecessaryCast(Scope scope, TypeBinding castType) {
// record deconstruction and binding type pattern variable are legitimate operations.
if (this.pattern != null)
return;
// null is not instanceof Type, recognize direct scenario
if (this.expression.resolvedType != TypeBinding.NULL)
scope.problemReporter().unnecessaryInstanceof(this, castType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4428,4 +4428,49 @@ public static void main(String[] args) {
"Record component with type short is not compatible with type int\n" +
"----------\n");
}
// https://github.com/eclipse-jdt/eclipse.jdt.core/issues/1999
// [Patterns][records] Instanceof with record deconstruction patterns should never be flagged as unnecessary
public void testIssue1999() {
Map<String, String> options = getCompilerOptions();
options.put(CompilerOptions.OPTION_ReportUnnecessaryTypeCheck, CompilerOptions.ERROR);
runNegativeTest(new String[] {
"X.java",
"""
interface I {
}
final class A implements I {
}
final class B implements I {
}
record R(I x, I y) {
}
public class X {
public static boolean foo(R r) {
if (r instanceof R(A a1, A a2)) // don't warn here.
return true;
A a = null;
if (a instanceof A) {} // warn here
if (a instanceof A a1) {} // don't warn here
return false;
}
public static void main(String argv[]) {
System.out.println(X.foo(new R(new A(), new A())));
System.out.println(X.foo(new R(new A(), new B())));
}
}
"""
},
"----------\n" +
"1. ERROR in X.java (at line 18)\n" +
" if (a instanceof A) {} // warn here\n" +
" ^^^^^^^^^^^^^^\n" +
"The expression of type A is already an instance of type A\n" +
"----------\n",
null, true, options);
}
}

0 comments on commit 00748e7

Please sign in to comment.