A variable declared to suppress a warning throws the same warning on second assignment.

135 views
Skip to first unread message

Utkarsh Rai

unread,
Dec 4, 2020, 11:26:25 AM12/4/20
to checker-framework-gsoc
Minimum Working Example -

import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;

public class BugCheck {
public static void main(String[] args) {
@SuppressWarnings("assignment.type.incompatible")
@NonNull String s1 = nString1();
s1 = nString2();
}

private static @Nullable String nString2() {
return "World";
}

private static @Nullable String nString1() {
return "Hello";
}
}

Command -
javacheck -processor nullness BugCheck.java

Expected behavior -
Compile normally.

Actual behavior -
BugCheck.java:8: error: [assignment.type.incompatible] incompatible types in assignment.
        s1 = nString2();
                     ^
  found: @Initialized @Nullable String
  required: @UnknownInitialization @NonNull String
1 error


Comments -
This might be the expected behavior to prevent the misuse of variable s1 further in the code, but I read through section 29.1 (@SuppressWarnings annotation) in the manual and did not find any clarifications regarding this, so I decided to report it. This is a recurring pattern in my case study where I have to transfer the (Nullable) contents of a ResultSet to an ArrayList which holds NonNull elements, where I can guarantee that the contents are not null.

Thanks in advance.

Michael Ernst

unread,
Dec 4, 2020, 11:33:28 AM12/4/20
to Utkarsh Rai, checker-framework-gsoc
This is the documented behavior of the @SuppressWarnings annotation.  That warning is defined by Java, so this isn't specific to the Checker Framework.  A @SuppressWarnings annotation applies to all warnings within the program element on which it is written.  The Checker Framework Manual explains this, in the 4th sentence of the section you cited:  "It [a @SuppressWarnings annotation] suppresses all warnings within that program element."  The program element is the declaration, not all uses of the variable.

Mike

--
You received this message because you are subscribed to the Google Groups "checker-framework-gsoc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to checker-framework...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/checker-framework-gsoc/1ac7d2b2-d8ee-44c3-b421-d13f758c014fn%40googlegroups.com.

Utkarsh Rai

unread,
Dec 4, 2020, 2:16:41 PM12/4/20
to checker-framework-gsoc
"The program element is the declaration, not all uses of the variable.", this is what I was missing.

Thanks for the clarification.

Michael Ernst

unread,
Dec 4, 2020, 2:17:42 PM12/4/20
to Utkarsh Rai, checker-framework-gsoc
Reply all
Reply to author
Forward
0 new messages