Search code examples
javachecker-framework

How to suppress all initialization errors


When I have a simple test-class

public final class InitTest {

    private String field;

    public InitTest() {
        init();
    }

    private void init() {
        field = "";
    }   
}

The initialization checker of the checker-framework correctly reports the issues:

InitTest.java:7: error: [initialization.fields.uninitialized] the constructor does not initialize fields: field
        public InitTest() {
               ^
InitTest.java:8: error: [method.invocation.invalid] call to init() not allowed on the given receiver.
                init();
                    ^
  found   : @UnderInitialization(java.lang.Object.class) @NonNull InitTest
  required: @Initialized @NonNull InitTest
2 errors

According to the docs of the Initialization Checker, it should be able to disable the initialization checker via a command line argument:

To disable initialization checking, supply the command-line argument
-AsuppressWarnings=uninitialized

When we use this argument (in checker-framework 2.2.1) we still get an initialization error:

InitTest.java:8: error: [method.invocation.invalid] call to init() not allowed on the given receiver.
                init();
                    ^
  found   : @UnderInitialization(java.lang.Object.class) @NonNull InitTest
  required: @Initialized @NonNull InitTest
1 error

Am I missing something or is this a bug?

Note: when we use -AsuppressWarnings=initialization then no error is shown anymore - but this would also suppress the nullness-related issues: see this SO discussion.


Solution

  • -AsuppressWarnings=uninitialized suppresses all warnings related to initialization itself. It lets you write a constructor that does not initialize all its fields, for example.

    However, that does not currently disable all other checks in the Checker Framework. The Checker Framework implementation treats the error that you showed as a method call error (that's how it is printed, for example).

    Another way of saying this is that the -AsuppressWarnings=uninitialized affects messages that are printed as initialization errors but not errors that are printed as other types. This behavior is consistent, but it isn't what a user such as you desires. I would call this behavior a Checker Framework bug and report it at the issue tracker.