rspec/rules/S3062/rule.adoc

23 lines
514 B
Plaintext
Raw Normal View History

Combine inheritance and array covariance, and the results can potentially get nasty. Together, they allow you to write array stores that will compile, but cause errors at runtime.
== Noncompliant Code Example
2022-02-04 17:28:24 +01:00
[source,text]
----
public class Bowl {
abstract class Fruit { }
class Apple : Fruit { }
class Orange : Fruit { }
Fruit [] fruits;
public void fillBowl(){
fruits = new Apple[30];
fruits[0] = new Orange(); // Noncompliant; this compiles but raises exception at runtime
}
}
----