typetools / checker-framework

Pluggable type-checking for Java
http://checkerframework.org/
Other
1.02k stars 353 forks source link

We need a way to suppress all initialization errors #1590

Open tmtron opened 6 years ago

tmtron commented 6 years ago

We need a way to suppress all initialization errors (but not the nullness checks).

Example:

    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

Quote from mernst in this Stackoverflow discussion "How to suppress all initialization errors" :

-AsuppressWarnings=uninitialized suppresses all warnings related to initialization itself. It lets you write a constructor that does not initialize all its fields, for example.
The -AsuppressWarnings=uninitialized affects messages that are printed as initialization errors but not errors that are printed as other types.

flix- commented 3 years ago

Latest docs propose to use -AsuppressWarnings=fbc (https://checkerframework.org/manual/#initialization-checking-suppressing-warnings). Initialization warnings are gone however null checks also doesn't seem to be reported anymore:

public class Main {

  public static void main(String[] args) {
    foo(null);
  }

  public static void foo(String a) {}
}

Without suppressing any warnings I'm getting the following error:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project parent: Compilation failure
[ERROR] Main.java:[4,8] error: [argument.type.incompatible] incompatible argument for parameter a of foo.
[ERROR]   found   : null

With -AsuppressWarnings=fbc compilation just runs fine without any warnings. The other mentioned option -AsuppressWarnings=initialization. doesn't have any impact. I was using version 3.8.0.

mernst commented 3 years ago

The manual's mention of the "fbc" SuppressWarnings string is wrong. I'm sorry about that. Thank you for pointing it out; we appreciate it.

There are two relevant SuppressWarnings strings:

You did two experiments.

The behavior you are seeing seems right to me, but let us know if there is another problem.

flix- commented 3 years ago

Thank you for the clarification. However, the problem still persists. "initialization." does not suppress initialization warnings:

public class Foo {

  private String a;

  public Foo() {
    init();
  }

  public void init() {
    a = "gude";
  }
}
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project parent: Compilation failure: Compilation failure:
[ERROR] Foo.java:[5,9] error: [initialization.fields.uninitialized] the constructor does not initialize fields: a
[ERROR] Foo.java:[6,8] error: [method.invocation.invalid] call to init() not allowed on the given receiver.
[ERROR]   found   : @UnderInitialization @NonNull Foo
[ERROR]
[ERROR]   required: @Initialized @NonNull Foo

I am using the following configuration for the maven plugin:

<profile>
      <id>checkerframework</id>
      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
            <executions>
              <execution>
                <id>default-compile</id>
                <phase>compile</phase>
                <goals>
                  <goal>compile</goal>
                </goals>
                <configuration>
                  <fork>true</fork>
                  <compilerArgs>
                    <arg>-proc:only</arg>
                    <arg>-Xmaxerrs</arg>
                    <arg>10000</arg>
                    <arg>-Xmaxwarns</arg>
                    <arg>10000</arg>
                    <arg>-J--add-opens=jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED</arg>
                    <arg>-AsuppressWarnings=initialization.</arg>
                  </compilerArgs>
                  <annotationProcessorPaths>
                    <path>
                      <groupId>org.checkerframework</groupId>
                      <artifactId>checker</artifactId>
                      <version>${checkerframework.version}</version>
                    </path>
                  </annotationProcessorPaths>
                  <annotationProcessors>
                    <annotationProcessor>org.checkerframework.checker.nullness.NullnessChecker</annotationProcessor>
                  </annotationProcessors>
                </configuration>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
      <properties>
        <!-- Temporary until animal-sniffer-maven-plugin is released with https://github.com/mojohaus/animal-sniffer/pull/91.
             Version 1.19 is broken:  https://github.com/mojohaus/animal-sniffer/issues/76. -->
        <animal.sniffer.skip>true</animal.sniffer.skip>
      </properties>
    </profile>
mernst commented 3 years ago

You are right. The behavior changed in version 3.2.0 early this year, but the documentation was not updated correctly at that time. The wrong manual is confusing to both users and developers!

To revise my previous statement, there are two relevant SuppressWarnings strings:

Thanks again for reporting this and bearing with us while we fix this documentation.

flix- commented 3 years ago

I am still puzzled. I would like to only have null checking and no initialization warnings/errors. I tried "fbc", "initialization", and "initialization." options. None of them yield the desired behavior. Am I correctly assuming that currently there is no way to suppress initialization errors but have full null validation support?

flix- commented 3 years ago

@mernst Any update on this?

mernst commented 3 years ago

@flix- Can you please take a look at the "Suppressing Warnings" section in the Initialization Checker manual and let me know if it is not clear? I think it directly answers your question, but if it is unclear or there is a bug, let us know and we will fix it. Thanks.

starkos commented 1 year ago

Encountering this issue here. Adding Checker to a large existing project which does a lot of explicit dependency injection, passing class references around in constructors to be stored for later use. So we're seeing lots of errors like:

  found   : @UnderInitialization(com.its.idmp.aws.AwsInstallStep.class) @NonNull AwsInstallStep
  required: @Initialized @NonNull AwsInstallStep

In this case we know the code is fine. We want to turn off all of the @UnderInitialization warnings, and only those warnings. I've tried the examples mentioned above and none of them do it. Is there something in the manual I'm missing?

mernst commented 1 year ago

I'm sorry you are having trouble. Could you please provide a concrete example of code and exactly what you did that didn't work? Otherwise it is hard to make a suggestion.

Did you try the suggestion in the "Suppressing Warnings" section of the "Initialization Checker" section of the manual? It says:

You can write @SuppressWarnings("initialization") on a field, constructor, or class, or pass the command-line argument -AsuppressWarnings=initialization when running the Nullness Checker.