Skip to content

Specify @SuppressWarnings suppression strings relevant to analyses powered by our annotations #55

Open

Description

@amaembo

It seems we have a consensus that our project not only specifies which code is correct and which is not but also specifies which kinds of warnings at which code locations should be issued. So it seems reasonable also to specify ways to suppress such warnings. I believe it's not the top priority problem. Probably we may even skip it for the first release, allowing tools to use their current suppression mechanisms. But having a conversion about this won't harm.

Note that we should provide suppressions for the specified warnings only. And we probably should not overspecify. E.g.

void foo() {
  getNullable().bar(); // we specify that the warning should be issued here
  if (getNotNull() == null) { // we don't specify (at least for now) whether here should be a warning
  }
}

I believe, all the tools which report nullability violations already have ways to suppress the warnings. Most of them support a standard @SuppressWarnings annotation. A notable exception is bytecode analyzers such as FindBugs and SpotBugs. As they don't see the source code normally, and @SuppressWarnings annotation has SOURCE retention, they cannot see it.. So if we recommend using @SuppressWarnings, these tools will be unable to follow our recommendations. Both of them define a CLASS-level @SuppressFBWarnings annotation (also they allow to define such annotation in any package you like if you don't want an external dependency).

Suppression of FindBugs problem requires to specify the internal ID of the issued warning. E.g. @SuppressFBWarnings("NP_NULL_PARAM_DEREF_NONVIRTUAL") for non-virtual method parameter dereference. They are too granular and, I believe, we don't need to specify such granularity. On the other hand, it's possible to suppress by prefix like @SuppressFBWarnings("NP"): suppresses all NPE-related warnings (though IIRC not all of them actually start with "NP").

IntelliJIDEA supports standard @SuppressWarnings annotation, as well as special comment like //noinspection ... which allows suppressing warning on every statement inside method body (not only on the declaration statement). As a value, you should specify a "suppressionId" which often coincides with internal inspection ID. Here we have the opposite picture: most of the nullability problems are reported by too broad inspection named "Constant Conditions & Exceptions", so you have to write //noinspection ConstantConditions or @SuppressWarnings("ConstantConditions"). This looks crazy and also may suppress many more warnings like "Condition is always true" or "Array index is out of bounds". So I would definitely not recommend using IntelliJIDEA suppression ID as a standard.

Eclipse supports standard @SuppressWarnings annotation (no way to suppress for individual statement), and their suppress IDs looks much better than ones IntelliJ or FindBugs has. For nullability violation, it's just a @SuppressWarnings("null"). This suppression is not too specific and not too broad. To me, it looks the best way to suppress the warnings about unsafe conversion from nullable type to non-nullable (assign nullable to not-null; pass nullable argument as not-null parameter; dereference nullable; throw nullable; unbox nullable; etc.). However, we also need to be able to suppress strict mode warnings (conversion from nullable to unknown and from unknown to not-null), these should have separate suppress ID (I'm inclined to have single suppress ID for both cases). @SuppressWarnings("unchecked-null") looks fine to me, though you may suggest something better.

Now it's unclear whether we should have the same suppress ID when the implicit conversion of container element nullability is performed. E.g.:

List<@Nullable String> list1 = ...
List<@NotNull String> list2 = list1;

I think we should specify that the warning must be issued at list2 = list1 declaration. Should it have the same "null" suppress ID or something different?

Please share how suppression works in your tools (Sonar, ErrorProne, Checker Framework, etc.) Thank you!

