ctolkmit / checker-framework

Automatically exported from code.google.com/p/checker-framework
0 stars 0 forks source link

Bogus Warning with Optional.ofNullable #366

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1.Run the Null Checker on the class below
package test;

import java.util.Optional;

class Test {
    static Optional<String> getPossiblyEmptyString() {
        String s = System.currentTimeMillis() % 2 == 0 ? null : "String";
        return Optional.ofNullable(s);
    }
}

What is the expected output? What do you see instead?
Expected: no error
Actual: test\Test.java:8: error: [return.type.incompatible] incompatible types 
in return.
        return Optional.ofNullable(s);
                                  ^
  found   : @Initialized @NonNull Optional<@Initialized @Nullable String>
  required: @Initialized @NonNull Optional<@Initialized @NonNull String>
1 error

What version of the product are you using? On what operating system?
1.8.6/Windows 8/Java 8

Original issue reported on code.google.com by ClovisSe...@gmail.com on 28 Sep 2014 at 9:24

GoogleCodeExporter commented 9 years ago
Hi,

As written there is no bug because the declared return type Optional<String> is 
defaulted to @NonNull Optional<@NonNull String>, and the type returned from 
Optional.ofNullable is Option<@Nullable String>.

However, if you changing the return type to Optional<@Nullable String> reveals 
that Optional class does not yet have the correct api annotations in the 
checker framework (so you will get a different error). I am working on adding 
the api annotations for the Optional class. This should go out in our release 
next week, and pushed to the repo sometime before then.

Thanks for your interest!

Original comment by mcart...@cs.washington.edu on 21 Nov 2014 at 3:12

GoogleCodeExporter commented 9 years ago
This issue was closed by revision 28e0256c8b1b.

Original comment by mcart...@cs.washington.edu on 24 Nov 2014 at 6:31

GoogleCodeExporter commented 9 years ago

Original comment by mcart...@cs.washington.edu on 24 Nov 2014 at 6:32

GoogleCodeExporter commented 9 years ago

Original comment by mcart...@cs.washington.edu on 26 Nov 2014 at 8:58