Strictness of Java RSPEC-2222

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?

1 Like

Hi @gillesB,

Thank you for noticing and reporting.
Here is a ticket to fix this [SONARJAVA-4604] - Jira

All the best,

Irina

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.