Repository navigation
Description
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
stephan-herrmann commentedon Aug 2, 2019
Thanks for bringing this up!
In addition to
@SuppressWarningsEclipse 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 :)wmdietlGC commentedon Aug 2, 2019
For the Checker Framework, the relevant documentation is here:
In
@SuppressWarningswe use a type-system specific prefix, in this casenullness, 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
assertstatements andcheckNotNullmethods.kevinb9n commentedon Aug 14, 2019
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.
kevinb9n commentedon Aug 14, 2019
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.cpovirk commentedon Aug 15, 2019
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.cpovirk commentedon Aug 15, 2019
(One possible approach:
Map.getis declared@UnknownNullness("key-not-present"), and I can suppresskey-not-presenterrors (which might also includeTable.get, Android APIs for looking up UI elements by key, etc.).)[-]Warning suppression[/-][+]Specify @SuppressWarnings suppression strings relevant to analyses powered by our annotations[/+]kevinb9n commentedon Jun 23, 2021
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.
netdpb commentedon Apr 9, 2024
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.
kevinb9n commentedon Sep 1, 2024
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
sdeleuze commentedon Sep 23, 2025
+1 for supporting
@SupressWarnings("nullness")or equivalent in a potential future 1.1 release of the spec.cpovirk commentedon Oct 8, 2025
One thing that would be interesting to know here is how the JDK might ultimately approach this:
unchecked?agentgt commentedon Oct 8, 2025
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:
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.
garretwilson commentedon Jan 13, 2026
A standard warning suppression is needed especially for tests. Here is a real-life scenario. I just added a
Newtypeto myglobalmentor-corelibrary. It doesn't allownull, but a separate serialization framework needs to have tests to ensure it raises the right exception if aNewtypeappears for whichNewtype.value()is in factnull. SinceNewtypeuses JSpecify to indicate thatNewtype.value()must never benull, we need a way to suppress warnings when we create a testBadNewtypeto 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.kevinb9n commentedon Jan 14, 2026
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?
cpovirk commentedon Jan 14, 2026
@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
@SuppressWarningsis valid on packages modules but that it shouldn't have an effect there. Maybe those suggest some other things worth testing to someone.