Activity

  1. stephan-herrmann commented on Aug 2, 2019

    @stephan-herrmann

    Thanks for bringing this up!

    In addition to @SuppressWarnings Eclipse also has a few precedents where line comments control reporting of warnings, like //$NON-NLS-1$, //$FALL-THROUGH$ etc. If the group considers per-line suppression important, I would be fine with adding one or two of these (for statements that are not local variable declarations).

    If the group thinks @SuppressWarnings("unchecked-null") worthwhile, I might agree, too. Just wondering if "null-unchecked" would be more appropriate (giving emphasis to "null"), but that's close to splitting hairs already :)

  2. wmdietlGC commented on Aug 2, 2019

    @wmdietlGC
    Collaborator

    For the Checker Framework, the relevant documentation is here:

    In @SuppressWarnings we use a type-system specific prefix, in this case nullness, possibly followed by a specific error key.
    For example @SuppressWarnings("nullness:assignment.type.incompatible") suppresses warnings from incompatible types in an assignment, for the Nullness Checker only.
    One can also only specify a type system or only an error message key.

    For finer-grained suppressions we support both assert statements and checkNotNull methods.

  3. kevinb9n commented on Aug 14, 2019

    @kevinb9n
    Collaborator

    I see a lot of value in standardizing suppression strings. They are API!

    I believe we can think of this as an orthogonal effort to standardizing the annotations and their semantics. But to me it does belong under the 'codeanalysis' banner, and even though our current github and group names happen to include "annotations" I am totally fine with treating this as in scope.

  4. kevinb9n commented on Aug 14, 2019

    @kevinb9n
    Collaborator

    I think giving some hierarchical structure to the strings is an interesting idea that could prove useful. Suppose checker A cares about distinguishing "a:b:c" from "a:b:d", but checker B only recognizes all of "a:b" as a single category. The convention could be, then, that it ignores the unrecognized ":c" or ":d" suffix. A lot more thought will have to go into this, though.

  5. cpovirk commented on Aug 15, 2019

    @cpovirk
    Collaborator

    One thing that I could imagine is wanting is to see errors for unsafe conversions due to unannotated code but wanting not to see them for cases like Map.get. I'm not sure that that's particularly well defined or easy to understand, though, nor whether I'd really want it when push came to shove.

  6. cpovirk commented on Aug 15, 2019

    @cpovirk
    Collaborator

    (One possible approach: Map.get is declared @UnknownNullness("key-not-present"), and I can suppress key-not-present errors (which might also include Table.get, Android APIs for looking up UI elements by key, etc.).)

  7. changed the title [-]Warning suppression[/-] [+]Specify @SuppressWarnings suppression strings relevant to analyses powered by our annotations[/+] on Nov 26, 2019
  8. added
    nullness
    on Dec 5, 2019
  9. kevinb9n commented on Jun 23, 2021

    @kevinb9n
    Collaborator

    I still believe in this one and have a proposal to pitch at some point. It doesn't feel urgent, but if anyone else is thinking about this, drop me a note.

  10. added this to the Post-1.0 milestone on Oct 13, 2021
  11. modified the milestones: Post-1.0, 1.0 on Sep 20, 2022
  12. added
    design
    and removed on Nov 30, 2022
  13. modified the milestones: 1.0, Post-1.0 on Mar 27, 2024
  14. netdpb commented on Apr 9, 2024

    @netdpb
    Collaborator

    Current decision: We will not include this feature in 1.0.

    Proposal for 1.0: Finalize the current decision. If you agree, please add a thumbs-up emoji (👍) to this comment. If you disagree, please add a thumbs-down emoji (👎) to this comment and briefly explain your disagreement. Please only add a thumbs-down if you feel you can make a strong case why this decision will be materially worse for users or tool providers than an alternative.

  15. added
    needs-decision-post-1.0
    and removed on Apr 9, 2024
  16. kevinb9n commented on Sep 1, 2024

    @kevinb9n
    Collaborator

    Simple thing we can do today: add to our spec that "nullness" must be respected as a suppression string for all related warnings.

    Less simple things we can consider to finally properly solve the problem: see a detailed draft proposal from 2021:
    https://docs.google.com/document/d/1yVUhgFaoxE8V8geX8SlrJ5WnKs_9tBplcHkYBmKtJUA/edit?usp=sharing

  17. sdeleuze commented on Sep 23, 2025

    @sdeleuze
    Collaborator

    +1 for supporting @SupressWarnings("nullness") or equivalent in a potential future 1.1 release of the spec.

  18. cpovirk commented on Oct 8, 2025

    @cpovirk
    Collaborator

    One thing that would be interesting to know here is how the JDK might ultimately approach this:

    • Would they use a single kind of suppression for all nullness-related diagnostics, or would they use different strings for different types, possibly even "reusing" existing strings like unchecked?
      • If so, how much should that "future precedent" influence us?
    • If we pick the same string as they do, is that good or bad?
      • Good? If I suppress an error from my nullness checker today, I'd be happy to not have to change/add a suppression string in the future.
      • Bad? If my checker detects problems that javac does not, then the javac suppression may look "unused" to some tools.
  19. agentgt commented on Oct 8, 2025

    @agentgt

    I would hope that each tool have its own distinct string also if there is to be a shared one. If we did go down this path perhaps a recommendation is that the tools do something like:

    @SuppressWarnings("NullAway.nullness")
    @SuppressWarnings("nullness.intellij")

    In a multi-tool environment I often need to suppress warnings for one tool but not the other but this is largely because of bugs in the checkers.

    I suppose the above is pretty obvious but just wanted to check.

  20. garretwilson commented on Jan 13, 2026

    @garretwilson

    A standard warning suppression is needed especially for tests. Here is a real-life scenario. I just added a Newtype to my globalmentor-core library. It doesn't allow null, but a separate serialization framework needs to have tests to ensure it raises the right exception if a Newtype appears for which Newtype.value() is in fact null. Since Newtype uses JSpecify to indicate that Newtype.value() must never be null, we need a way to suppress warnings when we create a test BadNewtype to ensure that the serialization framework does the right thing when it encounters this violation of JSpecify.

    For now I will use @SupressWarnings("nullness") in the code and reference this ticket, but I hope you can standardize it.

  21. kevinb9n commented on Jan 14, 2026

    @kevinb9n
    Collaborator

    Our consensus proposal is to add a notice to the JSpecify specification addressing warning suppression. The notice needs to be carefully wordsmithed, but the gist is that:

    Any "warning" that a tool generates, that is based on nullness information from JSpecify annotations, should be suppressible using the specific case-sensitive suppression string "nullness". It is fine if the tool wants to obey other alternative suppression strings as well.

    This probably shouldn't be the end of the story, but we think it addresses the immediate concern. Any objections or questions?

  22. cpovirk commented on Jan 14, 2026

    @cpovirk
    Collaborator

    @netdpb had mentioned the value of conformance tests for this. Coincidentally, NullAway just got a bug report for a case in which suppression wasn't behaving as expected. The other example I can recall is that @SuppressWarnings is valid on packages modules but that it shouldn't have an effect there. Maybe those suggest some other things worth testing to someone.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    designneeds-decision-post-1.0nullness

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions