rspec/rules/S2637/java/rule.adoc
Philipp Dominik Schubert f45132d5aa
Modify rule S2637: Expand and adjust for LaYC
## Review

A dedicated reviewer checked the rule description successfully for:

- [x] logical errors and incorrect information
- [x] information gaps and missing content
- [x] text style and tone
- [x] PR summary and labels follow [the
guidelines](https://github.com/SonarSource/rspec/#to-modify-an-existing-rule)

---------

Co-authored-by: Arseniy Zaostrovnykh <arseniy.zaostrovnykh@sonarsource.com>
2023-10-04 13:02:47 +02:00

47 lines
1.1 KiB
Plaintext

== Why is this an issue?
Fields, parameters and return values marked ``++@NotNull++``, ``++@NonNull++``, or ``++@Nonnull++`` are assumed to have non-null values and are not typically null-checked before use. Therefore setting one of these values to ``++null++``, or failing to set such a class field in a constructor, could cause ``++NullPointerException++``s at runtime.
=== Noncompliant code example
[source,java]
----
public class MainClass {
@Nonnull
private String primary;
private String secondary;
public MainClass(String color) {
if (color != null) {
secondary = null;
}
primary = color; // Noncompliant; "primary" is Nonnull but could be set to null here
}
public MainClass() { // Noncompliant; "primary" is Nonnull but is not initialized
}
@Nonnull
public String indirectMix() {
String mix = null;
return mix; // Noncompliant; return value is Nonnull, but null is returned.
}
----
== Resources
include::../standards.adoc[]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
include::../message.adoc[]
endif::env-github,rspecator-view[]