Skip to content

Spurious resource leak report when returning before a variable's definition #6317

@Calvin-L

Description

@Calvin-L

Given this code:

import java.io.*;
import org.checkerframework.checker.nullness.qual.*;
import org.checkerframework.checker.mustcall.qual.*;
import org.checkerframework.checker.calledmethods.qual.*;

abstract class TestCase {

  abstract @Nullable Closeable alloc();
  abstract boolean arbitraryChoice();

  void method() throws IOException {

    if (arbitraryChoice()) {
      return;
    }

    Closeable r1 = alloc();
    if (r1 == null) {
      return;
    }
    r1.close();

  }

}

The RLC reports

error: [required.method.not.called] @MustCall method close may not have been invoked on r1 or any of its aliases.
      Closeable r1 = alloc();
                ^
  The type of object is: java.io.Closeable.
  Reason for going out of scope: regular method exit

The error looks spurious. Indeed, removing the early return block

      if (arbitraryChoice()) {
        return;
      }

silences the warning! It appears that the RLC wants us to close r1 at the premature return site, before it has been defined.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions