openhab / static-code-analysis

Maven tooling for static code analysis
https://www.openhab.org/
Other
32 stars 21 forks source link

False-positive warning when @Nullable is casted to a @NonNull type #431

Closed pfink closed 2 years ago

pfink commented 2 years ago

If I cast a @Nullable type to a @NonNull type, a false-positive warning is thrown.

Example:

public void mymethod(@Nullable Object myobject) {
        if (myobject != null) {
            this.mysecondmethod((@NonNull Object) myobject);
        }
 }

There is no problem with this code, but currently it will wrongly throw the warning: There is no need for a @NonNull annotation because it is set as default. Only @Nullable should be used

wborn commented 2 years ago

When I hit save on the flicbutton files my Eclipse auto save actions removed all those unneccessary casts automatically. The only issue is that you must assign @Nullable fields to a local variable. That's because another thread can change the value of a field at any time to null. So if you annotate fields as @Nullable they can never be inferred as @NonNull, unless you assign them to a local variable.

pfink commented 2 years ago

Ah okay, that makes sense. Probably I should use Eclipse at least when I do bigger PRs next time ^^