A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/pmd/pmd/issues/1922 below:

[java] CloseResource possible false positive with Streams · Issue #1922 · pmd/pmd · GitHub

Affects PMD Version: 6.16
Rule: CloseResource
Description:

CloseResource reports that Streams should be closed (indeed they implement AutoCloseable). In nearly all cases those violations are noise though. Closing a stream that doesn't feed on system resources is useless. Only streams that are obtained from eg Files::lines, or Files::walk should really be closed, but they could escape their defining scope and be closed elsewhere (the laziness of the stream API is meant for that). I don't think we can reliably flag those at the moment. So I would say that the rule should not by default consider Stream instances.

Code Sample demonstrating the issue:

 public static <T> Stream<MatchResult<T>> filterResults(List<T> candidates, Function<T, String> matchExtractor, String query, MatchSelector<T> limiter) {
        if (query.length() < MIN_QUERY_LENGTH) {
            return Stream.empty();
        }

       // violation here
        Stream<MatchResult<T>> base = candidates.stream()
                                                .map(it -> {
                                                    String cand = matchExtractor.apply(it);
                                                    return new MatchResult<>(0, it, cand, query, new TextFlow(makeNormalText(cand)));
                                                });
        return limiter.selectBest(base);
    }

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4