Files
openide/java/java-impl/resources/inspectionDescriptions/OptionalAssignedToNull.html
2025-02-05 04:43:28 +00:00

26 lines
1014 B
HTML

<html>
<body>
Reports <code>null</code> assigned to an <code>Optional</code> variable or returned from a method returning <code>Optional</code>.
<p>It's recommended that you use <code>Optional.empty()</code> (or <code>Optional.absent()</code> for Guava) to denote an empty value.</p>
<p>Example:</p>
<pre><code>
Optional&lt;Integer&gt; foo(boolean flag) {
return flag ? Optional.of(42) : null;
}
</code></pre>
<p>After the quick-fix is applied:</p>
<pre><code>
Optional&lt;Integer&gt; foo(boolean flag) {
return flag ? Optional.of(42) : Optional.empty();
}
</code></pre>
<!-- tooltip end -->
<p>Configure the inspection:</p>
<p>
Use the <b>Report comparison of Optional with null</b> option to also report comparisons like <code>optional == null</code>. While in rare cases (e.g. lazily initialized
optional field) this might be correct, optional variable is usually never null, and probably <code>optional.isPresent()</code> was
intended.
</p>
<p><small>New in 2017.2</small>
</body>
</html>