Skip to content

[java] CloseResource false positive with Stream and ternary #2835

@adangel

Description

@adangel

Fun one:
Ensure that resources like this boolean object are closed after use

Because of:

final Stream<V1Container> containers = spec.getContainers() != null ? spec.getContainers().stream() : Stream.empty();

How the hell do I close a boolean? :)

Replaced with this and the problem went away:

final Stream<V1Container> containers = Optional.ofNullable(spec.getContainers()).map(Collection::stream).orElse(Stream.empty());

Originally posted by @marcopelegrini in https://github.com/pmd/pmd/issue_comments/710488934 from #1911

Metadata

Metadata

Assignees

No one assigned

    Labels

    a:false-positivePMD flags a piece of code that is not problematic

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions