Strictness of Java RSPEC-2222


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()) {
      // ...

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 = ...;
 try {
   // access the resource protected by this lock
 } finally {

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,


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