Hi everyone,
I’m writing a custom rule to check one method called before another on the same variable. Understand that it can depend on runtime information, but it’s not my case.
Here is a test code for this example:
class A
{
void method() {
ClassB x1 = getInstanceWithMandatory();
x1.get(); // Compliant
ClassB x2 = getInstance();
x2.get(); // Noncompliant: mandatoryMethod() not called before get()
}
void getInstanceWithMandatory() {
ClassB x = new ClassB();
x.mandatoryMethod();
return x;
}
void getInstance() {
return new ClassB();
}
}
I found a similar rule was implemented OptionalGetBeforeIsPresentCheck using ProgramState / Constraints approach, but not sure is it an option for my case and it’s not covered in the documentation.
What would you recommend to implement such a rule, please?