Hello,
I am not sure, if this is the correct category, but I think that false-negatives could occur because the Java Rule RSPEC-2222 is (in my opinion) not strict enough.
RSPEC-2222 states that the following code would comply:
public class MyClass {
public void doSomething() {
Lock lock = new Lock();
if (isInitialized()) {
lock.lock();
// ...
lock.unlock();
}
}
}
But the locked code could throw an unchecked exception and the lock is therefore never unlocked.
In the Lock javadoc the proposed idiom is:
Lock l = ...;
l.lock();
try {
// access the resource protected by this lock
} finally {
l.unlock();
}
This idiom will unlock the lock also in case of an unchecked exception.
My question is: why does not RSPEC-2222 check that unlock is in the finally block?