Skip to content

False positive in lambda inside a method returning Optional #80

@Tuupertunut

Description

@Tuupertunut

If in a method that returns Optional, a lambda expression returns something else than Optional, that lambda gives a false positive warning of not returning Optional.

public Optional<Object> getSomeOptional() {
    Function<String, String> f = (String s) -> {
        return "text";
    };
    return Optional.empty();
}

In the above example, the return "text" correctly returns a String, but Netbeans still gives the following warning:
Convert to Optional.ofNullable()/Optional.of(). The return value should be an Optional instance for a method with a return value java.util.Optional.

The warning probably thinks return "text" is a return statement of the method, even though it really is a return statement of the lambda.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions