-
Bug
-
Resolution: Not an Issue
-
P4
-
None
-
21
-
generic
-
generic
ADDITIONAL SYSTEM INFORMATION :
Reproducible using java 17.0.10, 21.0.5, and 23.0.1 on both Lubuntu and Windows 11
A DESCRIPTION OF THE PROBLEM :
When using a pattern matching instanceof and return or throw exception if the result is false. you still can access that variable outside the scope.
The key here is the return statement if the matching failed. you will get the expected compilation error when that return statement get removed.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Return if the pattern matching failed, then you can access the pattern matching variable outside the scope
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Compilation error on executing ` System.out.println("not cool : " + b);` because the b is not in the scope.
ACTUAL -
Compiles successfully and the output is:
cool : false
not cool : false
---------- BEGIN SOURCE ----------
public static void main(String[] args) {
Object object = Boolean.FALSE;
if (object instanceof Boolean b) {
System.out.println("cool : " + b);
} else {
return;
}
System.out.println("not cool : " + b);
}
---------- END SOURCE ----------
FREQUENCY : always
Reproducible using java 17.0.10, 21.0.5, and 23.0.1 on both Lubuntu and Windows 11
A DESCRIPTION OF THE PROBLEM :
When using a pattern matching instanceof and return or throw exception if the result is false. you still can access that variable outside the scope.
The key here is the return statement if the matching failed. you will get the expected compilation error when that return statement get removed.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Return if the pattern matching failed, then you can access the pattern matching variable outside the scope
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Compilation error on executing ` System.out.println("not cool : " + b);` because the b is not in the scope.
ACTUAL -
Compiles successfully and the output is:
cool : false
not cool : false
---------- BEGIN SOURCE ----------
public static void main(String[] args) {
Object object = Boolean.FALSE;
if (object instanceof Boolean b) {
System.out.println("cool : " + b);
} else {
return;
}
System.out.println("not cool : " + b);
}
---------- END SOURCE ----------
FREQUENCY : always